How to make List from Numpy Matrix in Python

Question:

I using the dot() function from numpy to multiply a matrix of 3×3 with a numpy.array of 1×3. The output is for example this:

[[ 0.16666667 0.66666667 0.16666667]]

which is of type:

<class 'numpy.matrixlib.defmatrix.matrix'>

how can I convert this to a list. Because I know the result will always be a matrix of 1×3 so it should be coverted to a list because I need to be able to loop through it later for calculation the pearson distance of two of those lists.

So to summarize: how can I make a list from this matrix?

Asked By: Javaaaa

||

Answers:

Use the tolist() method on the matrix object :

>>> import numpy
>>> m = numpy.matrix([1, 2, 3])
>>> type(m)
<class 'numpy.core.defmatrix.matrix'>
>>> m.tolist()
[[1, 2, 3]]
Answered By: tito

If a is your matrix, try

a.ravel().tolist()

but you don’t need to turn it into a list to iterate over it.

Answered By: Sven Marnach

May not be the optimal way to do this but the following works:

a = numpy.matrix([[ 0.16666667, 0.66666667, 0.16666667]])
list(numpy.array(a).reshape(-1,))

or

numpy.array(a).reshape(-1,).tolist()

or

numpy.array(a)[0].tolist()
Answered By: JoshAdel
m = numpy.matrix([[ 0.16666667, 0.66666667, 0.16666667]])
a = numpy.array(m)[0]

for i in a:
    print i

results in

0.16666667
0.66666667
0.16666667
Answered By: Hugh Bothwell

Another way:

>>> import numpy as np
>>> m = np.matrix([1,2,3])
>>> np.array(m).flatten().tolist()
[1,2,3]
Answered By: Alejandro

I came here looking for a way to convert numpy matrices to typical 2D lists.

For a numpy matrix m:

my_2d_list = map(list, list(m.A))

If you just want a one dimensional list from a 1 x n matrix m:

my_1d_list = list(list(m.A)[0])
Answered By: Aaron Feldman

Try this simplistic approach. It works with 1D arrays, do not know with higher dimensions.

import mumpy as np         # to create a numpy array example
a = np.array([1,2.5,3])    # your 1D numpy array
b = [i for i in a]        # your list out of the original numpy array
Answered By: FerYepes
import numpy as np
a = np.matrix([[1,2,3,4]])
b = map(float, a.transpose())

This code snippet will apply the built-in function “float” – which converts something to a floating point number – to every element of a. Since the first element of a is an array itself, it has to be transposed, so that every number becomes an element of a itself. a.transpose() is equivalent to np.matrix([[1],[2],[3],[4]]) in this example.

Answered By: user7358

why not simple:

list(a.flat)

for example:

>>> import numpy as np
>>> a = np.matrix([[ 0.16666667, 0.66666667, 0.16666667]])
>>> a
matrix([[ 0.16666667,  0.66666667,  0.16666667]])
>>> a.flat
<numpy.flatiter object at 0x0000000002DE8CC0>
>>> a.flat.tolist()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'numpy.flatiter' object has no attribute 'tolist'
>>> list(a.flat)
[0.16666666999999999, 0.66666667000000002, 0.16666666999999999]
Answered By: yourstruly

I think getA1() can do the job.
From the documentation:

getA1()

Return self as a flattened ndarray.

Equivalent to np.asarray(x).ravel()

From https://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.getA1.html

Answered By: Robert Ribas
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.