Numpy matrix to array

PythonArraysMatrixNumpy

Python Problem Overview


I am using numpy. I have a matrix with 1 column and N rows and I want to get an array from with N elements.

For example, if i have M = matrix([[1], [2], [3], [4]]), I want to get A = array([1,2,3,4]).

To achieve it, I use A = np.array(M.T)[0]. Does anyone know a more elegant way to get the same result?

Thanks!

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

Solution 3 - Python

A, = np.array(M.T)

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

Solution 4 - Python

You can try the following variant:

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

Solution 5 - Python

np.array(M).ravel()

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

np.asarray(M).ravel()

Solution 6 - Python

Or you could try to avoid some temps with

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

Solution 7 - Python

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,:].

Solution 8 - Python

This will convert the matrix into array

A = np.ravel(M).T

Solution 9 - Python

ravel() and flatten() functions from numpy are two techniques that I would try here. I will like to add to the posts made by Joe, Siraj, bubble and Kevad.

Ravel:

A = M.ravel()
print A, A.shape
>>> [1 2 3 4] (4,)

Flatten:

M = np.array([[1], [2], [3], [4]])
A = M.flatten()
print A, A.shape
>>> [1 2 3 4] (4,)

numpy.ravel() is faster, since it is a library level function which does not make any copy of the array. However, any change in array A will carry itself over to the original array M if you are using numpy.ravel().

numpy.flatten() is slower than numpy.ravel(). But if you are using numpy.flatten() to create A, then changes in A will not get carried over to the original array M.

numpy.squeeze() and M.reshape(-1) are slower than numpy.flatten() and numpy.ravel().

%timeit M.ravel()
>>> 1000000 loops, best of 3: 309 ns per loop

%timeit M.flatten()
>>> 1000000 loops, best of 3: 650 ns per loop

%timeit M.reshape(-1)
>>> 1000000 loops, best of 3: 755 ns per loop

%timeit np.squeeze(M)
>>> 1000000 loops, best of 3: 886 ns per loop

Solution 10 - Python

Came in a little late, hope this helps someone,

np.array(M.flat)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyassinView Question on Stackoverflow
Solution 1 - PythonJoe KingtonView Answer on Stackoverflow
Solution 2 - PythonhpauljView Answer on Stackoverflow
Solution 3 - PythonmvuView Answer on Stackoverflow
Solution 4 - PythonbubbleView Answer on Stackoverflow
Solution 5 - PythonKevadView Answer on Stackoverflow
Solution 6 - PythonPierre GMView Answer on Stackoverflow
Solution 7 - PythonoracleyueView Answer on Stackoverflow
Solution 8 - PythonSiraj S.View Answer on Stackoverflow
Solution 9 - PythonSiddharth SatpathyView Answer on Stackoverflow
Solution 10 - PythonCephas SvosveView Answer on Stackoverflow