Numpy | Basics | Priority Flashcards
Pearson’s correlation r from a pandas dataframe.
corrmat = np.corrcoef(df.values.T)
corrmat = np.corrcoef(df.values, rowvar=False)
Machine Learning with PyTorch and Scikit-Learn Chapter 9 p277
Function to count the number of occurrences of each value in array.
np.bincount()
Machine Learning with PyTorch and Scikit-Learn Chapter 3 p55
Function to modify a sequence in-place by shuffling its contents.
… indices = np.arange(X.shape[0])
… np.random.shuffle(indices)
Raschka MLWPT Chapter 11 Implementing a Multilayer Artificial Neural Network from Scratch
Create and use a random number generator.
“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
Example of how to convert an array’s data type.
example: ary2d.astype(np.int32)
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.1: NumPy Basics
Example of how to initialize an array with 0s or 1s and a specific data type.
np.ones((3, 4), dtype=np.int)
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.2: NumPy Array Construction and Indexing
Example of how to create a particular number of evenly spaced values in a specified interval.
np.linspace(6., 15., num=10)
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.2: NumPy Array Construction and Indexing
Example of how to add 1 to every element in a 2D array.
ary + 1
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.3: NumPy Array Math and Universal Functions
Examples of how to get the square root and exponentiated versions of an array.
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
Idioms to sum out rows and sum out columns.
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
Function to return a sorted copy of an array; Function to return the indices that would sort an array.
np.sort(); np.argsort()
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.3: NumPy Array Math and Universal Functions
Slicing creates what? What is a way to avoid this behavior?
Views. ary.copy()
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.5: NumPy Advanced Indexing – Memory Views and Copies
Fancy indexing always returns what? Is boolean indexing fancy indexing?
A copy of an array. Yes.
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.5: NumPy Advanced Indexing – Memory Views and Copies
Example of how to select array elements that are greater than 3 and divisible by 2.
ary[(ary > 3) & (ary % 2 == 0)]
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.5: NumPy Advanced Indexing – Memory Views and Copies
What is the difference between flatten() and ravel()? What is one more way to flatten an array?
flatten: copy; ravel: view; ary.reshape(-1)
Scientific Computing in Python: Introduction to NumPy and Matplotlib 4.7: Reshaping NumPy Arrays