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()
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()
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()
from sklearn.linear_model import LinearRegression
model = LinearRegression().fit(cars['speed'].values.reshape(-1,1), cars['dist'].values.reshape(-1,1))
print(model.intercept_)
print(model.coef_)
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 para parar um carro')
plt.xlabel('Velocidade')
plt.ylabel('Distância')
plt.plot(cars['speed'], y_predicted, color='r')
plt.show()
y_predicted = cars['speed'].apply(lambda x : 42.3 + 0 * x)
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()
print(model.intercept_)
print(model.coef_)
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)
print(rmse)
print(r2)
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'])
print(model.intercept_)
print(model.coef_)
y_predicted = model.predict(boston['data'])
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))
import numpy as np
df1 = pd.DataFrame(data = np.c_[boston['target'], y_predicted], columns = ['real','predicted'])
df1.head()
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)
print(y)
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_)
y_pred = model.predict(x)
print('predicted response:', y_pred, sep='\n')
y_pred = model.intercept_ + model.coef_ * x
print('predicted response:', y_pred, sep='\n')
plt.plot(x, y, 'ro')
plt.plot(x, y_pred, color='b')
plt.show()
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])
transformer = PolynomialFeatures(degree=2, 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()
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()