Numpy Basic Flashcards
How do you import NumPy under the common alias np
and create a 1D array from the Python list [1, 2, 3]
?
```python
import numpy as np
arr = np.array([1, 2, 3])
~~~
How do you check the shape of a NumPy array arr = np.array([[1, 2], [3, 4]])
?
```python
arr.shape # Returns (2, 2)
~~~
Given arr = np.array([1, 2, 3, 4])
, how do you reshape it into a 2x2 matrix?
```python
matrix = arr.reshape((2, 2))
~~~
How do you create a NumPy array of 5 evenly spaced numbers between 0 and 1 (inclusive)?
```python
arr = np.linspace(0, 1, 5)
# [0. 0.25 0.5 0.75 1. ]
~~~
How do you create a NumPy array of numbers from 0 (inclusive) to 10 (exclusive) in steps of 2?
```python
arr = np.arange(0, 10, 2)
# [0 2 4 6 8]
~~~
How do you create a 3x3 NumPy array of all zeros?
```python
zeros_3x3 = np.zeros((3, 3))
~~~
How do you create a 2x2 NumPy array of all ones with a float data type?
```python
ones_2x2 = np.ones((2, 2), dtype=float)
~~~
How do you create a 3x3 NumPy identity matrix (1s on the diagonal, 0s elsewhere)?
```python
eye_3x3 = np.eye(3)
~~~
How do you select the element at row 1, column 2 of arr = np.array([[10, 20, 30], [40, 50, 60]])
?
```python
element = arr[1, 2] # 60
~~~
How do you slice out the first two columns from arr = np.array([[1, 2, 3], [4, 5, 6]])
, resulting in [[1, 2], [4, 5]]
?
```python
slice_2_columns = arr[:, :2]
~~~
How do you slice out the first row only from arr = np.array([[1, 2, 3], [4, 5, 6]])
?
```python
first_row = arr[0, :]
~~~
How do you slice out just the second row, first two columns from arr = np.array([[1, 2, 3], [4, 5, 6]])
, resulting in [4, 5]
?
```python
slice_part = arr[1, :2]
~~~
How do you change the shape of arr = np.array([1, 2, 3, 4, 5, 6])
into a 2D array with 2 rows and 3 columns, without creating a copy?
```python
arr.shape = (2, 3)
~~~
How do you create a random NumPy array of shape (2, 2) using np.random.rand()
with values between 0 and 1?
```python
arr = np.random.rand(2, 2)
~~~
How do you create a random NumPy array of integers between 0 and 9 (inclusive) of shape (3, 3) using np.random.randint()
?
```python
arr = np.random.randint(0, 10, (3, 3))
~~~
How do you get the number of dimensions of arr = np.array([[1, 2], [3, 4]])
?
```python
arr.ndim # Returns 2
~~~
How do you compute the sum of all elements in a 2D array arr = np.array([[1, 2], [3, 4]])
using a single NumPy method?
```python
total = arr.sum() # 10
~~~
How do you compute the mean of all elements in the array arr = np.array([1, 2, 3, 4])
?
```python
mean_val = arr.mean() # 2.5
~~~
How do you compute the standard deviation of the elements in arr = np.array([1, 2, 3, 4])
?
```python
std_val = arr.std() # 1.1180…
~~~
Given arr = np.array([[1, 2], [3, 4]])
, how do you compute the sum along the rows (axis=1), resulting in [3, 7]
?
```python
row_sums = arr.sum(axis=1)
~~~
How do you flatten the 2D array arr = np.array([[1, 2], [3, 4]])
into a 1D array without creating a copy (if possible)?
```python
flattened_view = arr.ravel()
~~~
How do you create a copy of arr = np.array([1, 2, 3])
named arr_copy
so that changes to arr_copy
won’t affect arr
?
```python
arr_copy = arr.copy()
~~~
How do you replace all elements in arr = np.array([1, 2, 3, 4])
that are less than 3 with the value 0 (in-place)?
```python
arr[arr < 3] = 0
~~~
How do you extract all elements greater than 2 from arr = np.array([1, 2, 3, 4, 5])
as a new array?
```python
filtered = arr[arr > 2]
# [3, 4, 5]
~~~