numpy Primer Flashcards Preview

11637 Foundations of Computational Data Science > numpy Primer > Flashcards

Flashcards in numpy Primer Deck (85)
Loading flashcards...
1

What's a major benefit of numpy? How does it do this?

Efficient manipulation of large datasets. It's especially useful for high-dimensional array operations.

It does this through:

  • A homogeneous array that allows for faster access
  • Specialized C-based implementations of many operations.

2

What's the heart of numpy?

The array data structure np.ndarray, which offers many powerful utilities.

3

What's the rank of an array?

The number of dimensions it has

4

What's the shape of a numpy array? (type, make-up)

  • Type: An n-element tuple
  • Make-up: each element denotes the size of an array along a particular dimension.

5

How to create an array

a = np.array([[1., 2.], [3., 4.]])

6

What type are the elements of a numpy array?

  • numpy will deduce using the elements passed in
  • If you don't pass elements in initially (e.g. zeros method), default is float64

7

How do you access an element in a numpy array?

[row_index, col_index]

8

How to create a numpy array of a certain constant

np.full((2,2), 100)

goes to: 

array([[100, 100], [100, 100]])

9

How to create an identity matrix in numpy

np.eye( length of side)

10

How to create a numpy array of a random sample from a predefined distribution (e.g. normal)?

np.random.some_distribution.(size = some_shape_tuple)

np.random.normal(size = (2, 3))

goes to:

array([[-1.41377114, -0.26157494, 0.05751016],

[ 0.64421317, -0.46843433, -2.47728257]])

11

What do we know about arrays of shape ( some_int, )?

They should be thought of as column vectors, since they have multiple rows, but only one column.

12

What does the shape (2,) mean? How do you initialize an array of that shape?

For example: np.array([1,2])

13

What happens when you transpose a 1D array in numpy?

Nothing. It will still be a column vector.

14

How do you get the shape of a numpy array?

.shape

15

How do you transpose a numpy array?

.T

16

What's the syntax for slicing in numpy?

 Syntax is start_index:end_index. You must specify a slice for each dimension of the array. To take all values in a certain dimension, you can use a standalone : or just leave that dimension blank.

17

What do we know about how numpy slicing works in memory?

It's a shallow copy? If you make a new array that's a slice of another array and you modify the new array, it will also modify the original array.

18

When mixing integer and slicing indexing, how does slicing vs using integer indexing effect the rank?

Each dimension with integer indexing reduces the rank by 1. Using slicing in all dimensions preserves the rank.

19

What's integer array indexing?

Instead of an index, you put an array of indexes where the index would be. That will get all of the portions or the array corresponding to the specified indexes, and it will combine them into the output array. 

 

E.g. this gets  two copies of row 0 in a and three copies of row 1 in a:

a[[0, 0, 1, 1, 1],:]

20

What's a good way to quickly combine different portions of an input array to form a new output array?

integer array indexing

21

How do you manipulate two elements in a numpy array in one line? 

Use integer array indexing. E.g. 

22

What's boolean array indexing?

Can easily test a numpy array with a boolean condition. 

a = np.array([[1,2], [3,4], [5,6]])
print(a > 2)

Returns an array of booleans.

23

What does this do on multidimensional numpy array a?

print(a[a > 0.9])

Returns a rank-1 array of elements that meet the condition. This boolean array indexing is always rank 1. 

24

How does numpy determine the data type of array elements?

 

  • Numpy will try to guess the data type of an array upon creation, but you can also explicitly specify the data type

25

How do you explicitly specify a datatype in numpy?

z = np.array([1, 2], dtype=np.int64)

26

How do you convert from one datatype to another in numpy?

Use astype. E.g.:

print(x.astype(np.float64))

27

How do you element-wise multiply two arrays in numpy?

*

28

What do we know about binary operator operations in numpy?

the two input arrays must have the same shape (I think after broadcasting, if applicable)

29

How do you raise elements to an exponent in numpy?

**

30

What's np.dot used for? (3)

What method type is it?

use np.dot to

  • compute inner products of vectors
  • multiply a vector by a matrix
  • multiply matrices

dot is available both as a function in the numpy module and as an instance method of array objects.

# dot product of v and w
print(v.dot(w))

# equivalent function to compute dot product
print(np.dot(v, w))