scipy
In [3]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
In [11]:
Copied!
x = np.linspace(0,10, 21)
x
x = np.linspace(0,10, 21)
x
Out[11]:
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 [49]:
Copied!
y = 3*x+4
y
y = 3*x+4
y
Out[49]:
array([ 4. , 5.5, 7. , 8.5, 10. , 11.5, 13. , 14.5, 16. , 17.5, 19. , 20.5, 22. , 23.5, 25. , 26.5, 28. , 29.5, 31. , 32.5, 34. ])
In [73]:
Copied!
fig = plt.figure()
plt.scatter(x,y)
plt.plot(x,y)
fig = plt.figure()
plt.scatter(x,y)
plt.plot(x,y)
Out[73]:
[<matplotlib.lines.Line2D at 0x13217768520>]
In [53]:
Copied!
y1 = np.random.uniform(low=-1, high=1, size=y.shape)*3
y1 = y1 +y
y1
y1 = np.random.uniform(low=-1, high=1, size=y.shape)*3
y1 = y1 +y
y1
Out[53]:
array([ 5.31833371, 5.76020449, 8.87245009, 6.3823421 , 7.52872377, 9.24035315, 15.9268918 , 15.97524007, 14.99819733, 15.2770018 , 18.33654815, 20.84173274, 24.40244943, 24.57254997, 24.25786796, 28.44592888, 25.24881334, 28.16143712, 30.1461995 , 31.01584478, 34.61264811])
In [54]:
Copied!
plt.scatter(x,y1)
plt.scatter(x,y1)
Out[54]:
<matplotlib.collections.PathCollection at 0x1321592bfa0>
In [56]:
Copied!
p, cov = optimize.curve_fit(lambda x, m, l: x*m+l, x, y)
print(p, cov)
p, cov = optimize.curve_fit(lambda x, m, l: x*m+l, x, y)
print(p, cov)
[3. 4.] [[ 0. -0.] [-0. 0.]]
In [57]:
Copied!
p, cov = optimize.curve_fit(lambda x, m, l: x*m+l, x, y1)
print(p, cov)
p, cov = optimize.curve_fit(lambda x, m, l: x*m+l, x, y1)
print(p, cov)
[2.93175338 4.16607872] [[ 0.01657997 -0.08289987] [-0.08289987 0.56648246]]
In [92]:
Copied!
y_fit = x*p[0] + p[1]
plt.figure(1, figsize=(5,5))
plt.scatter(x,y1, color="teal")
plt.plot(x,y_fit, color='orange' )
y_fit = x*p[0] + p[1]
plt.figure(1, figsize=(5,5))
plt.scatter(x,y1, color="teal")
plt.plot(x,y_fit, color='orange' )
Out[92]:
[<matplotlib.lines.Line2D at 0x1321929b9a0>]
In [131]:
Copied!
y_fit = x*p[0] + p[1]
fig ,ax= plt.subplots( figsize=(5,10))
plt.title("Regrassão linear", fontsize=25)
plt.scatter(x,y1, color="teal", s=30,label = 'Valores observados')
plt.plot(x,y_fit, color='orange',linewidth=1, label = 'Ajuste de função' )
plt.legend(loc = 'upper left')
plt.xlabel('valores x', fontsize=18)
plt.ylabel('valores y', fontsize=18)
#ax.set_aspect('equal')
plt.grid(axis='y', color='lightgrey')
plt.grid(axis='x', color='lightgrey')
plt.savefig( 'Exemplo_reg_lin' + '.png', format ='png', bbox_inches='tight')
y_fit = x*p[0] + p[1]
fig ,ax= plt.subplots( figsize=(5,10))
plt.title("Regrassão linear", fontsize=25)
plt.scatter(x,y1, color="teal", s=30,label = 'Valores observados')
plt.plot(x,y_fit, color='orange',linewidth=1, label = 'Ajuste de função' )
plt.legend(loc = 'upper left')
plt.xlabel('valores x', fontsize=18)
plt.ylabel('valores y', fontsize=18)
#ax.set_aspect('equal')
plt.grid(axis='y', color='lightgrey')
plt.grid(axis='x', color='lightgrey')
plt.savefig( 'Exemplo_reg_lin' + '.png', format ='png', bbox_inches='tight')