Exercise Answers¶
import numpy as np
Some problems. Put your answers inside the print functions¶
a = np.arange(4).reshape((2,2))
print("array a:")
print(a)
print("\nMaximum value of a:")
print(a.max()) # put your answer inside the print
print("\nMinimum value of a:")
print(a.min()) # put your answer inside the print
print("\nReturn an array of the max in each column of a:")
print(a.max(axis=0))
print("\nReturn an array of the min in each row of a:")
print(a.max(axis=1))
print("\nReturn a, sorted within each row:")
a.sort()
print(a)
print("\nReturn a, sorted within each column:")
a.sort(axis=1)
print(a)
array a:
[[0 1]
[2 3]]
Maximum value of a:
3
Minimum value of a:
0
Return an array of the max in each column of a:
[2 3]
Return an array of the min in each row of a:
[1 3]
Return a, sorted within each row:
[[0 1]
[2 3]]
Return a, sorted within each column:
[[0 1]
[2 3]]
b = np.arange(40).reshape((20,2))
print("array b:")
print(b)
print("\nPrint elements of the first column of b above that column's 80th percentile:")
p80 = np.percentile(b[:,0],80)
print(b[ b[:,0]>p80 , 0])
#b[:,0]>p80 finds the rows of b where the first columns value is above p80
#b[ that, 0] prints the firms column
print("\nCovariance matrix of the columns of b:")
print(np.cov(b.T))
array b:
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]
[12 13]
[14 15]
[16 17]
[18 19]
[20 21]
[22 23]
[24 25]
[26 27]
[28 29]
[30 31]
[32 33]
[34 35]
[36 37]
[38 39]]
Print elements of the first column of b above that column's 80th percentile:
[32 34 36 38]
Covariance matrix of the columns of b:
[[140. 140.]
[140. 140.]]