Chapter 01
Machine Learning Zoomcamp
NotebookPython 3 (ipykernel)41 cells
Machine Learning Zoomcamp
1.7 Introduction to NumPy
Plan:
- Creating arrays
- Multi-dimensional arrays
- Randomly generated arrays
- Element-wise operations
- Comparison operations
- Logical operations
- Summarizing operations
In [7]python · cell 2
python
import numpy as npIn [8]python · cell 3
python
npOutput
<module 'numpy' from '/home/alexey/.pyenv/versions/3.8.11/lib/python3.8/site-packages/numpy/__init__.py'>
Creating arrays
In [10]python · cell 5
python
np.zeros(10)Output
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
In [11]python · cell 6
python
np.ones(10)Output
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
In [12]python · cell 7
python
np.full(10, 2.5)Output
array([2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5])
In [15]python · cell 8
python
a = np.array([1, 2, 3, 5, 7, 12])
aOutput
array([ 1, 2, 3, 5, 7, 12])
In [17]python · cell 9
python
a[2] = 10In [18]python · cell 10
python
aOutput
array([ 1, 2, 10, 5, 7, 12])
In [20]python · cell 11
python
np.arange(3, 10)Output
array([3, 4, 5, 6, 7, 8, 9])
In [24]python · cell 12
python
np.linspace(0, 100, 11)Output
array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
Multi-dimensional arrays
In [25]python · cell 14
python
np.zeros((5, 2))Output
array([[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.],
[0., 0.]])In [27]python · cell 15
python
n = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])In [29]python · cell 16
python
n[0, 1] = 20In [30]python · cell 17
python
nOutput
array([[ 1, 20, 3],
[ 4, 5, 6],
[ 7, 8, 9]])In [34]python · cell 18
python
n[2] = [1, 1, 1]In [35]python · cell 19
python
nOutput
array([[ 1, 20, 3],
[ 4, 5, 6],
[ 1, 1, 1]])In [39]python · cell 20
python
n[:, 2] = [0, 1, 2]In [40]python · cell 21
python
nOutput
array([[ 1, 20, 0],
[ 4, 5, 1],
[ 1, 1, 2]])Randomly generated arrays
In [51]python · cell 23
python
np.random.seed(2)
100 * np.random.rand(5, 2)Output
array([[43.59949021, 2.59262318],
[54.96624779, 43.53223926],
[42.03678021, 33.0334821 ],
[20.4648634 , 61.92709664],
[29.96546737, 26.68272751]])In [50]python · cell 24
python
np.random.seed(2)
np.random.randn(5, 2)Output
array([[-0.41675785, -0.05626683],
[-2.1361961 , 1.64027081],
[-1.79343559, -0.84174737],
[ 0.50288142, -1.24528809],
[-1.05795222, -0.90900761]])In [52]python · cell 25
python
np.random.seed(2)
np.random.randint(low=0, high=100, size=(5, 2))Output
array([[40, 15],
[72, 22],
[43, 82],
[75, 7],
[34, 49]])Element-wise operations
In [53]python · cell 27
python
a = np.arange(5)
aOutput
array([0, 1, 2, 3, 4])
In [62]python · cell 28
python
b = (10 + (a * 2)) ** 2 / 100In [65]python · cell 29
python
bOutput
array([1. , 1.44, 1.96, 2.56, 3.24])
In [68]python · cell 30
python
a / b + 10Output
array([10. , 10.69444444, 11.02040816, 11.171875 , 11.2345679 ])
Comparison operations
In [70]python · cell 32
python
aOutput
array([0, 1, 2, 3, 4])
In [69]python · cell 33
python
a >= 2Output
array([False, False, True, True, True])
In [71]python · cell 34
python
bOutput
array([1. , 1.44, 1.96, 2.56, 3.24])
In [72]python · cell 35
python
a > bOutput
array([False, False, True, True, True])
In [73]python · cell 36
python
a[a > b]Output
array([2, 3, 4])
Summarizing operations
In [75]python · cell 38
python
aOutput
array([0, 1, 2, 3, 4])
In [79]python · cell 39
python
a.std()Output
1.4142135623730951
In [82]python · cell 40
python
n.min()Output
0
Next
Linear algebra refresher
