---
title: Basic NumPy Arrays and Operations
format: live-html
---
This is a **formative test**. It is an occasion to practice the course material. It **does not** contribute to your final grade.
Using the topics covered within the workshops (or otherwise), complete the questions below.
Make sure to use any described variable names exactly and do not change the name of this file. This ensures the nbgrader tool can grade your work correctly.
---
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| setup:
#| exercise:
#| - ex_1a
#| - ex_1b
import numpy as np
```
### Question 1
1A) Create a NumPy array called `numbers` containing the values [1, 3, 5, 7, 9].
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_1a
numbers = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_1a
#| check: true
feedback = None
expected = np.array([1, 3, 5, 7, 9])
if isinstance(numbers, np.ndarray) and np.array_equal(numbers, expected):
feedback = {"correct": True, "message": "Nice work!"}
else:
feedback = {"correct": False, "message": "Make sure you're creating a NumPy array with the correct values."}
feedback
```
::: { .hint exercise="ex\_1a"}
::: { .callout-note collapse="false"}
## Hint
Use `np.array([1, 3, 5, 7, 9])` to create a NumPy array from a list.
:::
:::
::: { .solution exercise="ex\_1a" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
numbers = np.array([1, 3, 5, 7, 9])
```
:::
:::
---
1B) Create a NumPy array called `zeros_array` containing 8 zeros using a NumPy function.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_1b
zeros_array = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_1b
#| check: true
feedback = None
expected = np.zeros(8)
if isinstance(zeros_array, np.ndarray) and np.array_equal(zeros_array, expected):
feedback = {"correct": True, "message": "Well done!"}
else:
feedback = {"correct": False, "message": "Use np.zeros() to create an array of zeros."}
feedback
```
::: { .hint exercise="ex\_1b"}
::: { .callout-note collapse="false"}
## Hint
Use `np.zeros(8)` to create an array with 8 zeros.
:::
:::
::: { .solution exercise="ex\_1b" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
zeros_array = np.zeros(8)
```
:::
:::
---
### Question 2
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| setup:
#| exercise:
#| - ex_2a
#| - ex_2b
data = np.array([10, 20, 30, 40, 50])
```
2A) Access the third element (value 30) from the `data` array and assign it to a variable called `third_element`.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_2a
data = np.array([10, 20, 30, 40, 50])
third_element = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_2a
#| check: true
feedback = None
if third_element == 30:
feedback = {"correct": True, "message": "Correct!"}
else:
feedback = {"correct": False, "message": "Remember arrays are zero-indexed."}
feedback
```
::: { .hint exercise="ex\_2a"}
::: { .callout-note collapse="false"}
## Hint
The third element has index `2`.
:::
:::
::: { .solution exercise="ex\_2a" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
third_element = data[2]
```
:::
:::
---
2B) Create a slice of the `data` array containing the last three elements and assign it to `last_three`.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_2b
last_three = data[______]
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_2b
#| check: true
feedback = None
expected = np.array([30, 40, 50])
if isinstance(last_three, np.ndarray) and np.array_equal(last_three, expected):
feedback = {"correct": True, "message": "Great!"}
else:
feedback = {"correct": False, "message": "Check your slicing syntax."}
feedback
```
::: { .hint exercise="ex\_2b"}
::: { .callout-note collapse="false"}
## Hint
Use negative indexing: `data[-3:]` to get the last three elements.
:::
:::
::: { .solution exercise="ex\_2b" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
last_three = data[-3:]
# Alternative solution:
# last_three = data[2:]
```
:::
:::
---
### Question 3
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| setup:
#| exercise:
#| - ex_3a
#| - ex_3b
values = np.array([2, 4, 6, 8, 10])
```
3A) Multiply all elements in the `values` array by 3 and assign the result to `tripled`.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_3a
values = np.array([2, 4, 6, 8, 10])
tripled = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_3a
#| check: true
feedback = None
expected = np.array([6, 12, 18, 24, 30])
if isinstance(tripled, np.ndarray) and np.array_equal(tripled, expected):
feedback = {"correct": True, "message": "Correct!"}
else:
feedback = {"correct": False, "message": "Use element-wise multiplication."}
feedback
```
::: { .hint exercise="ex\_3a"}
::: { .callout-note collapse="false"}
## Hint
NumPy allows element-wise operations: `values * 3`.
:::
:::
::: { .solution exercise="ex\_3a" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
tripled = values * 3
```
:::
:::
---
3B) Add the arrays `values` and `tripled` together element-wise and assign to `sum_array`.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_3b
sum_array = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_3b
#| check: true
feedback = None
expected = np.array([8, 16, 24, 32, 40])
if isinstance(sum_array, np.ndarray) and np.array_equal(sum_array, expected):
feedback = {"correct": True, "message": "Well done!"}
else:
feedback = {"correct": False, "message": "Add the two arrays element-wise."}
feedback
```
::: { .hint exercise="ex\_3b"}
::: { .callout-note collapse="false"}
## Hint
Use `values + tripled` for element-wise addition.
:::
:::
::: { .solution exercise="ex\_3b" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
sum_array = values + tripled
```
:::
:::
---
### Question 4
4A) Create a NumPy array called `range_array` containing integers from 0 to 9 using `np.arange()`.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| setup:
#| exercise:
#| - ex_4a
#| - ex_4b
import numpy as np
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_4a
range_array = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_4a
#| check: true
feedback = None
expected = np.arange(10)
if isinstance(range_array, np.ndarray) and np.array_equal(range_array, expected):
feedback = {"correct": True, "message": "Good job!"}
else:
feedback = {"correct": False, "message": "Use np.arange() to create a sequence of numbers."}
feedback
```
::: { .hint exercise="ex\_4a"}
::: { .callout-note collapse="false"}
## Hint
Use `np.arange(10)` to create integers from 0 to 9.
:::
:::
::: { .solution exercise="ex\_4a" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
range_array = np.arange(10)
```
:::
:::
---
4B) Calculate the mean (average) of the `range_array` and assign it to a variable called `mean_value`.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_4b
mean_value = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_4b
#| check: true
feedback = None
expected = np.mean(np.arange(10))
if np.isclose(mean_value, expected):
feedback = {"correct": True, "message": "Correct calculation!"}
else:
feedback = {"correct": False, "message": "Use np.mean() or the .mean() method."}
feedback
```
::: { .hint exercise="ex\_4b"}
::: { .callout-note collapse="false"}
## Hint
Use `np.mean(range_array)` or `range_array.mean()`.
:::
:::
::: { .solution exercise="ex\_4b" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
mean_value = np.mean(range_array)
# or
mean_value = range_array.mean()
```
:::
:::
---
### Question 5
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| setup:
#| exercise:
#| - ex_5a
#| - ex_5b
measurements = np.array([1.2, 3.8, 2.1, 4.5, 2.9, 3.3, 1.7])
```
5A) Find all elements in the `measurements` array that are greater than 2.5 and assign them to `filtered_data`.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_5a
measurements = np.array([1.2, 3.8, 2.1, 4.5, 2.9, 3.3, 1.7])
filtered_data = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_5a
#| check: true
feedback = None
expected = measurements[measurements > 2.5]
if isinstance(filtered_data, np.ndarray) and np.array_equal(filtered_data, expected):
feedback = {"correct": True, "message": "Excellent Boolean indexing!"}
else:
feedback = {"correct": False, "message": "Use Boolean indexing: measurements[measurements > 2.5]."}
feedback
```
::: { .hint exercise="ex\_5a"}
::: { .callout-note collapse="false"}
## Hint
Use Boolean indexing: `measurements[measurements > 2.5]`.
:::
:::
::: { .solution exercise="ex\_5a" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
filtered_data = measurements[measurements > 2.5]
```
:::
:::
---
5B) Count how many elements in the `measurements` array are greater than the mean of the array. Assign this count to `count_above_mean`.
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_5b
count_above_mean = ______
```
```{pyodide}
#| caption: "▶ Ctrl/Cmd+Enter | ⇥ Ctrl/Cmd+] | ⇤ Ctrl/Cmd+["
#| exercise: ex_5b
#| check: true
feedback = None
mean_val = np.mean(measurements)
expected = np.sum(measurements > mean_val)
if count_above_mean == expected:
feedback = {"correct": True, "message": "Great work with Boolean operations!"}
else:
feedback = {"correct": False, "message": "Use np.sum() on a Boolean array to count True values."}
feedback
```
::: { .hint exercise="ex\_5b"}
::: { .callout-note collapse="false"}
## Hint
First calculate the mean, then use `np.sum(measurements > mean_value)` to count True values.
:::
:::
::: { .solution exercise="ex\_5b" }
::: { .callout-tip collapse="false"}
## Fully worked solution
```python
mean_val = np.mean(measurements)
count_above_mean = np.sum(measurements > mean_val)
# or in one line:
count_above_mean = np.sum(measurements > np.mean(measurements))
```
:::
:::