1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import pandas as pd data = pd.read_csv("data.csv") data.head()
x = data.loc[:,'x'] y = data.loc[:,'y'] print(x,y)
print(type(x),x.shape) print(type(y),y.shape)
from matplotlib import pyplot as plt plt.figure(figsize=(2,2)) plt.scatter(x,y) plt.show()
from sklearn.linear_model import LinearRegression lr_model = LinearRegression()
import numpy as np x = np.array(x) x = x.reshape(-1,1) y = np.array(y) y = y.reshape(-1,1) print(type(x),x.shape) print(type(y),y.shape)
lr_model.fit(x,y)
y_predict = lr_model.predict(x) print(y_predict)
a = lr_model.coef_ b = lr_model.intercept_ print(a,b)
from sklearn.metrics import mean_squared_error,r2_score MSE = mean_squared_error(y,y_predict) R2 = r2_score(y,y_predict) print(MSE,R2)
plt.figure() plt.plot(y,y_predict) plt.show()
|