How do I consistently flatten a numpy array?

前端 未结 1 1732
萌比男神i
萌比男神i 2021-01-15 03:57
from numpy import array, eye, matrix

x = array([1, 0])
A = eye(2)
print(A.dot(x))

prints [1. 0.].

On the other hand,

1条回答
  •  广开言路
    2021-01-15 04:19

    Stop using matrix. numpy.matrix.flatten returns a 1-row matrix, because that's as flat as matrix instances get. If for some reason you are dead set on using matrix, convert to ndarray with matrix.A before flattening:

    flat = whatever_matrix.A.flatten()
    

    or just use A1 to get a flat ndarray directly:

    flat = whatever_matrix.A1
    

    0 讨论(0)
提交回复
热议问题