Numpy Flashcards
create a regular array
a=[1,2,3,4]
create a ndarray, or a np array
B=np.array([1,2,3,4,5,6,55,5])
create a numpy array that is 2x5 matrix
a=[[1,2,3,4,5],[6,7,8,9,10]]
A=np.array(a)# create a np array. A is also a 2 by 5 matrix
A
find type
A.dtype
returns a np array starting from 0 to 10, but not including 10. The increment is 2 each time.
np.arange(0,10,2)
array([0, 2, 4, 6, 8])
show the shape of a array
A.shape (2, 5) B=np.array([1, 2, 3, 4, 5]) B.shape (5,)
How do we make B an 5 by 1 matrix?
#reshape function returns to a multi-dim matrix. Bis is 5 element in 1 dimension. Notice the double brackets! B=np.array([1, 2, 3, 4, 5]) print(B.reshape(1,5)) [[1 2 3 4 5]]
reshape to a 3x3 matrix.
only applys to ndarray C =np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) C.reshape(3,3) array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
create 4x3 matrix of zeros
np.zeros((4,3)) array([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) np.zeros(8) array([0., 0., 0., 0., 0., 0., 0., 0.])
create 2x2 1’s
np.ones((2,2))
array([[1., 1.],
[1., 1.]])
eye stands for Identity and sysmetric. This function retruns to an edentity matrix with specific dimentions.
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.]])
generate another (3 x 3) matrix to be multiplied.
D = np.arange(1,10).reshape(3,3) # no stepsize is specified means stepsize is 1. D array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
Please using numpy random to create one 2by4 random uniform array, named A and one 4by2 random uniform array, named B.
Then get the dot product of A and B.
Get the dot product of B and A.
A=np.random.rand(2,4)
B=np.random.rand(4,2)
print(np.dot(A,B))
print(np.dot(B,A))
[[0.41745136 0.36938202]
[0.869284 0.79135864]]
[[0.12728448 0.44244018 0.39016312 0.70958712]
[0.18397938 0.68209005 0.53437116 1.14459566]
[0.12598516 0.45241722 0.37611223 0.74283167]
[0.00561522 0.01666167 0.01919675 0.02332323]]
create float array
A=np.array([[1,2,3,4,5],[6,7,8,9,10]],dtype=np.float64)
A
array([[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9., 10.]])
create int array
B=np.array([[1,2,3,4,5],[6,7,8,9,10]],dtype=np.int64)
B
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
cast array to float
A=np.array([[1,2,3,4,5],[6,7,8,9,10]]) # by defining this, type of A will be int64.
B=A.astype(np.float64)
B
float to int
2
myFloat = -10.8;
print(int(myFloat));
create numpy array 1 thru 9
a=np.arange(10) # create a one-dimentional array.
return to a np array from first to third elements, or elements with index 0-2.
a[0:3]
array([0, 1, 2])
insert value 10 in front of 50 on a list.
then print sum
List = [True, 50, 10]
List.insert(2, 10)
print(List, “Sum is: “, sum(List))
how do you tell if it’s a float, int, string
type(myFloat)
pull value from list 7th position
a[6]
retrieve first 9 values in array
array1[0:9] . it would be position 0 thru 8
insert value 10 for first 5 positions in array
arrayB[0:4]= 10
make a new array with slice of another array
arrayc=arrayb[0:5]
make 3x3 matrix with values 1-9
arrayd=np.array([[1,2,3],[4,5,6],[7,8,9]])
dont forget the double braces
what is output of A[:2] assuming 1-9 3x3
rows 0 and 1, all colums
array([[1, 2],
[4, 5],
[7, 8]])
A[:2,1:]
row 0 and 1, columes 1 to end.
array([[2, 3],
[5, 6]])
create random numpy array 2 rows 4 columns
arr=np.random.randn(2,4)
array([[-1.00636069, 0.72807051, 0.49342933, 1.15461629],
[ 1.49388338, 0.36216412, 0.66747094, -1.51994783]])
create a np array with three axes, axis 0 size is 2, axis 1 size is 3, axis 2 size is 4.
arr=np.arange(24).reshape(2,3,4)
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], arr=np.arange(36).reshape(3,3,4) array([[[ 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]]])
swap axis
arr.swapaxes(1,2)
array([[[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]],
[[12, 16, 20], [13, 17, 21], [14, 18, 22], [15, 19, 23]]])
create array using a range of numbers
then convert to square root of the values
arr=np.arange(10)
np.sqrt(arr)
arr([1,2,3,4,5,6,7,8])
array([0. , 1. , 1.41421356, 1.73205081, 2. ,
2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])
generate array of 8 with random generated numbers.
a=np.random.randn(8)
a
add 2 arrays together
np.add(x,y)
or g=np.add(x,y)
x=np.array([[1,2,3],[4,5,6],[7,8,9]]) #x y=np.array([[1,2,3],[4,5,6],[7,8,9]]) array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]])
show maxium of each element comparing 2 arrays
np.maximum(x,y)
array([[ 2, 4, 6],
[ 8, 10, 12],
[14, 16, 18]])
2 4x4 random arrays normal distribution
x=np.random.randn(4,4)
y=np.random.randn(4,4)
compare 2 arrays where and show highest in new array
newarray=np.where(x>y,x,y)
create random 5x3 and replace neg numbers with 1
a=np.random.randn(5,3)
b=np.where(x>0,x,1)
compute the mean over columns. its the same as a.mean(axis=1)
a.mean(1)
column 0
row 1
axis = 0 means along the column and axis = 1 means working along the row.
add all the array elements
b=np.array([[1,3],[2,4]])
b
b.sum()
average of all the elements of array
a.mean()
Exercise
Please generate a random numpy array of size 200 from standard normal distribution. Then please find the 5% quantile
n=200
a=np.random.randn(n)
a.sort()
a[int(n*0.05)-1]
create regular array and create numpy array
a=[1,2,3,4]
b=np.array([1,2,3,4])