How to make List from Numpy Matrix in Python

64,202

Solution 1

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()

Solution 2

If a is your matrix, try

a.ravel().tolist()

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

Solution 3

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]]

Solution 4

Another way:

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

Solution 5

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]
Share:
64,202
Javaaaa
Author by

Javaaaa

If you want to get better and more successful with spreadsheets check out my website SpreadsheetPro.net Also check out http://jsonprettyprint.net

Updated on July 09, 2022

Comments

  • Javaaaa
    Javaaaa almost 2 years

    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
    Javaaaa about 13 years
    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
    Javaaaa about 13 years
    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?
  • tito
    tito about 13 years
    Same as other comments said: numpy.array(a).reshape(-1,).tolist() or use ravel()
  • Sven Marnach
    Sven Marnach about 13 years
    @Javaaaa: You get a list with a single item that is a list itself. Simply use [0] to retrieve that single item.
  • hpaulj
    hpaulj almost 10 years
    As shown in 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`.
  • niyasc
    niyasc about 9 years
    Please try to explain your answer in a few words even if your code is working
  • Martin
    Martin over 8 years
    ravel() is redundant: b.tolist() == b.ravel().tolist() >>> True
  • Sven Marnach
    Sven Marnach over 8 years
    @Martin: No, it's only redundant if your matrix only has a single line in the first place.