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
Q

Creates view of arr elements with type dtype

A

arr.view(dtype)

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

Sorts arr

A

arr.sort()

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

Sorts specific axis of arr

A

arr.sort(axis=0)

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

Flattens 2D array two_d_arr to 1D

A

two_d_arr.flatten()

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

Transposes arr (rows become columns and vice versa)

A

arr.T

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

Reshapes arr to 3 rows, 4 columns without changing data

A

arr.reshape(3,4)

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

Changes arr shape to 5 x 6 and fills new values with 0

A

arr.resize((5,6))

32
Q

Appends values to the end of arr

A

np.append(arr,values)

33
Q

Inserts values into arr before index 2

A

np.insert(arr,2,values)

34
Q

Deletes rows on index 3 of arr

A

np.delete(arr,3,axis=0)

35
Q

Deletes column on index 4 of arr

A

np.delete(arr,4,axis=1)

36
Q

Adds arr2 as rows to the end of arr1

A

np.concatenate((arr1,arr2),axis=0)

37
Q

Adds arr2 as columns to end of arr1

A

np.concatenate((arr1,arr2),axis=1)

38
Q

Splits arr into 3 sub-arrays

A

np.split(arr,3)

39
Q

Splits arr horizontally on the 5th index

A

np.hsplit(arr,5)

40
Q

Returns the element at index 5

A

arr[5]

41
Q

Returns the 2D array element on index [2] [5]

A

arr[2,5]

42
Q

Assigns array element on index 1 the value 4

A

arr[1]=4

43
Q

Assigns array element on index [1] [3] the value 10

A

arr[1,3]=10

44
Q

Returns the elements at indices 0, 1, 2 (for 2D array, returns rows 0, 1, 2)

A

arr[0:3]

45
Q

Returns the elements on rows 0, 1, 2 at column 4

A

arr[0:3,4]

46
Q

Returns the elements at indices 0, 1 (on a 2D array: returns rows 0,1)

A

arr[:2]

47
Q

Returns the elements at index 1 on all rows

A

arr[:,1]

48
Q

Returns an array with boolean values

A

arr<5 OR (arr1<3) & (arr2>5)

49
Q

Inverts a boolean array

A

~arr

50
Q

Returns array elements smaller than 5

A

arr[arr<5]

51
Q

Add 1 to each array element

A

np.add(arr,1)

52
Q

Subtract 2 from each array element

A

np.subtract(arr,2)

53
Q

Multiply each array element by 3

A

np.mulitply(arr,3)

54
Q

Divide each array element by 4

A

np.divide(arr,4)

55
Q

Raise each array element to the 5th power

A

np.power(arr,5)

56
Q

Elementwise add arr2 to arr1

A

np.add(arr1,arr2)

57
Q

Elementwise subtract arr2 from arr1

A

np.subtract(arr1,arr2)

58
Q

Elementwise multiply arr1 by arr2

A

np.multiply(arr1,arr2)

59
Q

Elementwise divide arr1 by arr2

A

np.divide(arr1,arr2)

60
Q

Elementwise raise arr1 raised to the power of arr2

A

np.power(arr1,arr2)

61
Q

Returns true if the arrays have the same elements and shape

A

np.array_equal(arr1,arr2)

62
Q

Square root of each element in the array

A

np.sqrt(arr)

63
Q

Sine of each element in the array

A

np.sin(arr)

64
Q

Natural log of each element in the array

A

np.log(arr)

65
Q

Absolute value of each element in the array

A

np.abs(arr)

66
Q

Rounds up to the nearest int

A

np.ceil(arr)

67
Q

Rounds down to the nearest int

A

np.floor(arr)

68
Q

Rounds to the nearest int

A

np.round(arr)

69
Q

Returns mean along specific axis

A

np.mean(arr,axis=0)

70
Q

Returns sum of arr

A

arr.sum()

71
Q

Returns minimum value of arr

A

arr.min()

72
Q

Returns maximum value of specific axis

A

arr.max(axis=0)

73
Q

Returns the variance of array

A

np.var(arr)

74
Q

Returns the standard deviation of specific axis

A

np.std(arr,axis=1)

75
Q

Returns correlation coefficient of array

A

arr.corrcoef()