Data book - Numpy Flashcards

1
Q

How do you make a 2x3x2 empty np array?

A

np.empty((2,3,2))

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

What is np’s equivalent to the range function?

A

Np.arange(n)

Out: array([0, 1, 2, …, n])

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

How do you take an existing array a and make a new array with the same shape but all 1’s?

A

Np.ones_like(a)

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

How do you make a square n x n matrix?

A

Np.eye(n)

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

How do you change an np dtype from one type to another?

A

array_to_change.astype(dtype)

E.g

a = np.array([‘1’])
a = a.astype(np.float64)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Does slicing an ndarray copy that section and return the copy, or zoom into that section?

A

Zoom in

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

How do you make a copy of an ndarray slice?

A

arr = np.array([…])

barr = arr[5:8].copy()

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

How do you select the top-left most element of the 2d array arr2d?

A

arr2d[0,0]

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

How do you generate randomly distributed data 7 rows by 4 columns?

A

Np.random.randn(7, 4)

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

How do you do both a boolean and an array slice in one line?

A

data[names==‘Bob’, 2:]

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

How do you select everything except ‘Bob’ in an ndarray?

A

names != ‘Bob’

Or

data[-(names == ‘Bob’)]

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

How do you use and/or logic in ndarray slicing?

A

Mask = (names == ‘Bob’) |/& (names == ‘Will’)

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

What is fancy indexing?

A

Indexing with integer arrays

E.g arr[4, 3, 0, 6] pops out those rows in that order

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

Say you call arr[[1, 5, 7, 2], [0, 3, 1, 2]] on a big 2D ndarray. What happens?

A

Outputs a 1D array with elements selected by [[rows], [columns]] in the input.

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

How do you convert two 1D ndarrays to an indexer that selects a square region?

A

arr[np.ix_([], [])]

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

How do you transpose?

A

arr.T

17
Q

How to compute the dot product of two arrays?

A

np.dot(a, b)

18
Q

How do you swap axis in general(e.g inn dimensions)?

A

arr.swapaxes(n1, n2, …, n)

19
Q

What is a universal function in numpy?

A

Funct that performs elementwise operations on data in ndarrays

20
Q

What are examples of ufuncs?

A

np. sqrt(arr)
np. exp(arr)

(Unary funcs - take one array)

21
Q

x = np.array([1, 3, 3,])

y = np.array([3, 1, 1])

What does np.maximum(x, y) output?

A

array([3, 3, 3)]

Also its a binary ufunc

22
Q

What does np.modf(arr) do?

A

Returns fractional and integral parts of an array

23
Q

What ufunc gives booleans corresponding to whether an element is NaN?

A

Np.isnan

24
Q

How do you make a 3 by 5 array filled with the integers 3.14?

A

np.full((3, 5), 3.14, dtype=float)

25
Q

How do you make a 3 by 3 array filled with random integers between 0 and 1?

A

np.random.random((3, 3))

26
Q

How do you make a 3 by 3 array with random values from a normal distribution with mean 0 and SD 1?

A

np.random.normal(0, 1, (3, 3))

27
Q

How do you make a 3 by 3 array with random values between 0 and 10?

A

np.random.randint(0, 10, (3, 3))

28
Q

How do you make a 3x3 identity matrix?

A

np.eye(3)

29
Q

What 3 methods are generally used for array concatenation?

A

np. concatenate
np. vstack
np. hstack

30
Q

How do you stack elements on the 3rd axis?

A

np.dstack

31
Q

What 3 methods are used for splitting?

A

np. split
np. hsplit
np. vsplit

32
Q

How do you apply a ufunc multiple times on an array x to remove a dimension?

A

np.ufunc.reduce(x)

33
Q

How do you apply a ufunc multiple times to an array x cumulatively, and save the result to an array?

A

np.ufunc.accumulate(x)

34
Q

What numpy function could you use to make an easy multiplication table in one line from an array x?

A

np.multiply.outer(x)