Home > Blockchain >  Python NumPy, remove unnecessary brackets
Python NumPy, remove unnecessary brackets

Time:02-03

In Numpy, Transposing of a column vector makes the the array an embedded array. For example, transposing [[1.],[2.],[3.]] gives [[1., 2., 3.]] and the dimension of the outermost array is 1. And this produces many errors in my code. Is there a way to produce [1., 2., 3.] directly?

CodePudding user response:

Try .flatten(), .ravel(), .reshape(-1), .squeeze().

CodePudding user response:

Yes, you can use the .flatten() method of a Numpy array to convert a multi-dimensional array into a one-dimensional array. In your case, you can simply call .flatten() on the transposed array to get the desired result:

import numpy as np
column_vector = np.array([[1.], [2,], [3,]])
flattened_array = column_vector.T.flatten()
print(flattened_array)

Output:

[1. 2. 3.]
  • Related