NumPy Flashcards
What is NumPy
A Python library for numerical data.
What does NumPy provide functionality for?
Arrays, Linear Algebra, and RNG
Why is important to use “import numpy as np” as opposed to “from numpy import *”?
Because NumPy namespace conflicts with python’s with functions like ‘min’ or ‘max’.
How much faster are NumPy array operations vs standard python?
About 20 to 30 times faster or more
Why is NumPy so much faster than Python?
NumPy stores 1 object type and no need to dynamically create and reference variably sized memory addresses.
How do you declare an array in numpy?
new_arr = np.array([x1,x2,…,xn])
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
array([[False, True, False],
[ True, False, True]])
Given nparray new_arr, what does new_arr.shape return?
A tuple containing the dimensions of new_arr.
Given nparray new_arr, what does new_arr.dtype return?
The type of the data stored in new_arr.
How do you specify the datatype in an nparray?
By using the overloaded constructor with the dtype flag. Ex: new_arr = np.array([1,2,3], dtype=np.int32)
Will numpy allow creation of arrays with multiple data types?
Yes, but all indices will default to the common denominator (i.e. to a string or a float)
What is default_rng?
A random number generator in numpy.
How is default_rng imported?
from numpy.random import default_rng
What does default_rng.integers(1,5, size=(10,)) produce?
A 1x10 array with int objects from 1 to 4.
what does default_rng.random(size=(3,3)) produce?
A 3x3 array with float objects between 0.0 and 1.0.