Numpy Introduction¶
In [1]:
import numpy as np
In [2]:
np.pi
Out[2]:
3.141592653589793
In [3]:
a = np.arange(0,31,2)
print(a)
type(a)
[ 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30]
Out[3]:
numpy.ndarray
In [4]:
b = np.linspace(0,10, 21)
print(b)
type(b)
[ 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7. 7.5 8. 8.5 9. 9.5 10. ]
Out[4]:
numpy.ndarray
In [5]:
c = np.array([1,2,3])
c
Out[5]:
array([1, 2, 3])
In [6]:
print("sum", b.sum()) # https://numpy.org/doc/stable/reference/generated/numpy.sum.html?highlight=sum
print("mean", b.mean()) # https://numpy.org/doc/stable/reference/generated/numpy.mean.html#numpy.mean
print("Standard Deviation", b.std()) # https://numpy.org/doc/stable/reference/generated/numpy.std.html
print("Variance", b.var()) # https://numpy.org/doc/stable/reference/generated/numpy.var.html#numpy.var
print("Max", b.max()) # https://numpy.org/doc/stable/reference/generated/numpy.ndarray.max.html
print("Min", b.min()) # https://numpy.org/doc/stable/reference/generated/numpy.ndarray.min.html
print("Max Index", b.argmax()) # https://numpy.org/doc/stable/reference/generated/numpy.argmax.html?highlight=numpy%20max
print("Min Index", b.argmin()) # https://numpy.org/doc/stable/reference/generated/numpy.argmin.html#numpy.argmin
sum 105.0 mean 5.0 Standard Deviation 3.0276503540974917 Variance 9.166666666666666 Max 10.0 Min 0.0 Max Index 20 Min Index 0
In [7]:
b.shape
Out[7]:
(21,)
In [8]:
d = b.reshape(3,7)
print(d.shape)
d
(3, 7)
Out[8]:
array([[ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. ], [ 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5], [ 7. , 7.5, 8. , 8.5, 9. , 9.5, 10. ]])
In [9]:
e = np.array([1,3,4])
f = np.array([7,8,9])
In [10]:
print(e + f)
print(e * f)
print(e @ f)
[ 8 11 13] [ 7 24 36] 67
Linear Algebra¶
In [11]:
mat1 = np.linspace(0,12, 9)
mat1 = mat1.reshape(3,3)
mat1
Out[11]:
array([[ 0. , 1.5, 3. ], [ 4.5, 6. , 7.5], [ 9. , 10.5, 12. ]])
In [12]:
mat2 = np.arange(0,9, 1)
mat2 = mat2.reshape(3,3)
mat2
Out[12]:
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
In [13]:
print("Dor Product \n",np.dot(mat1, mat2))
print("Inner Product \n",np.inner(mat1, mat2))
Dor Product [[ 22.5 27. 31.5] [ 63. 81. 99. ] [103.5 135. 166.5]] Inner Product [[ 7.5 21. 34.5] [ 21. 75. 129. ] [ 34.5 129. 223.5]]
Images as arrays¶
In [14]:
from skimage import io
from skimage import color
from matplotlib import pyplot as plt
In [15]:
foto = io.imread('https://github.com/255ribeiro/Algorithmic_Modeling_Special_Training/blob/master/docs/Python_Training/cientif_py/bebe_01.jpeg?raw=true')
plt.imshow(foto)
type(foto)
Out[15]:
numpy.ndarray
In [16]:
fotopb = color.rgb2gray(foto)
plt.imshow(fotopb.T, cmap=plt.cm.gray)
fotopb.shape
Out[16]:
(1197, 1600)
In [17]:
fotoT = np.transpose(foto, axes=(1,0,2))
plt.imshow(fotoT)
Out[17]:
<matplotlib.image.AxesImage at 0x1a66844fdc0>
In [18]:
foto_filtro = np.where(foto >150, 255, 0)
plt.imshow(foto_filtro)
Out[18]:
<matplotlib.image.AxesImage at 0x1a668a5c6a0>
In [19]:
foto_cut = foto[0:700, 550:1050]
plt.imshow(foto_cut)
Out[19]:
<matplotlib.image.AxesImage at 0x1a668ab8580>