The Python Oracle

Numpy matrix to array

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands

--

Chapters
00:00 Numpy Matrix To Array
00:32 Accepted Answer Score 243
00:47 Answer 2 Score 13
00:56 Answer 3 Score 14
01:06 Answer 4 Score 151
01:12 Thank you

--

Full question
https://stackoverflow.com/questions/3337...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #arrays #matrix #numpy

#avk47



ACCEPTED ANSWER

Score 243


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.




ANSWER 2

Score 151


result = M.A1

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

matrix.A1
1-d base array



ANSWER 3

Score 14


You can try the following variant:

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



ANSWER 4

Score 13


A, = np.array(M.T)

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