Reputation: 1104
I've come across seemingly inconsistent results when flattening certain numpy arrays with numpy.reshape. Sometimes if I reshape an array it returns a 2D array with one row, whereas if I first copy the array then do the exact same operation, it returns a 1D array.
This seems to happen primarily when combining numpy arrays with scipy arrays, and creates alignment problems when I want to later multiply the flattened array by a matrix.
For example, consider the following code:
import numpy as np
import scipy.sparse as sps
n = 10
A = np.random.randn(n,n)
I = sps.eye(n)
X = I+A
x1 = np.reshape(X, -1)
x2 = np.reshape(np.copy(X), -1)
print 'x1.shape=', x1.shape
print 'x2.shape=', x2.shape
When run it prints:
x1.shape= (1, 100)
x2.shape= (100,)
The same thing happens with numpy.flatten(). What is going on here? Is this behavior intentional?
Upvotes: 3
Views: 189
Reputation: 281252
You added together a sparse matrix object and a normal ndarray:
X = I+A
The result is a dense matrix object, an instance of np.matrix
, not a normal ndarray.
This:
np.reshape(X, -1)
ends up returning a matrix, which can't be less than 2D.
This:
np.reshape(np.copy(X), -1)
makes a normal ndarray in np.copy(X)
, so you get a 1D output from np.reshape
.
Be very careful at all times about whether you're dealing with sparse matrices, dense matrices, or standard ndarrays. Avoid np.matrix
whenever possible.
Upvotes: 5