Practice and reinforce the concepts from Lesson 10
In this activity, you'll practice:
Estimated Time: 45-60 minutes
import numpy as np
:bulb: Tip :bulb: Pro tip: Always import NumPy at the beginning of your notebook to avoid errors later!
Time: 10 minutes
Write the program using variables named array1
and print()
to show the outcome for each step.
Create a NumPy array from 11 to 20 using array()
in array1
. Print array1
as shown below.
:bulb: Tip :bulb: Remember to use a list inside
np.array()
like this:np.array([11, 12, 13, ...])
Expected output:
[11 12 13 14 15 16 17 18 19 20]
Access number 18 in array1
.
Expected output:
18
Access numbers from 13 to 17 in array1
.
:bulb: Tip :bulb: Remember array indexing starts at 0! Count carefully to find the right indices.
Expected output:
[13 14 15 16 17]
Filter the data to show only values greater than 15 in array1
.
Expected output:
[16 17 18 19 20]
Multiply array1
with 10.
Expected output:
[110 120 130 140 150 160 170 180 190 200]
Time: 10 minutes
Write the program using variables named array2
and print()
to show the outcome for each step.
Create a NumPy array from 0 to 9 using arange()
in array2
. Print array2
as shown below.
:bulb: Tip :bulb: The
arange()
function is like Python'srange()
but creates a NumPy array!
Expected output:
[0 1 2 3 4 5 6 7 8 9]
Access number 9 in array2
.
Expected output:
9
Access numbers from 5 to 8 in array2
.
Expected output:
[5 6 7 8]
Subtract array2
away from array1
.
:bulb: Tip :bulb: NumPy arrays must have the same shape for element-wise operations!
Expected output:
[11 11 11 11 11 11 11 11 11 11]
Time: 10 minutes
Write the program using the built-in function random
from package numpy and print()
to show the outcome for each step.
From the package numpy, import the built-in function random:
from numpy import random
Generate a random real number from 0 to 1.
:information_source: Info :pushpin: Note: Your answer will be different because it's random! That's expected.
Expected output:
0.9973521728863931
Generate a random integer from 0 to 20.
:bulb: Tip :bulb: Use
random.randint()
for generating random integers!
Expected output:
15
Time: 15 minutes
Write the program using a variable named array2D
and print()
to show the outcome for each step.
Create a 3x3 2D NumPy array with all ones using ones()
in array2D
.
:bulb: Tip :bulb: Use
np.ones((3, 3))
- notice the double parentheses for the shape!
Expected output:
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
Check the array properties: dtype
, ndim
, shape
, and size
:information_source: Info :pushpin: These properties tell you:
dtype
: Data type of elementsndim
: Number of dimensionsshape
: Size of each dimensionsize
: Total number of elements
Expected output:
float64
2
(3, 3)
9
Try to make the output as follows by changing the number in the center of array2D
to 2.
:bulb: Tip :bulb: For a 3x3 array, the center element is at position [1, 1]!
Expected output:
[[1. 1. 1.]
[1. 2. 1.]
[1. 1. 1.]]
:warning: Warning Important: Before submitting, make sure you have:
- :white_check_mark: Completed all 4 questions
- :white_check_mark: Tested your code and verified the outputs match the expected results
- :white_check_mark: Saved your Colab notebook
- :white_check_mark: Made note of any questions or challenges you encountered
:link: Submit your exercise here
Common Issues:
:bulb: Tip :speech_balloon: Need help? Ask your instructor or discuss with your classmates in the forum!