NumPy Flashcards

1
Q

What is NumPy

A

A Python library for numerical data.

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

What does NumPy provide functionality for?

A

Arrays, Linear Algebra, and RNG

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

Why is important to use “import numpy as np” as opposed to “from numpy import *”?

A

Because NumPy namespace conflicts with python’s with functions like ‘min’ or ‘max’.

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

How much faster are NumPy array operations vs standard python?

A

About 20 to 30 times faster.

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

Why is NumPy so much faster than Python?

A

NumPy stores 1 object type and no need to dynamically create and reference variably sized memory addresses.

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

How do you declare an array in numpy?

A

new_arr = np.array([x1,x2,…,xn])

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

NumPy Boolean Operators: what does the following code produce?

a1 = np.array([[1., 2., 3.], [4., 5., 6.]])
a2 = np.array([[0., 4., 1.], [7., 2., 12.]])
a2 > a1

A

array([[False, True, False],
[ True, False, True]])

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

Given nparray new_arr, what does new_arr.shape return?

A

A tuple containing the dimensions of new_arr.

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

Given nparray new_arr, what does new_arr.dtype return?

A

The type of the data stored in new_arr.

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

How do you specify the datatype in an nparray?

A

By using the overloaded constructor with the dtype flag. Ex: new_arr = np.array([1,2,3], dtype=np.int32)

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

Will numpy allow creation of arrays with multiple data types?

A

Yes, but all indices will default to the common denominator (i.e. to a string or a float)

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

What is default_rng?

A

A random number generator in numpy.

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

How is default_rng imported?

A

from numpy.random import default_rng

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

What does default_rng.integers(1,5, size=(10,)) produce?

A

A 1x10 array with int objects from 1 to 4.

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

what does default_rng.random(size=(3,3)) produce?

A

A 3x3 array with float objects between 0.0 and 1.0.

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

What does default_rng.standard_normal(size=(5,)) produce?

A

A 1x5 array with float objects that will produce a gaussian distribution over time.

16
Q

What does np.arange(1, 4, 0.1) produce?

A

A 1 dimensional array with all values of stride 0.1 between 1.0 and 3.9.

17
Q

What does np.linspace(1, 4, 2) produce?

A

A 1 dimensional array with all values of stride 0.2 between 1.0 and 4.0.

18
Q

Do list slices create a reference of a list or a copy?

A

A reference.

19
Q

Does fancy indexing create a reference of a list or a copy?

A

A copy.

20
Q

Given an array new_arr, how would you copy a slice of indices 5 through 8?

A

new__arr[5:9].copy()

21
Q

Given an array new_arr, how would you access the 2’nd row?

A

new_arr[1]

22
Q

Given an array new_arr, how would you access the second column?

A

new_arr[:, 1]

23
Q

Given an array new_arr, how would you extract the first three rows using fancy indexing?

A

new_arr[ [0,1,2] ]

24
Q

Given an array new_arr, how would you extract the first three columns using fancy indexing?

A

new_arr[ :, [0,1,2] ]

24
Q

What is Boolean indexing?

A

Selectively accessing parts of an array based upon a Boolean condition.

25
Q

What are the not, and, or operators in NumPy?

A

not = ~
and = &
or = |

26
Q

What does the reshape function do to a numpy array?

A

It modifies the shape of an array as long as no elements are lost or created.

27
Q

Given one dimensional numpy array num_arr with 15 elements, what does the following code produce?

num_arr.reshape((5, 3))

A

It takes the elements of num_arr and produces an array with 5 rows and 3 columns.

28
Q

What is the transpose of an array?

A

An array with all column and row indices flipped.

29
Q

What are universal functions (or ufuncs) in Numpy?

A

A function that performs element-wise operations on data in an np.array

30
Q

What is array broadcasting?

A

It is taking a vector and adding it to every row of an array.

31
Q

How would you find the sum of all elements in numpy array num_arr?

A

np.sum(num_arr) or num_arr.sum()

32
Q

How would you find the mean of all elements in numpy array num_arr?

A

np.mean(num_arr) or num_arr.mean()

33
Q

How would you find the standard deviation of all elements in numpy array num_arr?

A

np.std(num_arr) or num_arr.std()

34
Q

What is the difference between np.min and np.argmin or np.max and np.argmax?

A

min or max returns the value, argmin or argmax return the index of that value.

35
Q

What does np.sum(axis=0) return?

A

The sum of each column in an array.

36
Q

What does np.sum(axis=1) return?

A

The sum of each row in an array.

37
Q

Given num_arr = np.array([false, false, true, true, true]), what is returned by num_arr.sum()?

A

3