Introdução a Numpy¶
In [1]:
Copied!
import numpy as np
import numpy as np
In [2]:
Copied!
np.pi
np.pi
Out[2]:
3.141592653589793
In [8]:
Copied!
a = np.arange(0,31,2)
print(a)
type(a)
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[8]:
numpy.ndarray
In [9]:
Copied!
b = np.linspace(0,10, 21)
print(b)
type(b)
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[9]:
numpy.ndarray
In [12]:
Copied!
c = np.array([1,2,3])
c
c = np.array([1,2,3])
c
Out[12]:
array([1, 2, 3])
In [24]:
Copied!
print("soma", b.sum()) # https://numpy.org/doc/stable/reference/generated/numpy.sum.html?highlight=sum
print("média", b.mean()) # https://numpy.org/doc/stable/reference/generated/numpy.mean.html#numpy.mean
print("desvio padrão", b.std()) # https://numpy.org/doc/stable/reference/generated/numpy.std.html
print("variancia", b.var()) # https://numpy.org/doc/stable/reference/generated/numpy.var.html#numpy.var
print("máximo", b.max()) # https://numpy.org/doc/stable/reference/generated/numpy.ndarray.max.html
print("mínimo", b.min()) # https://numpy.org/doc/stable/reference/generated/numpy.ndarray.min.html
print("indice máximo", b.argmax()) # https://numpy.org/doc/stable/reference/generated/numpy.argmax.html?highlight=numpy%20max
print("indice mínimo", b.argmin()) # https://numpy.org/doc/stable/reference/generated/numpy.argmin.html#numpy.argmin
print("soma", b.sum()) # https://numpy.org/doc/stable/reference/generated/numpy.sum.html?highlight=sum
print("média", b.mean()) # https://numpy.org/doc/stable/reference/generated/numpy.mean.html#numpy.mean
print("desvio padrão", b.std()) # https://numpy.org/doc/stable/reference/generated/numpy.std.html
print("variancia", b.var()) # https://numpy.org/doc/stable/reference/generated/numpy.var.html#numpy.var
print("máximo", b.max()) # https://numpy.org/doc/stable/reference/generated/numpy.ndarray.max.html
print("mínimo", b.min()) # https://numpy.org/doc/stable/reference/generated/numpy.ndarray.min.html
print("indice máximo", b.argmax()) # https://numpy.org/doc/stable/reference/generated/numpy.argmax.html?highlight=numpy%20max
print("indice mínimo", b.argmin()) # https://numpy.org/doc/stable/reference/generated/numpy.argmin.html#numpy.argmin
soma 105.0 média 5.0 desvio padrão 3.0276503540974917 variancia 9.166666666666666 máximo 10.0 mínimo 0.0 indice máximo 20 indice mínimo 0
In [25]:
Copied!
b.shape
b.shape
Out[25]:
(21,)
In [28]:
Copied!
d = b.reshape(3,7)
print(d.shape)
d
d = b.reshape(3,7)
print(d.shape)
d
(3, 7)
Out[28]:
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 [29]:
Copied!
e = np.array([1,3,4])
f = np.array([7,8,9])
e = np.array([1,3,4])
f = np.array([7,8,9])
In [32]:
Copied!
print(e + f)
print(e * f)
print(e @ f)
print(e + f)
print(e * f)
print(e @ f)
[ 8 11 13] [ 7 24 36] 67
Funções de Algebra Linear¶
In [36]:
Copied!
mat1 = np.linspace(0,12, 9)
mat1 = mat1.reshape(3,3)
mat1
mat1 = np.linspace(0,12, 9)
mat1 = mat1.reshape(3,3)
mat1
Out[36]:
array([[ 0. , 1.5, 3. ], [ 4.5, 6. , 7.5], [ 9. , 10.5, 12. ]])
In [38]:
Copied!
mat2 = np.arange(0,9, 1)
mat2 = mat2.reshape(3,3)
mat2
mat2 = np.arange(0,9, 1)
mat2 = mat2.reshape(3,3)
mat2
Out[38]:
array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
In [44]:
Copied!
print("Dor Product \n",np.dot(mat1, mat2))
print("Inner Product \n",np.inner(mat1, mat2))
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]]
In [2]:
Copied!
from skimage import io
from skimage import color
from matplotlib import pyplot as plt
from skimage import io
from skimage import color
from matplotlib import pyplot as plt
In [3]:
Copied!
foto = io.imread('https://255ribeiro.github.io/curso_python_gis/cientif_py/bebe_01.jpeg')
plt.imshow(foto)
type(foto)
foto = io.imread('https://255ribeiro.github.io/curso_python_gis/cientif_py/bebe_01.jpeg')
plt.imshow(foto)
type(foto)
Out[3]:
numpy.ndarray
In [49]:
Copied!
fotopb = color.rgb2gray(foto)
plt.imshow(fotopb.T, cmap=plt.cm.gray)
fotopb.shape
fotopb = color.rgb2gray(foto)
plt.imshow(fotopb.T, cmap=plt.cm.gray)
fotopb.shape
Out[49]:
(1197, 1600)
In [50]:
Copied!
fotoT = np.transpose(foto, axes=(1,0,2))
plt.imshow(fotoT)
fotoT = np.transpose(foto, axes=(1,0,2))
plt.imshow(fotoT)
Out[50]:
<matplotlib.image.AxesImage at 0x137d7811b50>
In [51]:
Copied!
foto_filtro = np.where(foto >150, 255, 0)
plt.imshow(foto_filtro)
foto_filtro = np.where(foto >150, 255, 0)
plt.imshow(foto_filtro)
Out[51]:
<matplotlib.image.AxesImage at 0x137d7e0f6d0>
In [57]:
Copied!
foto_cut = foto[0:700, 550:1050]
plt.imshow(foto_cut)
foto_cut = foto[0:700, 550:1050]
plt.imshow(foto_cut)
Out[57]:
<matplotlib.image.AxesImage at 0x137d66c01f0>