Numpy | Basics | Priority Flashcards

1
Q

Pearson’s correlation r from a pandas dataframe.

A

corrmat = np.corrcoef(df.values.T)
corrmat = np.corrcoef(df.values, rowvar=False)

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

Function to count the number of occurrences of each value in array.

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

Function to modify a sequence in-place by shuffling its contents.

A

… indices = np.arange(X.shape[0])
… np.random.shuffle(indices)

Raschka MLWPT Chapter 11 Implementing a Multilayer Artificial Neural Network from Scratch

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

Create and use a random number generator.

A

“rng = np.random.RandomState(random_seed)
self.weight_h = rng.normal(
loc=0.0, scale=0.1, size=(num_hidden, num_features))”

Raschka MLWPT Chapter 11 Implementing a Multilayer Artificial Neural Network from Scratch

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

Example of how to convert an array’s data type.

A

example: ary2d.astype(np.int32)

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.1: NumPy Basics

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

Example of how to initialize an array with 0s or 1s and a specific data type.

A

np.ones((3, 4), dtype=np.int)

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.2: NumPy Array Construction and Indexing

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

Example of how to create a particular number of evenly spaced values in a specified interval.

A

np.linspace(6., 15., num=10)

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.2: NumPy Array Construction and Indexing

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

Example of how to add 1 to every element in a 2D array.

A

ary + 1

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.3: NumPy Array Math and Universal Functions

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

Examples of how to get the square root and exponentiated versions of an array.

A
np.sqrt(ary)
;
ary**2 OR np.power(ary, 2)

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.3: NumPy Array Math and Universal Functions

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

Idioms to sum out rows and sum out columns.

A

np.sum(axis=1) # i.e. sum along columns to get row sums; np.sum(axis=0) # i.e. sum along rows to get column sums

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.3: NumPy Array Math and Universal Functions

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

Function to return a sorted copy of an array; Function to return the indices that would sort an array.

A

np.sort(); np.argsort()

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.3: NumPy Array Math and Universal Functions

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

Slicing creates what? What is a way to avoid this behavior?

A

Views. ary.copy()

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.5: NumPy Advanced Indexing – Memory Views and Copies

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

Fancy indexing always returns what? Is boolean indexing fancy indexing?

A

A copy of an array. Yes.

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.5: NumPy Advanced Indexing – Memory Views and Copies

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

Example of how to select array elements that are greater than 3 and divisible by 2.

A

ary[(ary > 3) & (ary % 2 == 0)]

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.5: NumPy Advanced Indexing – Memory Views and Copies

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

What is the difference between flatten() and ravel()? What is one more way to flatten an array?

A

flatten: copy; ravel: view; ary.reshape(-1)

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.7: Reshaping NumPy Arrays

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

What is the difference between flatten() and ravel()? What is one more way to flatten an array?

A

flatten: copy; ravel: view; ary.reshape(-1)

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.7: Reshaping NumPy Arrays

17
Q

Idioms to stack along rows and along columns.

A

np.concatenate((ary, ary), axis=0); np.concatenate((ary, ary), axis=1)

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.7: Reshaping NumPy Arrays

18
Q

Idiom to count how many elements in an array meet a certain condition.

A

mask = ary > 2; mask.sum()

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.8: NumPy Comparison Operators and Masks

19
Q

Function to assign values to specific elements in an array.

A

Example: np.where(ary, 1, 0)

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.8: NumPy Comparison Operators and Masks

20
Q

3 ways to reshape a one-dimensional array into a two-dimensional one.

A

row_vector..reshape(-1, 1); row_vector[:, np.newaxis]; row_vector[:, None]

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.9: Linear Algebra with NumPy

21
Q

3 ways to compute the dot product of a row_vector.

A

np.matmul(row_vector, row_vector); np.dot(row_vector, row_vector); row_vector @ row_vector

Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.9: Linear Algebra with NumPy