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)