Numpy Flashcards

1
Q

convert list to numpy array

A

np.array(list)

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

arange

A

np.arange(10,30)

Return evenly spaced values within a given interval.

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

zeros

A

np.zeros(count)

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

ones

A

np.ones(count)

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

linspace

A

np.linspace(0,10,3)

np.linspace(start,stop,count)

Return evenly spaced numbers over a specified interval.

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

eye

A

np.eye(4)

Creates an 4x4 identity matrix
array([[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]])

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

rand

A

np.random.rand(2)
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1) .

np.random,rand(5,5)

creates 5x5 matrix with random float values 0-1

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

randn

A

np.random.randn(2) –> 2 values

np.random.randn(5,5)
creates 5x5 matrix with random float values.

Return a sample (or samples) from the “standard normal” distribution. Unlike rand which is uniform:

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

randint

A

np.random.randint(1,10)

Return random integers from low (inclusive) to high (exclusive). similar to random module

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

Reshape

A

array.reshape(5,5)

Returns an array containing the same data with a new shape.

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

argmax

A

arr = np.array([[3, 2, 1], [6, 5, 4], [9, 8, 7]])
np.argmax(arr)
# Find the indices of the maximum element along the flattened array

np.argmax(arr,axis = 0)
# Find the indices of the maximum element along the first axis (rows)

np.argmxax(arr, axis = 1)
# Find the indices of the maximum element along the second axis (columns)

argmax is a function that returns the indices of the maximum values along a given axis.

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

Shape

A

Shape is an attribute that arrays have (not a method) returns the shape of an array

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

dtype

A

You can also grab the data type of the object in the array:

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

Fetching the values from numpy array

A

arr[index]

arr[start : stop]

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

Broadcasting

A

arr[0:5]=100

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

copy

A

To get a copy, need to be explicit

arr.copy()

17
Q

Indexing a 2D array (matrices) in numpy

A

The general format is arr_2d[row][col] or arr_2d[row,col].
arr_2d[1][0] == arr_2d[1,0]

18
Q

2D array slicing in numpy

A

arr_2d[row,col]

arr_2d[:2,1:]

arr_2d[2,:]

19
Q

Fancy Indexing in Numpy

A

arr2d = arr[[0., 0., 0., 0.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.]]
print(arr2d[[1,3]]) –> arr2d[[row1,row2,…]]
o/p
——
array([[1., 1., 1., 1.],
[3., 3., 3., 3.]])

20
Q

Selection based on comparison operators

A

arr > 4

returns an array of true or false
boll_arr = array([False, True, False, False, True]

arr[boll_arr] == arr[arr>2]
returns values greater than 4
array([ 5, 6, 7, 8])

21
Q

Arithematic operators on array

A

arr + arr
arr * arr
arr - arr
arr / arr
arr ** arr

22
Q

Array functions

A

np.sqrt(arr)
np.exp(arr)
np.max(arr)
np.sin(arr)
np.log(arr)