09 NumPy Udemy Flashcards
NumPy is a Linear Algebra Library for Python, the Reasos it is so important for Data Science with Python is that almost all of the libraries in the PyData Ecosystem rely on NumPy as one of their main building blocks
NumPy is also incredibly fast, as it has bindings to C libraries
Vectors are strictly 1-d arrays
Matrices are 2-d
but can still have only one row or one columns
NumPy Arrays
my_list = [1,2,3] my_list np.array(my_list) == [1, 2, 3] array([1, 2, 3])
my_matrix = [[1,2,3],[4,5,6],[7,8,9]] my_matrix np.array(my_matrix) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.arange(0,10,2)
==
array([ 0, 2, 4, 6, 8])
np.zeros((4,5))
==
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
np.ones((2,3))
==
array([[ 1., 1., 1.],
[ 1., 1., 1.]])
np.linspace(0,10,3)
array([ 0., 5., 10.])
np.linspace(0,10,6)
array([ 0., 2., 4., 6., 8., 10.])
np.eye(4)
==
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
np.random.rand(4,3)
==
rray([[0.88277868, 0.64350899, 0.34540502],
[0.48679882, 0.27605306, 0.78273307],
[0.62853873, 0.68858502, 0.81299911],
[0.80075412, 0.49107607, 0.90190195]])
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1)
np.random.randn(5,4)
==
array([[-0.6898178 , -2.19758573, 0.0801776 , -0.04129733],
[-2.7230242 , 0.43690199, -0.23268161, 0.88727527],
[ 0.38131687, -0.15555836, 1.11193221, -2.1126014 ],
[-0.70322463, -0.16923125, -1.80195906, 2.00983817],
[-0.97640657, 1.14549034, -1.05173513, -0.80275212]])
Return a sample (or samples) from the “standard normal” distribution. Unlike rand which is uniform:
np.random.randint(1,11,100)
==
Return random integers from low (inclusive) to high (exclusive).
array([ 5, 8, 10, 5, 7, 10, 5, 3, 3, 3, 9, 8, 3, 1, 9, 10, 1,
5, 10, 2, 3, 9, 8, 2, 2, 7, 3, 4, 6, 10, 8, 7, 6, 5,
8, 2, 1, 8, 9, 4, 3, 5, 2, 9, 8, 6, 10, 8, 7, 9, 9,
7, 9, 5, 3, 4, 10, 10, 3, 3, 2, 1, 8, 5, 8, 4, 7, 10,
8, 1, 10, 10, 9, 10, 8, 3, 4, 4, 5, 6, 2, 5, 1, 3, 5,
8, 10, 10, 10, 6, 4, 4, 8, 5, 6, 8, 8, 7, 1, 8])
arr = np.arange(25)
arr
==
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
arr.reshape(5,5) == array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]])
ranarr = np.random.randint(0,50,10)
ranarr
==
array([38, 12, 4, 45, 4, 23, 14, 49, 41, 21])
ranarr. max() == ???
ranarr. argmax() == ???
49
7
(pra mínimo (min) o raciocínio é o mesmo)
argmax é o índice / posição
x.shape
mostra o formato
x.dtype
mostro o tipo de arquivo
arr = np.arange(0,11)
arr[1:5]
==
array([1, 2, 3, 4])
Bracket Indexing and Selection
The simplest way to pick one or some elements of an array looks very similar to python lists: