42

I using the dot() function from numpy to multiply a matrix of 3x3 with a numpy.array of 1x3. 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 1x3 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?

Javaaaa
  • 3,788
  • 7
  • 43
  • 54

10 Answers10

46

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()
JoshAdel
  • 66,734
  • 27
  • 141
  • 140
15

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]]
tito
  • 12,990
  • 1
  • 55
  • 75
  • the problem is that the whole matrix then becomes 1 element fo a list with 1 element. Like: [[0.16666666666666666, 0.6666666666666666, 0.16666666666666666]]. I need every value to be an element of a new list of length 3. How can i do that? – Javaaaa Mar 03 '11 at 16:34
  • Same as other comments said: numpy.array(a).reshape(-1,).tolist() or use ravel() – tito Mar 03 '11 at 16:44
15

If a is your matrix, try

a.ravel().tolist()

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

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • I know I don't need that to iterate over it, but a whole class is written to handle lists, so it is easier if I make a list of it then to change the whole class. However, with your solution I too get a list with 1 element that is the matrix instead of a list with 3 elements. How can i make the latter? any ideas? – Javaaaa Mar 03 '11 at 16:36
  • 1
    @Javaaaa: You get a list with a single item that is a list itself. Simply use `[0]` to retrieve that single item. – Sven Marnach Mar 03 '11 at 17:14
  • `ravel()` is redundant: `b.tolist() == b.ravel().tolist()` >>> `True` – Martin Nov 01 '15 at 21:04
  • 2
    @Martin: No, it's only redundant if your matrix only has a single line in the first place. – Sven Marnach Nov 02 '15 at 11:45
10

Another way:

>>> import numpy as np
>>> m = np.matrix([1,2,3])
>>> np.array(m).flatten().tolist()
[1,2,3]
Alejandro
  • 1,002
  • 8
  • 14
2

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]
yourstruly
  • 972
  • 1
  • 9
  • 17
1

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
FerYepes
  • 71
  • 1
1

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

rene
  • 41,474
  • 78
  • 114
  • 152
Robert Ribas
  • 289
  • 3
  • 7
1
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
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
0

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])
Aaron Feldman
  • 474
  • 4
  • 7
  • As shown in http://stackoverflow.com/a/5183572/901925 `tolist()` on a 2d array (e.g. matrix) produces a nested list. I assume that's what you are calling a 'typical 2D list`. – hpaulj May 26 '14 at 04:14
0
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.

user7358
  • 109
  • 1