NumPy Cheat Sheet Flashcards

1
Q

A NumPy Array object shorthand

A

arr

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

Import NumPy

A

import numpy as np

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

Import from a text file

A

np.loadtxt(‘file.txt’)

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

Import from a CSV file

A

np.genfromtxt(‘file.csv’,delimiter=‘,’)

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

Writes to a text file

A

np.savetxt(‘file.txt’,arr,delimiter=‘ ‘)

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

Writes to a CSV file

A

np.savetext(‘file.csv’,arr,delimiter=‘,’)

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

Create one dimensional array

A

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

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

Create two dimensional array

A

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

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

Create 1D array of length 3 all values 0

A

np.zeros(3)

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

3 x 4 array with all values 1

A

np.ones((3,4)

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

5 x 5 array of 0 with 1 on diagonal (identity matrix)

A

np.eye(5)

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

Array of 6 evenly divided values from 0 to 100

A

np.linspace(0,100,6)

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

Array of values from 0 to less than 10 with step 3

A

np.arange(0,10,3)

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

2 x 3 array with all values 8

A

np.full((2,3),8)

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

4 x 5 array of random floats between 0-1

A

np.random.rand(4,5)

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

6 x 7 array of random floats between 0-100

A

np.random.rand(6,7)*100

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

2 x 3 array with random ints between 0-4

A

np.random.randint(5,size=2,3))

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

Returns number of elements in arr

A

arr.size

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

Returns dimensions of arr

A

arr.shape

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

Returns the type of elements in arr

A

arr.dtype

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

Convert arr elements to type dtype

A

arr.astype(dtype)

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

Convert arr to a Python list

A

arr.tolist()

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

View documentation for np.eye

A

np.info(np.eye)

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

Copies arr to new memory

A

np.copy(arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Creates view of arr elements with type dtype
arr.view(dtype)
26
Sorts arr
arr.sort()
27
Sorts specific axis of arr
arr.sort(axis=0)
28
Flattens 2D array two_d_arr to 1D
two_d_arr.flatten()
29
Transposes arr (rows become columns and vice versa)
arr.T
30
Reshapes arr to 3 rows, 4 columns without changing data
arr.reshape(3,4)
31
Changes arr shape to 5 x 6 and fills new values with 0
arr.resize((5,6))
32
Appends values to the end of arr
np.append(arr,values)
33
Inserts values into arr before index 2
np.insert(arr,2,values)
34
Deletes rows on index 3 of arr
np.delete(arr,3,axis=0)
35
Deletes column on index 4 of arr
np.delete(arr,4,axis=1)
36
Adds arr2 as rows to the end of arr1
np.concatenate((arr1,arr2),axis=0)
37
Adds arr2 as columns to end of arr1
np.concatenate((arr1,arr2),axis=1)
38
Splits arr into 3 sub-arrays
np.split(arr,3)
39
Splits arr horizontally on the 5th index
np.hsplit(arr,5)
40
Returns the element at index 5
arr[5]
41
Returns the 2D array element on index [2] [5]
arr[2,5]
42
Assigns array element on index 1 the value 4
arr[1]=4
43
Assigns array element on index [1] [3] the value 10
arr[1,3]=10
44
Returns the elements at indices 0, 1, 2 (for 2D array, returns rows 0, 1, 2)
arr[0:3]
45
Returns the elements on rows 0, 1, 2 at column 4
arr[0:3,4]
46
Returns the elements at indices 0, 1 (on a 2D array: returns rows 0,1)
arr[:2]
47
Returns the elements at index 1 on all rows
arr[:,1]
48
Returns an array with boolean values
arr<5 OR (arr1<3) & (arr2>5)
49
Inverts a boolean array
~arr
50
Returns array elements smaller than 5
arr[arr<5]
51
Add 1 to each array element
np.add(arr,1)
52
Subtract 2 from each array element
np.subtract(arr,2)
53
Multiply each array element by 3
np.mulitply(arr,3)
54
Divide each array element by 4
np.divide(arr,4)
55
Raise each array element to the 5th power
np.power(arr,5)
56
Elementwise add arr2 to arr1
np.add(arr1,arr2)
57
Elementwise subtract arr2 from arr1
np.subtract(arr1,arr2)
58
Elementwise multiply arr1 by arr2
np.multiply(arr1,arr2)
59
Elementwise divide arr1 by arr2
np.divide(arr1,arr2)
60
Elementwise raise arr1 raised to the power of arr2
np.power(arr1,arr2)
61
Returns true if the arrays have the same elements and shape
np.array_equal(arr1,arr2)
62
Square root of each element in the array
np.sqrt(arr)
63
Sine of each element in the array
np.sin(arr)
64
Natural log of each element in the array
np.log(arr)
65
Absolute value of each element in the array
np.abs(arr)
66
Rounds up to the nearest int
np.ceil(arr)
67
Rounds down to the nearest int
np.floor(arr)
68
Rounds to the nearest int
np.round(arr)
69
Returns mean along specific axis
np.mean(arr,axis=0)
70
Returns sum of arr
arr.sum()
71
Returns minimum value of arr
arr.min()
72
Returns maximum value of specific axis
arr.max(axis=0)
73
Returns the variance of array
np.var(arr)
74
Returns the standard deviation of specific axis
np.std(arr,axis=1)
75
Returns correlation coefficient of array
arr.corrcoef()