Q)Create an array with variable name a and the following contents (shape (3, 4)):
2 7 12 0
3 9 3 4
4 0 1 3
>>> import numpy as np
>>> a = np.array([[2, 7, 12, 0], [3, 9, 3, 4], [4, 0, 1, 3]])
>>> a
array([[ 2, 7, 12, 0],
[ 3, 9, 3, 4],
[ 4, 0, 1, 3]])
Q) What is the array shape?
>>> a.shape
(3, 4)
Q) What is the array ndim?
>>> a.ndim
Q) How about the len of the array?
>>> len(a)
Q) Can you get the ndim and len from the shape?
>>> len(a.shape) == a.ndim
True
>>> a.shape[0] == len(a)
True
1. Create a 1D array from 2 through 5 inclusive.
>>> np.arange(2, 6)
array([2, 3, 4, 5])
2. Make an array with 10 equally spaced elements between 2 and 5 inclusive.
>>> np.linspace(2, 5, 10)
array([ 2. , 2.333333, 2.666667, 3. , 3.333333, 3.666667,
4. , 4.333333, 4.666667, 5. ])
3. Make an all-ones array shape (4, 4).
>>> np.ones((4, 4))
array([[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.],
[ 1., 1., 1., 1.]])
4. Make an identity array shape (6, 6).
>>> np.eye(6)
array([[ 1., 0., 0., 0., 0., 0.],
[ 0., 1., 0., 0., 0., 0.],
[ 0., 0., 1., 0., 0., 0.],
[ 0., 0., 0., 1., 0., 0.],
[ 0., 0., 0., 0., 1., 0.],
[ 0., 0., 0., 0., 0., 1.]])
5. Make this array with a single Python / numpy command:
1 0 0
0 2 0
0 0 3
>>> np.diag([1, 2, 3])
array([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
6. Make a shape (3, 5) array with random numbers from a standard normal
distribution (a normal distribution with mean 0 and variance 1).
>>> rand_arr = np.random.rand(3, 5)
>>> rand_arr.shape
(3, 5)
7. Make an array x with 100 evenly spaced values between 0 and 2 * pi;
>>> x = np.linspace(0, 2 * np.pi, 100)
>>> x.shape
(100,)
8. Make an array y which contains the cosine of the corresponding value in x -
so y[i] = cos(x[i]) (hint: np.lookfor('cosine')).
>>> y = np.cos(x)
>>> y.shape
(100,)
Plot x against y;
>>> import matplotlib.pyplot as plt
>>> plt.plot(x, y)
[...]
9. Make a 10 by 20 array of mean 0 variance 1 normal random numbers;
>>> rand_arr = np.random.randn(10, 20)
>>> rand_arr.shape
(10, 20)
10.Display this array as an image;
>>> plt.imshow(rand_arr)
<...matplotlib.image.AxesImage object at ...>
11. Investigate plt.cm. See if you can work out how to make the displayed
image be grayscale instead of color.
>>> #- Grayscale image of array
>>> plt.imshow(rand_arr, cmap=plt.cm.gray)
<...matplotlib.image.AxesImage object at ...>
12. Create the following array, call this a (you did this before):
2 7 12 0
3 9 3 4
4 0 1 3
>>> a = np.array([[2, 7, 12, 0], [3, 9, 3, 4], [4, 0, 1, 3]])
Get the 2nd row of a ([ 3 9 3 4]);
>>> a[1]
array([3, 9, 3, 4])
Get the 3rd column of a ([12 3 1]);
>>> a[:, 2]
array([12, 3, 1])
13. Create the following arrays (with correct data types):
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 2],
[1, 6, 1, 1]]
>>> arr1 = np.ones((4, 4), dtype=np.int64) # Would be float by default
>>> arr1[3, 1] = 6
>>> arr1[2, 3] = 2
>>> arr1
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 2],
[1, 6, 1, 1]])
14. Create the following matrix
[[0., 0., 0., 0., 0.],
[2., 0., 0., 0., 0.],
[0., 3., 0., 0., 0.],
[0., 0., 4., 0., 0.],
[0., 0., 0., 5., 0.],
[0., 0., 0., 0., 6.]]
>>> arr2 = np.diag([2., 3., 4, 5, 6], -1) # Need a float input to
diag for float output
>>> arr2[:, :-1]
array([[ 0., 0., 0., 0., 0.],
[ 2., 0., 0., 0., 0.],
[ 0., 3., 0., 0., 0.],
[ 0., 0., 4., 0., 0.],
[ 0., 0., 0., 5., 0.],
[ 0., 0., 0., 0., 6.]])
15. Create the following matrix
[[4, 3, 4, 3, 4, 3],
[2, 1, 2, 1, 2, 1],
[4, 3, 4, 3, 4, 3],
[2, 1, 2, 1, 2, 1]]
>>> #- Use np.tile to construct array
>>> np.tile([[4, 3], [2, 1]], (2, 3))
array([[4, 3, 4, 3, 4, 3],
[2, 1, 2, 1, 2, 1],
[4, 3, 4, 3, 4, 3],
[2, 1, 2, 1, 2, 1]])
Fancy indexing using boolean arrays
1. Create the following array a (same as before):
2 7 12 0
3 9 3 4
4 0 1 3
>>> #- Create array a
>>> a = np.array([[2, 7, 12, 0], [3, 9, 3, 4], [4, 0, 1, 3]])
2. Use > to make a mask that is true where the elements are greater than 5, like
this:
>>> mask = a > 5
>>> mask
array([[False, True, True, False],
[False, True, False, False],
[False, False, False, False]], dtype=bool)
3. Return all the elements in a that are greater than 5.
>>> a[mask]
array([ 7, 12, 9])
4. Set all the elements greater than 5 to be equal to 5, to get this:
>>> a[mask] = 5
>>> a
array([[2, 5, 5, 0],
[3, 5, 3, 4],
[4, 0, 1, 3]])
Elementwise operations
2 7 12 0
3 9 3 4
4 0 1 3
1. Use array slicing to get a new array composed of the even columns (0, 2) of a.
Now get array that contains the odd columns (1, 3) of a. Add these two arrays.
>>> #- Add even and odd columns of a
>>> a = np.array([[2, 7, 12, 0], [3, 9, 3, 4], [4, 0, 1, 3]])
>>> even_columns = a[:, ::2]
>>> odd_columns = a[:, 1::2]
>>> even_columns + odd_columns
array([[ 9, 12],
[12, 7],
[ 4, 4]])
2. Generate this array:
[2**0, 2**1, 2**2, 2**3, 2**4]
>>> #- Generate array of powers of 2
>>> 2 ** np.arange(5)
array([ 1, 2, 4, 8, 16])
3. Generate an array length 10 such that this is true of the elements
(where x[i] is the element of x at index i):
x[i] = 2 ** (3 * i) - i
>>> #- Generate array
>>> inds = np.arange(10)
>>> x = 2 ** (3 * inds) - inds
>>> x
array([ 1, 7, 62, 509, 4092, 32763,
262138, 2097145, 16777208, 134217719])
Summary functions
2 7 12 0
3 9 3 4
4 0 1 3
>>> a = np.array([[2, 7, 12, 0], [3, 9, 3, 4], [4, 0, 1, 3]])
What are the:
sum of all the values?
>>> #- Sum of values in a
>>> a.sum()
48
sum of the columns?
>>> #- Sum of the values of the columns in a
>>> a.sum(axis=0) # Sum over the first axis, leaving the second
array([ 9, 16, 16, 7])
sum of the rows?
>>> #- Sum of the values of the rows in a
>>> a.sum(axis=1) # Sum over the second axis, leaving the first
array([21, 19, 8])
mean?
>>> #- Mean of all the values in a
>>> a.mean()
4.0
min?
>>> #- Minimum of all the values in a
>>> a.min()
max?
>>> #- Maximum of all the values in a
>>> a.max()