It is the expected behavior, you reassigned the variable so that it now points to a new object. The first array will be simply deleted by the GC.
To do what you want you can simply assign the values of the second matrix to the first using indexing. If you are always going to have the same number of columns you can do:
>>> import numpy as np
>>> a = np.zeros((4, 6))
>>> a
array([[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]])
>>> b = np.ones((2, 6))
>>> b
array([[ 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1.]])
>>> a[:b.shape[0]]
>>> a
array([[ 1., 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1., 1.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]])
If we want to do this with an array that has fewer columns than the first one, it is enough to indicate if we want it to be added to the left or to the right (or in any position):
>>> import numpy as np
>>> a = np.zeros((4, 6))
>>> a
array([[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]])
>>> b = np.ones((2, 6))
>>> b
array([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> a[:b.shape[0], -b.shape[1]:] = b
>>> a
array([[ 0., 0., 0., 1., 1., 1.],
[ 0., 0., 0., 1., 1., 1.],
[ 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0.]])
>>> a = np.zeros((4, 6))
>>> a[:b.shape[0], :b.shape[1]] = b
>>> a
[[ 1. 1. 1. 0. 0. 0.]
[ 1. 1. 1. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0.]]
Explanation:
-
In NumPy you can assign the value of an element of an array through indexing, for example, given an array of 3x3 we can do:
>>> m = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> m
array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
>>> m[1, 2] = 8
>>> m
array([[1, 1, 1],
[1, 1, 8],
[1, 1, 1]])
-
We can also do a slicing of an array or select part of it and assign values using the operator :
:
>>> m = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> m
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> m[0:2]
>>> m
array([[1, 1, 1],
[1, 1, 8],
[1, 1, 1]])
With m[0:2]
we select from row 0 to row 1 (2-1). We can also assign:
>>> m
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> m[0:2] = [[9, 9, 9], [7, 7, 7]]
>>> m
array([[9, 9, 9],
[7, 7, 7],
[7, 8, 9]])
-
We can also select rows and columns:
>>> m
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> m[0:2, 1:3]
array([[2, 3],
[5, 6]])
With m[0:2, 1:3]
we tell you to select from row 0 to 1 and from column 1 to 2.
-
If we leave an empty value before or after :
we indicate that it is selected from the beginning or from the end:
>>> m
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> m[1:]
array([[4, 5, 6],
[7, 8, 9]])
With m[1:]
we indicate that you select from row 1 to the end of the array. With m[:, 1]
we indicate that you select column 1 in full.
-
array.shape
gives us an array with the dimensions of the array. In our case, the first element is the number of rows and the second is that of columns. That is, b.shape[0]
gives us the number of rows and b.shape[1]
is the number of columns.
-
Recall that indexing with negative indexes is allowed, array[-2]
selects the penultimate element of the array.
When we do a[:b.shape[0]] = b
we are indicating that you select the columns of the array from the first to the column number of the array b and assign them the values of the array b.