Numpy: Why Is Difference Of A (2,1) Array And A Vertical Matrix Slice Not A (2,1) Array
Solution 1:
numpy.array
indexes such that a single value in any position collapses that dimension, while slicing retains it, even if the slice is only one element wide. This is completely consistent, for any number of dimensions:
>> A = numpy.arange(27).reshape(3, 3, 3)
>> A[0, 0, 0].shape
()
>> A[:, 0, 0].shape
(3,)
>> A[:, :, 0].shape
(3, 3)
>> A[:1, :1, :1].shape
(1, 1, 1)
Notice that every time a single number is used, that dimension is dropped.
You can obtain the semantics you expect by using numpy.matrix
, where two single indexes return a order 0 array and all other types of indexing return matrices
>> M = numpy.asmatrix(numpy.arange(9).reshape(3, 3))
>> M[0, 0].shape
()
>> M[:, 0].shape # This is different from the array
(3, 1)
>> M[:1, :1].shape
(1, 1)
Your example works as you expect when you use numpy.matrix
:
>> x = numpy.matrix([[1],[3]])
>> M = numpy.matrix([[1,2],[3,4]])
>> y = M[:, 0]
>> x - y
matrix([[0],
[0]])
Solution 2:
Look at the shape of y
. It is (2,)
; 1d. The source array is (2,2), but you are selecting one column. M[:,0]
not only selects the column, but removes that singleton dimension.
So we have for the 2 operations, this change in shape:
M[:,0]: (2,2) => (2,)
x - y: (2,1) (2,) => (2,1), (1,2) => (2,2)
There are various ways of ensuring that y
has the shape (2,1). Index with a list/vector, M[:,[0]]
; index with a slice, M[:,:1]
. Add a dimension, M[:,0,None]
.
Think also what happens when M[0,:]
or M[0,0]
.
Post a Comment for "Numpy: Why Is Difference Of A (2,1) Array And A Vertical Matrix Slice Not A (2,1) Array"