In [1]:
import numpy as np
In [2]:
np.pi
Out[2]:
3.141592653589793
In [3]:
list_a = [1, 1.2, 'string', True, [1,1.2, False]]
len(list_a[2])
Out[3]:
6
In [4]:
a = np.arange(5, 31, 1.5).reshape(9,2)
a.dtype
Out[4]:
dtype('float64')
In [5]:
a
Out[5]:
array([[ 5. , 6.5], [ 8. , 9.5], [11. , 12.5], [14. , 15.5], [17. , 18.5], [20. , 21.5], [23. , 24.5], [26. , 27.5], [29. , 30.5]])
In [6]:
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[6]:
numpy.ndarray
In [7]:
b.argmax()
b[20]
Out[7]:
10.0
In [8]:
c = a[2,1]
In [9]:
c
Out[9]:
12.5
In [10]:
from skimage import io
from skimage import color
from matplotlib import pyplot as plt
In [11]:
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[11]:
numpy.ndarray
In [12]:
foto.shape
Out[12]:
(1197, 1600, 3)
In [13]:
foto_filtro = np.where(foto >150, 255, 0)
plt.imshow(foto_filtro)
Out[13]:
<matplotlib.image.AxesImage at 0x24150bcc790>
In [14]:
a = list(range(0,11))
In [15]:
a
Out[15]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [16]:
a[0]
Out[16]:
0
In [17]:
b[:2]
Out[17]:
array([0. , 0.5])
In [18]:
b
Out[18]:
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 [19]:
foto_cut = foto[0:700, 550:1050]
plt.imshow(foto_cut)
Out[19]:
<matplotlib.image.AxesImage at 0x2415065f610>
Matplotlib¶
In [20]:
import matplotlib.pyplot as plt
In [21]:
x = np.arange(-10, 11, .25)
In [22]:
a
Out[22]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [23]:
plt.plot(np.sin(a))
plt.show()
In [24]:
fig, ax = plt.subplots(2, 2, figsize=(10,10))
ax[0,0].plot(np.sin(x), c='seagreen', linewidth=5)
ax[0,1].plot(np.cos(x), c='orange', linewidth=5)
ax[1,0].plot(np.log(x), c='teal', linewidth=5)
ax[1,1].plot(np.exp(x), c='firebrick', linewidth=5)
C:\Users\ferra\AppData\Local\Temp/ipykernel_9300/3263200711.py:5: RuntimeWarning: divide by zero encountered in log ax[1,0].plot(np.log(x), c='teal', linewidth=5) C:\Users\ferra\AppData\Local\Temp/ipykernel_9300/3263200711.py:5: RuntimeWarning: invalid value encountered in log ax[1,0].plot(np.log(x), c='teal', linewidth=5)
Out[24]:
[<matplotlib.lines.Line2D at 0x241508f2d30>]
Scipy¶
In [25]:
import numpy as np
import matplotlib.pyplot as plt
# you may see the pyplot imports like this:
#from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
In [26]:
x = np.linspace(0,10, 21)
x
Out[26]:
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 [27]:
y = .5*x + 10
In [28]:
plt.scatter(x,y)
Out[28]:
<matplotlib.collections.PathCollection at 0x24151720820>
In [29]:
y1 = np.random.uniform(low=-1, high=1, size= y.shape)*3
y1
Out[29]:
array([ 1.87134721, -2.13037948, 0.43348911, -1.39804321, 1.6614593 , -1.46573447, 2.92343878, -2.82147263, -1.56172733, -1.63033714, 2.51208232, -2.26058511, 1.56098448, -1.3646552 , 0.51747604, 1.33064618, -0.71374274, 2.29184471, 0.13645071, -2.0369601 , 1.35785415])
In [30]:
y = y + y1
y
Out[30]:
array([11.87134721, 8.11962052, 10.93348911, 9.35195679, 12.6614593 , 9.78426553, 14.42343878, 8.92852737, 10.43827267, 10.61966286, 15.01208232, 10.48941489, 14.56098448, 11.8853448 , 14.01747604, 15.08064618, 13.28625726, 16.54184471, 14.63645071, 12.7130399 , 16.35785415])
In [31]:
plt.scatter(x,y)
Out[31]:
<matplotlib.collections.PathCollection at 0x24151789fd0>
In [32]:
# define a function
def lin_func(x, a, b):
return x*a + b
In [33]:
p, cov = curve_fit(lin_func, x, y )
In [34]:
p, cov
Out[34]:
(array([0.55000317, 9.71252871]), array([[ 0.0179677 , -0.08983852], [-0.08983852, 0.61389653]]))
In [35]:
y_fit = lin_func(x, p[0], p[1])
In [36]:
plt.scatter(x,y, marker = "o", c = 'goldenrod')
plt.plot(x,y_fit, c= 'seagreen')
plt.show()
In [37]:
# set equally spaced axes
fig ,ax = plt.subplots()
ax.set_aspect('equal')
# the same as last plot
plt.scatter(x,y, marker = "o", c = 'goldenrod')
plt.plot(x,y_fit, c= 'seagreen')
plt.show()
In [44]:
fig ,ax= plt.subplots( figsize=(15,15))
ax.set_aspect('equal')
plt.title("Linear regression\n", fontsize=25)
plt.scatter(x,y, color="teal", s=40,label = 'Input values')
plt.plot(x,y_fit, color='orange',linewidth=2, label = 'Curve fit' )
plt.legend(loc = 'lower right')
plt.xlabel('\nvalues x', fontsize=18)
plt.ylabel('values y\n', fontsize=18)
plt.grid(axis='y', color='lightgrey', alpha = .4)
plt.grid(axis='x', color='lightgrey')
ax.set_xlim(-2,12)
ax.set_ylim(8,18)
ax.set_yticks(np.arange(8,18, 2))
ax.set_xticks(np.arange(-2,13, 2))
plt.savefig( 'Exemplo_reg_lin' + '.png', format ='png', bbox_inches='tight')
In [ ]: