What is the shape of the following array?
```python
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
```
Analysis & Theory
The array has 2 rows and 3 columns, so its shape is `(2, 3)`.
What will be the result of `arr.reshape(3, 2)` for the following array?
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
print(arr.reshape(3, 2))
```
A
A) [[1, 2, 3], [4, 5, 6]]
B
B) [[1, 2], [3, 4], [5, 6]]
C
C) [[1, 2], [3, 4], [5, 6]]
D
D) [[1, 2], [3, 4], [5, 6]]
Analysis & Theory
Using `reshape(3, 2)` reshapes the array into 3 rows and 2 columns, so the result will be `[[1, 2], [3, 4], [5, 6]]`.
What is the default shape of a 1D NumPy array created from a list of length 4?
```python
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr.shape)
```
Analysis & Theory
A 1D array created from a list of length 4 will have the shape `(4,)`.
What will the result of `arr.shape` be for the following array?
```python
import numpy as np
arr = np.array([[[1], [2]], [[3], [4]]])
print(arr.shape)
```
Analysis & Theory
The array has 2 blocks, 2 rows in each block, and 1 element in each row, so its shape is `(2, 2, 1)`.
What does the `-1` argument in the `reshape()` method do?
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
arr_reshaped = arr.reshape(-1, 2)
print(arr_reshaped.shape)
```
A
A) It automatically calculates the number of rows.
B
B) It automatically calculates the number of columns.
D
D) It sets the number of columns to `-1`.
Analysis & Theory
When `-1` is passed as an argument in `reshape()`, NumPy automatically calculates the number of rows or columns to make the reshaping valid. In this case, it calculates 3 rows and 2 columns, resulting in the shape `(3, 2)`.
What is the shape of a 3D array created by the following code?
```python
import numpy as np
arr = np.zeros((3, 4, 5))
print(arr.shape)
```
Analysis & Theory
`np.zeros((3, 4, 5))` creates a 3D array with 3 blocks, 4 rows per block, and 5 columns per row, so the shape is `(3, 4, 5)`.
What will be the shape of the following array after this operation?
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
arr = arr.reshape(2, 3)
arr = arr.T
print(arr.shape)
```
Analysis & Theory
After reshaping the array to `(2, 3)` and transposing it using `.T`, the shape becomes `(3, 2)`.
What is the result of `arr.shape` for the following code?
```python
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
print(arr.shape)
```
Analysis & Theory
The array has 3 rows and 2 columns, so the shape is `(3, 2)`.
What will `arr.shape` return for a 2D array with shape `(4, 5)` after applying `arr.flatten()`?
```python
import numpy as np
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]])
arr_flat = arr.flatten()
print(arr_flat.shape)
```
Analysis & Theory
`flatten()` converts a multi-dimensional array into a 1D array. The flattened array will have a shape of `(20,)` since the original array has 20 elements.
What is the effect of using `arr.shape = (2, 3)` directly on an existing NumPy array?
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
arr.shape = (2, 3)
print(arr.shape)
```
A
A) It raises an error if the new shape is incompatible with the total number of elements.
B
B) It changes the shape of the array if the new shape matches the total number of elements.
C
C) It creates a new array with the specified shape.
D
D) It does not affect the original array.
Analysis & Theory
You can directly modify the shape of an array if the new shape is compatible with the total number of elements. In this case, the total number of elements is 6, and the shape `(2, 3)` is compatible.