Correct option is B
The code creates a
NumPy array A with the following elements:
A = np.array([[2, 3], [10, 20]])
Then, the statement A + 1 adds 1 to each element of the array. This operation is
element-wise, meaning that 1 is added to each individual element of the array:
[[2, 3], -> [[2+1, 3+1], = [[3, 4],
[10, 20]] [10+1, 20+1]] [11, 21]]
So the output will be:
[[3 4]
[11 21]]
Important Key Points:
1.
Element-wise Operation: In NumPy, adding a scalar to an array performs the operation on each element of the array individually.
2.
Array Shape Preservation: The shape of the array is preserved. It remains a 2x2 matrix after the addition.
3.
Broadcasting: NumPy automatically broadcasts the scalar value 1 to each element of the array.
Knowledge Booster:
·
Option (a): This is incorrect because 1 is added to every element of the array, so the correct result should be [[3, 4], [11, 21]], not [[3, 4], [10, 20]].
·
Option (c): This is incorrect because the first row should become [3, 4], not [2, 3]. The scalar 1 is added to all elements, not just the second row.
·
Option (d): This is incorrect because the first row should become [3, 4], not [3, 3]. Each element in the array is incremented by 1, so the second element in the first row will become 4.