Numpy Flashcards

(72 cards)

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
What are .dtype and .shape
Attributes - has to be on an objective (i.e. they are not functions)
26
How do you get the number of elements in a 2 dimensional array?
name.size
27
How do you remove dimensionality from an array?
name.flat iterate over
28
How do you create an array?
np.array([]) - must be a list of lists if it is multidimensional
29
Do you feed 5,2 or (5,2) to np.ones()?
A tuple i.e. np.ones((5,2))
30
Can you get integers instead of floats in np.ones()?
Yes np.ones((5,2), dtype = int)
31
How do you get a array of numbers of any number?
np.full((3,5),13)
32
How do you create an arrange of items 0 to 4?
np.arange(5)
33
How many elements does lin space take and what are they?
start, end and num = | np.linspace(0,1,num = 5)
34
How do you specify the shape of an array using a range?
np.arange(0,21).reshape(4,5)
35
Is reshape in place?
No
36
If you do operators on a numpy array - is it in place?
No
37
Do you need to iterate over items to do things to an array?
No anarray * 2 works
38
Can you do augmented assignments in numpy?
Yes numbers += 10 adds 10 to every element
39
Can you multiply arrays together?
Yes
40
What happens if you multiply arrays together that are not the same shape?
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
Can you do comparisons on numpy arrays?
Yes numbers >= 13 will produce an array of booleans
42
How do you see what methods are available for the array type?
arrayname. then press tab
43
What are some of the methods available for arrays?
sum() min() max() mean()
44
How do you calculate the mean of a column in an array?
arrayname.mean(axis = 0)
45
Axis = 0 is column or row?
column
46
How do you access universal functions eg. sqrt?
Using the module name so if you import numpy as np then np.sqrt()
47
what is the main difference between indexing lists and numpy in multidimensions?
list[1][2] | array [1,2]
48
How do you select not continuous rows / columns in an array?
myarray[[1,3]]
49
What is a shallow copy?
Where you create a new object in memory but it points to the original object where the data is actually sorted?
50
How do you create a shallow copy of an array?
myarray.view()
51
If you change the original object, does the new object change?
Yes as it's pointing to the original still
52
If you take a slice of an array - is it a view or is it a completely new object?
It is a view, so if you change the original object it will change
53
How do you create a deep copy (i.e. new object) of an array?
x = myarray.copy()
54
Reshape vs resize, which is a view and which modifies the original array?
Reshape is a view
55
Is resize a view?
No
56
How many dimensions does this array have? np.array[[1,2,3,4]]
2 - double square brackets
57
How do you reduce dimensions of an array?
myarray.flatten()
58
Flatten or ravel, which is a view?
Ravel is a view
59
Can you filter an array?
Yes just like a dataframe arr[array % 2 != 0]
60
How can you determine if an array contains an element?
np.any(arrayname)
61
How do you reverse an array?
np.flip(arrayname, axis = )
62
How do you find elements that are non zero?
np.nonzero(arrayname)
63
How do you create an identity matrix?
np.identity(3)
64
How do you create a specific set of values on a diagonal?
np.diag([1,2,3],k = 0) k means the diagonal - 0 is the main - 1 is below
65
How do you get the coordinates from a flat index?
np.unravel_index(indexyouwant, [dimensions of array])
66
How do you do matrix multiplication
np.dot()
67
np.nonzero(arrayname) or arrayname.nonzero()
np.nonzero(arrayname)
68
What does np.where() do?
Evaluates if a condition is true, and if it is does something (or not)
69
What arguments does np.where() take?
np.where(x <4 , x, 5)
70
What is the output of np.where()
Typically an array of number unless you tell it booleaons
71
How can you test AND conditions in numpy?
np.logical_and(test1,test2)
72
np.where() or arrayname.where()
np.where()