numPy Flashcards
how to import numpy in python?
import numpy as np
we usually assign the package to a variable named np
how to create a numpy array (numpy.ndarray) with 3 elements, all zeros? (assign to a variable named “a”)
a = np.zeros(3)
how to create a numpy array (numpy.ndarray) with 6 elements, all ones? (assign to a variable named “a”)
a = np.ones(6)
how to create a 2 dimensional numpy array (numpy.ndarray) with shape 6, 4 with elements all zeros? (assign to a variable named “a”)
a = np.zeros((6,4))
the parameter is a tuple!
how to print the variable named “v” in Jupyter to see its content?
v
how to print the first element of one dimensional numpy array “a” in Jupyter to see its content?
a[0]
how to print the type of variable “v” in Jupyter?
type(v)
how to print the shape of a numpy array named “a” in Jupyter?
a.shape
this is the numpy.ndarray.shape function
how to change the shape of a numpy array named “a” in Jupyter to (4,5)?
a.shape = (4,5)
with a tuple
how to create a one dimensional numpy array, starting with 10, ending with 30, with equally distant elements, 12 of them altogether?
(assign to variable “a”)
a = np.linspace(10,30,12)
create a python list of 4,5,8 and immidiately convert to a numpy array and assign to a variable named “a”
a = np.array([4,5,8])
how to efficiently access the last element in a python list named l?
l[-1]
“a” array or list has 10 elements. Access the elements indexed 3,4,5
a[3:6]
create an array with 50 random floats, evenly distributed, from
0 to 10
a = np.random.rand(50)*10
create an array with 20 random integers from 0 to 10 (including 10 as a possible value)
a = np.random.randint(0,11,size=20)
access every second element of a list or array, starting from the first
a[::2]
how to return an array of booleans, indicating if numpy array “a” values are greater than 5 or not at corresponding indexes?
a > 5
use numpy to return mean of array “a”
np.mean(a)
use numpy to return standard deviation of array “a”
np.std(a)
use numpy to return sum of array “a”
np.sum(a)
use numpy to return variance of array “a”
np.var(a)
use numpy to return minimum of array “a” and also the index of the first occurance of it
minOfA = np.min(a) firstIndexOfMinOfA = np.argmin(a)
use numpy to return an array which contains the elements of array “a” that are greater than 3
a[a > 3]
change array “a” so that the values greater than 10 will be 5, the other will be 0
a = np.where(a > 10, 5, 0)