- 1. Import numpy as np and see the version
- 2. How to create a 1D array?
- 3. How to create a boolean array?
- 4. How to extract items that satisfy a given condition from 1D array?
- 6. How to replace items that satisfy a condition without affecting the original array?
- 7. How to reshape an array?
- 8. How to stack two arrays vertically?
- 9. How to stack two arrays horizontally?
- 10. How to generate custom sequences in numpy without hardcoding?
Numpy Exercises 1-10
- 1. Import numpy as np and see the version
- 2. How to create a 1D array?
- 3. How to create a boolean array?
- 4. How to extract items that satisfy a given condition from 1D array?
- 6. How to replace items that satisfy a condition without affecting the original array?
- 7. How to reshape an array?
- 8. How to stack two arrays vertically?
- 9. How to stack two arrays horizontally?
- 10. How to generate custom sequences in numpy without hardcoding?
1. Import numpy as np and see the version
import numpy as npnp.__version__
'1.21.5'
2. How to create a 1D array?
(With numbers 0-9)
np.arange(0, 10)
If you need non-integer steps use np.linspace instead!
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
3. How to create a boolean array?
np.full((3,3), True)
array([[ True, True, True],
[ True, True, True],
[ True, True, True]])
4. How to extract items that satisfy a given condition from 1D array?
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])arr[arr % 2 == 1]
Pretty cool, Numpy broadcasts operations to every element. arr % 2 == 1
returns the mask we need to extract the odds.
Helpful discussion on filtering here
6. How to replace items that satisfy a condition without affecting the original array?
np.where(arr % 2 == 1, -1, arr)
If you said just do a list comprehension... Me too. np.where
has some damned useful features though! It's definitely visually cleaner too.
See the np.where documentation. It has a list comprehension equivalent too, which is pretty cool!
7. How to reshape an array?
Convert a 1D array to a 2D array.
np.reshape(arr, (2, -1))
Using -1
as the second dimension lets numpy figure out how many columns there should be. Either dimension can be set to -1
.
See the np.reshape documentation
8. How to stack two arrays vertically?
Given arrays a
and b
:
a = np.arange(10).reshape(2,-1)b = np.repeat(1, 10).reshape(2,-1)
We stack them by using vstack:
np.vstack((a, b))
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
See the np.vstack documentation.
9. How to stack two arrays horizontally?
Given the same a
and b
from problem 8:
np.hstack((a,b))
array([[0, 1, 2, 3, 4, 1, 1, 1, 1, 1],
[5, 6, 7, 8, 9, 1, 1, 1, 1, 1]])
See the np.hstack documentation
10. How to generate custom sequences in numpy without hardcoding?
Given a
: a = np.array([1,2,3])
Make: #> array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
np.hstack((np.repeat(a, 3), np.tile(a, 3)))
Repeat repeats the elements of an array a given number of times. Tile repeats the array a given number of times.
See the np.repeat documentation and the np.tile documentation!