The Python Oracle

Numpy matrix to array

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Book End

--

Chapters
00:00 Question
00:43 Accepted answer (Score 234)
01:05 Answer 2 (Score 147)
01:18 Answer 3 (Score 13)
01:32 Answer 4 (Score 12)
01:45 Thank you

--

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

Answer 1 links:
https://numpy.org/doc/stable/reference/g...

--

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