Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy matrix to array

If you'd like something a bit more readable, you can do this:

A = np.squeeze(np.asarray(M))

Equivalently, you could also do: A = np.asarray(M).reshape(-1), but that's a bit less easy to read.


result = M.A1

https://numpy.org/doc/stable/reference/generated/numpy.matrix.A1.html

matrix.A1
1-d base array

A, = np.array(M.T)

depends what you mean by elegance i suppose but thats what i would do


You can try the following variant:

result=np.array(M).flatten()

np.array(M).ravel()

If you care for speed; But if you care for memory:

np.asarray(M).ravel()

Or you could try to avoid some temps with

A = M.view(np.ndarray)
A.shape = -1

First, Mv = numpy.asarray(M.T), which gives you a 4x1 but 2D array.

Then, perform A = Mv[0,:], which gives you what you want. You could put them together, as numpy.asarray(M.T)[0,:].