In [3]:
import pandas as pd
cars = pd.read_csv("cars.csv") 
import matplotlib.pyplot as plt
cars.plot ( kind ='scatter', x='speed', y='dist',style ='o')
plt.title("Relação entre velocidade e distância")
plt.xlabel('Velocidade')
plt.ylabel('Distância')
plt.show()
In [49]:
y_predicted = cars ['speed'].apply(lambda x : 42.3 + 0 * x)
cars.plot(kind ='scatter', x='speed', y='dist',style ='')
plt.title('Relação entre velocidade e distância')
plt.xlabel('Velocidade')
plt.ylabel('Distância')
plt.plot( cars ['speed'] , y_predicted , color ='#00ff00')
plt.show()
In [16]:
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(
    cars['speed'].values.reshape( -1 ,1),
    cars['dist'].values.reshape( -1 ,1)
)
y_predicted = model.predict(cars['speed'].values.reshape ( -1 ,1))
cars.plot( kind ='scatter', x='speed', y='dist',style ='o')
plt.title('Relação entre velocidade e distância')
plt.xlabel('Velocidade')
plt.ylabel('Distância')
plt.plot(cars['speed'], y_predicted, color ='r')
plt.show()
In [17]:
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(cars['speed'].values.reshape(-1,1), cars['dist'].values.reshape(-1,1))
In [18]:
print(model.intercept_)
print(model.coef_)
[-17.57909489]
[[3.93240876]]
In [19]:
y_predicted = model.predict(cars['speed'].values.reshape(-1,1))
In [20]:
cars.plot(kind='scatter', x='speed', y='dist', style='o')  
plt.title('Relação entre velocidade e distância para parar um carro')  
plt.xlabel('Velocidade')  
plt.ylabel('Distância')
plt.plot(cars['speed'], y_predicted, color='r')
plt.show()
In [21]:
y_predicted = cars['speed'].apply(lambda x : 42.3 + 0 * x)
In [22]:
cars.plot(kind='scatter', x='speed', y='dist', style='o')  
plt.title('Relação entre velocidade e distância para parar um carro')  
plt.xlabel('Velocidade')  
plt.ylabel('Distância')  
plt.plot(cars['speed'], y_predicted, color='r')
plt.show()
In [23]:
print(model.intercept_)
print(model.coef_)
[-17.57909489]
[[3.93240876]]
In [24]:
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
rmse = mean_squared_error(cars['dist'], y_predicted)
r2 = r2_score(cars['dist'], y_predicted)
In [25]:
print(rmse)
print(r2)
651.242
-0.0007105324137386404
In [26]:
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
from sklearn.datasets import load_boston
boston = load_boston()
model = LinearRegression().fit(boston['data'], boston['target'])
In [27]:
print(model.intercept_)
print(model.coef_)
36.459488385089855
[-1.08011358e-01  4.64204584e-02  2.05586264e-02  2.68673382e+00
 -1.77666112e+01  3.80986521e+00  6.92224640e-04 -1.47556685e+00
  3.06049479e-01 -1.23345939e-02 -9.52747232e-01  9.31168327e-03
 -5.24758378e-01]
In [28]:
y_predicted = model.predict(boston['data'])
In [29]:
rmse = mean_squared_error(boston['target'], y_predicted)
r2 = r2_score(boston['target'], y_predicted)
print(rmse)
print(r2)
print('Mean Absolute Error:', mean_absolute_error(boston['target'], y_predicted))
21.894831181729202
0.7406426641094095
Mean Absolute Error: 3.2708628109003115
In [30]:
import numpy as np
df1 = pd.DataFrame(data = np.c_[boston['target'], y_predicted], columns = ['real','predicted'])
df1.head()
Out[30]:
real predicted
0 24.0 30.003843
1 21.6 25.025562
2 34.7 30.567597
3 33.4 28.607036
4 36.2 27.943524
In [50]:
import numpy as np
from sklearn.linear_model import LinearRegression
x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1))
y = np.array([5, 20, 14, 32, 22, 38])
print(x)
[[ 5]
 [15]
 [25]
 [35]
 [45]
 [55]]
In [51]:
print(y)
[ 5 20 14 32 22 38]
In [53]:
model = LinearRegression()
model.fit(x, y)
r_sq = model.score(x, y)
print('coefficient of determination:', r_sq)
print('intercept:', model.intercept_)
print('coeficients:', model.coef_)
coefficient of determination: 0.715875613747954
intercept: 5.633333333333329
coeficients: [0.54]
In [54]:
y_pred = model.predict(x)
print('predicted response:', y_pred, sep='\n')
predicted response:
[ 8.33333333 13.73333333 19.13333333 24.53333333 29.93333333 35.33333333]
In [55]:
y_pred = model.intercept_ + model.coef_ * x
print('predicted response:', y_pred, sep='\n')
predicted response:
[[ 8.33333333]
 [13.73333333]
 [19.13333333]
 [24.53333333]
 [29.93333333]
 [35.33333333]]
In [56]:
plt.plot(x, y, 'ro')   
plt.plot(x, y_pred, color='b')
plt.show()
In [57]:
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1))
y = np.array([15, 11, 2, 8, 25, 32])
In [58]:
transformer = PolynomialFeatures(degree=2, include_bias=False)
transformer.fit(x)
x_ = transformer.transform(x)
In [59]:
print(x_)
[[   5.   25.]
 [  15.  225.]
 [  25.  625.]
 [  35. 1225.]
 [  45. 2025.]
 [  55. 3025.]]
In [60]:
model = LinearRegression().fit(x_, y)
In [61]:
r_sq = model.score(x_, y)
print('coefficient of determination:', r_sq)
print('intercept:', model.intercept_)
print('coefficients:', model.coef_)
coefficient of determination: 0.8908516262498564
intercept: 21.372321428571425
coefficients: [-1.32357143  0.02839286]
In [62]:
y_pred = model.predict(x_)
print('predicted response:', y_pred, sep='\n')
predicted response:
[15.46428571  7.90714286  6.02857143  9.82857143 19.30714286 34.46428571]
In [63]:
plt.plot(x, y, 'ro')   
plt.plot(x, y_pred, color='b')
plt.show()
In [64]:
transformer = PolynomialFeatures(degree=4, include_bias=False)
transformer.fit(x)
x_ = transformer.transform(x)
print(x_)
model = LinearRegression().fit(x_, y)
r_sq = model.score(x_, y)
print('coefficient of determination:', r_sq)
print('intercept:', model.intercept_)
print('coefficients:', model.coef_)
y_pred = model.predict(x_)
print('predicted response:', y_pred, sep='\n')
plt.plot(x, y, 'ro')   
plt.plot(x, y_pred, color='b')
plt.show()
[[5.000000e+00 2.500000e+01 1.250000e+02 6.250000e+02]
 [1.500000e+01 2.250000e+02 3.375000e+03 5.062500e+04]
 [2.500000e+01 6.250000e+02 1.562500e+04 3.906250e+05]
 [3.500000e+01 1.225000e+03 4.287500e+04 1.500625e+06]
 [4.500000e+01 2.025000e+03 9.112500e+04 4.100625e+06]
 [5.500000e+01 3.025000e+03 1.663750e+05 9.150625e+06]]
coefficient of determination: 0.9996871368552787
intercept: 4.085503472844266
coefficients: [ 3.67175926e+00 -3.44062500e-01  9.90740741e-03 -8.54166667e-05]
predicted response:
[15.02777778 10.86111111  2.27777778  7.72222222 25.13888889 31.97222222]
In [ ]: