Numpy (Wk 3 UCSD / DataCamp) Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How does a (numpy) array differ from a list?

A

You can perform mathematical operations element wise on the array (vector arithmetic)

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

How do you create a numpy array?

A

np_array = np.array(list) OR np.array([x, y, z])

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

What happens if you divide 2 np arrays (of the same size)

A

the output is the an array of the same size, w/ each element the respective quotients

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

What happens if you create a numpy array of multiple types?

A

it will convert all to one type (aka type coercion)

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

what’s the output of np_array > X

A

Array of Booleans element wise [True, False, etc]

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

What’s the output of np_array[ np_array > X]

A

Returns element wise values that meet the condition (product w/ Boolean result)

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

When do you use parentheses with an array?

A

When you’re instantiating only (eg, not when you’re setting or sub-setting

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

How do you create a multi-dimensional np array?

A

np_array = np.array(list1, list2) OR = 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

How do you get the number of rows and columns of an array?

A

np_array.shape (attribute of the array)

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

How do return 2nd element from 3rd row of a 2d np array?

A

np_array[2,1] OR np_array[2][1]

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

How do you sub-set multi-dimensional np arrays?

A

np_array[ x:y, k:j ]

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

using subsetting, return the entire second row of a np array

A

np_array[ 1, : ]

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

How does arithmetic work with multi-dim arrays?

A

can do all operations. Array X Array, or Array X Vector(s)

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

Get the mean of the first column of data in a 2D np array

A

np.mean(np_array[ : , 0]

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

summary stats functions np arrays

A

np.mean(), np.median(), np.std()

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

check the corrleation between column 1 and column 2 in a 2D array

A

np.corrcoef( np_array[ : , 0] , np_array[ : , 1] )

17
Q

How to generate an array with normally distributed random numbers

A

np.random.normal(mean, std, num of values to generate)

18
Q

how to merge two 1D arrays into a single 2D array

A

np_2d = np.column_stack(( np1, np2))

19
Q

create arrays full of 0s, 1s, diagonal 1

A

np.zeros, np.ones, np,eye

20
Q

create 2d array with all value 9

A

np_array = np.full((2,2), 9.0)

21
Q

create a 2d array with random floats between 0 and 1

A

np_array = np.random.random((2,2))

22
Q

What does a sub-set of an array like a_slice = np_array[1:3 , 1:3 ]

A

a 2x2 array that you can reference with the new indices (eg 0,0; 0,1; etc), but points to the same data as the original array

23
Q

how do you make a new data structure using a slice

A

new_slice = np.array(np_array[1:2, 2:3]

24
Q

explicitly declare the data type in an array

A

x = np.array(list1, dtype=float64)&raquo_space; forces to the dtype, can use various types

25
Q

get the mean by row, or by col, of an array

A

arr.mean(axis=1)&raquo_space; row; arr.mean(axis=2)&raquo_space; column

26
Q

print just the unique values of an array

A

print(np.unique(np_array))

27
Q

what happens if you add a 1,3 array to a 3,3 array

A

broadcasting - eg, python adds the 1,3 row to each row (will works same column wise)