I have encountered a weird behaviour of NumPy array. I am working on some matrix algebra examples and i found out that you can easily replace an array column (or a row for that matter) with various types of data but not with an array that has the correct number of rows and single column.
Let's have an array:
>>> import numpy as np
>>>
>>> A = np.zeros([4,4])
>>> A
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
Now let's assign some values to rows/columns:
>>> A[0,:] = [1,1,1,1]
>>> A
array([[1., 1., 1., 1.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
>>>
>>> A[:,0] = np.array([2,2,2,2])
>>> A
array([[2., 1., 1., 1.],
[2., 0., 0., 0.],
[2., 0., 0., 0.],
[2., 0., 0., 0.]])
>>>
>>> A[2,:] = np.array([[3,3,3,3]])
>>> A
array([[2., 1., 1., 1.],
[2., 0., 0., 0.],
[3., 3., 3., 3.],
[2., 0., 0., 0.]])
>>>
>>> A[:,2] = np.array([[4,4,4,4]])
>>> A
array([[2., 1., 4., 1.],
[2., 0., 4., 0.],
[3., 3., 4., 3.],
[2., 0., 4., 0.]])
In the last two replacements I assigned a 1x4 array as a new row and a 1x4 array as a new column. But for reason unknown to me i cannot assign an array 4x1 to a column or a row of an existing array:
>>> A[:,3] = np.array([[5],[5],[5],[5]])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not broadcast input array from shape (4,1) into shape (4,)
I googled the error and I still think this might be a design flaw.
Is there an actual reason to not permit an assignment of an Nx1 array as a column to a NxM array?