Numpy Flashcards

1
Q

How do you create a numpy array?

A

import numpy as np

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

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

How do you create an array with a range of elements?

A

np.arrange(4)

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

How do you create an array with evenly spaced elements

A

np.arrange(2,9,2)

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

How do you sort an array?

A

np.sort(array)

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

How do you change the shape of an array?

A

array.reshape(rows,cols)

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

Can you select a subset of numpy arrays?

A

Yes; create a boolean and then insert it in the slicer

criteria = y < 3
y[criteria]

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

How do you create a matrix of zeros?

A

np.zeros((5,5))

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

How do you create a matrix of ones?

A

np.ones((2,10))

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

Does np.zeros(()) create integers or floats?

A

Floats

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

How do you create random numbers?J

A

np.random.randint(0,100)

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

How do you create random numbers in a matrix?

A

np.random.randint(0,100,(5,5))

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

How do you get numbers that are spaced evenly?

A

np.linspace(0,10,101)

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

What does np.linspace(0,10,101) mean?

A

101 numbers between 0 to 10 that are equally spaced

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

How do you get consistent random numbers?

A

np.random.seed(x)

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

Does seed persistent beyond the next row?

A

No

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

How do you get the maximum value in an array?

A

arrayname.max()

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

How do you find out where in an array the max value is?

A

arrayname.argmax()

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

How do you change the shape of an array?

A

arrayname.reshape(2,5)

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

How do you slice on rows / columns?

A

arrayname[5,2]

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

How does numpy format arrays?

A

makes each number the same size as the biggest number by adding spaces

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

What is faster, numpy or lists?

A

Numpy

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

How do you access the dimensions of an array?

A

name. ndim

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

How do you access the number of rows and columns in an array

A

name.shape

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

What does shape give you for a 1 dimensional array?

A

Gives the number of elements

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

What are .dtype and .shape

A

Attributes - has to be on an objective (i.e. they are not functions)

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

How do you get the number of elements in a 2 dimensional array?

A

name.size

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

How do you remove dimensionality from an array?

A

name.flat iterate over

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

How do you create an array?

A

np.array([]) - must be a list of lists if it is multidimensional

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

Do you feed 5,2 or (5,2) to np.ones()?

A

A tuple i.e. np.ones((5,2))

30
Q

Can you get integers instead of floats in np.ones()?

A

Yes np.ones((5,2), dtype = int)

31
Q

How do you get a array of numbers of any number?

A

np.full((3,5),13)

32
Q

How do you create an arrange of items 0 to 4?

A

np.arange(5)

33
Q

How many elements does lin space take and what are they?

A

start, end and num =

np.linspace(0,1,num = 5)

34
Q

How do you specify the shape of an array using a range?

A

np.arange(0,21).reshape(4,5)

35
Q

Is reshape in place?

A

No

36
Q

If you do operators on a numpy array - is it in place?

A

No

37
Q

Do you need to iterate over items to do things to an array?

A

No anarray * 2 works

38
Q

Can you do augmented assignments in numpy?

A

Yes numbers += 10 adds 10 to every element

39
Q

Can you multiply arrays together?

A

Yes

40
Q

What happens if you multiply arrays together that are not the same shape?

A

Error if you use *. You can use * or np.multipy(array1, array2) if the arrays are different sizes but only if one is the same length as a row.

41
Q

Can you do comparisons on numpy arrays?

A

Yes numbers >= 13 will produce an array of booleans

42
Q

How do you see what methods are available for the array type?

A

arrayname. then press tab

43
Q

What are some of the methods available for arrays?

A

sum() min() max() mean()

44
Q

How do you calculate the mean of a column in an array?

A

arrayname.mean(axis = 0)

45
Q

Axis = 0 is column or row?

A

column

46
Q

How do you access universal functions eg. sqrt?

A

Using the module name so if you import numpy as np then np.sqrt()

47
Q

what is the main difference between indexing lists and numpy in multidimensions?

A

list[1][2]

array [1,2]

48
Q

How do you select not continuous rows / columns in an array?

A

myarray[[1,3]]

49
Q

What is a shallow copy?

A

Where you create a new object in memory but it points to the original object where the data is actually sorted?

50
Q

How do you create a shallow copy of an array?

A

myarray.view()

51
Q

If you change the original object, does the new object change?

A

Yes as it’s pointing to the original still

52
Q

If you take a slice of an array - is it a view or is it a completely new object?

A

It is a view, so if you change the original object it will change

53
Q

How do you create a deep copy (i.e. new object) of an array?

A

x = myarray.copy()

54
Q

Reshape vs resize, which is a view and which modifies the original array?

A

Reshape is a view

55
Q

Is resize a view?

A

No

56
Q

How many dimensions does this array have? np.array[[1,2,3,4]]

A

2 - double square brackets

57
Q

How do you reduce dimensions of an array?

A

myarray.flatten()

58
Q

Flatten or ravel, which is a view?

A

Ravel is a view

59
Q

Can you filter an array?

A

Yes just like a dataframe arr[array % 2 != 0]

60
Q

How can you determine if an array contains an element?

A

np.any(arrayname)

61
Q

How do you reverse an array?

A

np.flip(arrayname, axis = )

62
Q

How do you find elements that are non zero?

A

np.nonzero(arrayname)

63
Q

How do you create an identity matrix?

A

np.identity(3)

64
Q

How do you create a specific set of values on a diagonal?

A

np.diag([1,2,3],k = 0) k means the diagonal - 0 is the main - 1 is below

65
Q

How do you get the coordinates from a flat index?

A

np.unravel_index(indexyouwant, [dimensions of array])

66
Q

How do you do matrix multiplication

A

np.dot()

67
Q

np.nonzero(arrayname) or arrayname.nonzero()

A

np.nonzero(arrayname)

68
Q

What does np.where() do?

A

Evaluates if a condition is true, and if it is does something (or not)

69
Q

What arguments does np.where() take?

A

np.where(x <4 , x, 5)

70
Q

What is the output of np.where()

A

Typically an array of number unless you tell it booleaons

71
Q

How can you test AND conditions in numpy?

A

np.logical_and(test1,test2)

72
Q

np.where() or arrayname.where()

A

np.where()