Numpy Flashcards

1
Q

create a regular array

A

a=[1,2,3,4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

create a ndarray, or a np array

A

B=np.array([1,2,3,4,5,6,55,5])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

create a numpy array that is 2x5 matrix

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

find type

A

A.dtype

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

returns a np array starting from 0 to 10, but not including 10. The increment is 2 each time.

A

np.arange(0,10,2)

array([0, 2, 4, 6, 8])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

show the shape of a array

A
A.shape
(2, 5)
B=np.array([1, 2, 3, 4, 5])
B.shape
(5,)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do we make B an 5 by 1 matrix?

A
#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]]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

reshape to a 3x3 matrix.

A
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]])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

create 4x3 matrix of zeros

A
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.])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

create 2x2 1’s

A

np.ones((2,2))
array([[1., 1.],
[1., 1.]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

eye stands for Identity and sysmetric. This function retruns to an edentity matrix with specific dimentions.

A

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.]])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

generate another (3 x 3) matrix to be multiplied.

A
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]])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

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]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

create float array

A

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.]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

create int array

A

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]])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

cast array to float

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

float to int

A

2
myFloat = -10.8;
print(int(myFloat));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

create numpy array 1 thru 9

A

a=np.arange(10) # create a one-dimentional array.

19
Q

return to a np array from first to third elements, or elements with index 0-2.

A

a[0:3]

array([0, 1, 2])

20
Q

insert value 10 in front of 50 on a list.

then print sum

A

List = [True, 50, 10]
List.insert(2, 10)
print(List, “Sum is: “, sum(List))

21
Q

how do you tell if it’s a float, int, string

A

type(myFloat)

22
Q

pull value from list 7th position

23
Q

retrieve first 9 values in array

A

array1[0:9] . it would be position 0 thru 8

24
Q

insert value 10 for first 5 positions in array

A

arrayB[0:4]= 10

25
make a new array with slice of another array
arrayc=arrayb[0:5]
26
make 3x3 matrix with values 1-9
arrayd=np.array([[1,2,3],[4,5,6],[7,8,9]]) | dont forget the double braces
27
what is output of A[:2] assuming 1-9 3x3
rows 0 and 1, all colums array([[1, 2], [4, 5], [7, 8]])
28
A[:2,1:]
row 0 and 1, columes 1 to end. array([[2, 3], [5, 6]])
29
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]])
30
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]]])
31
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]]])
32
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. ])
33
generate array of 8 with random generated numbers.
a=np.random.randn(8) | a
34
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]]) ```
35
show maxium of each element comparing 2 arrays
np.maximum(x,y) array([[ 2, 4, 6], [ 8, 10, 12], [14, 16, 18]])
36
2 4x4 random arrays normal distribution
x=np.random.randn(4,4) | y=np.random.randn(4,4)
37
compare 2 arrays where and show highest in new array
newarray=np.where(x>y,x,y)
38
create random 5x3 and replace neg numbers with 1
a=np.random.randn(5,3) | b=np.where(x>0,x,1)
39
#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.
40
add all the array elements
b=np.array([[1,3],[2,4]]) b b.sum()
41
average of all the elements of array
a.mean()
42
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]
43
create regular array and create numpy array
a=[1,2,3,4] | b=np.array([1,2,3,4])