How Do I Consistently Flatten A Numpy Array?
from numpy import array, eye, matrix x = array([1, 0]) A = eye(2) print(A.dot(x)) prints [1. 0.]. On the other hand, B = matrix([[1, 0], [0, 1]]) print(B.dot(x)) prints [[1 0]]
Solution 1:
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
Post a Comment for "How Do I Consistently Flatten A Numpy Array?"