import matplotlib.pyplot as plt
from sklearn import linear_model
import numpy as np
from sklearn.metrics import mean_squared_error, r2_score
reg = linear_model.LinearRegression()
ar = np.array([[[1],[2],[3]], [[2.01],[4.03],[6.04]]])
y = ar[1,:]
x = ar[0,:]
print(‘x’,‘y’,x,y)
reg.fit(x,y)
print(‘Coefficients: \n’, reg.coef_)
xTest = np.array([[4],[5],[6]])
ytest = np.array([[9],[8.5],[14]])
preds = reg.predict(xTest)
print(“R2 score : %.2f” % r2_score(ytest,preds))
print(“Mean squared error: %.2f” % mean_squared_error(ytest,preds))
er = []
g = 0
for i in range(len(ytest)):
print( “actual=”, ytest[i], " observed=", preds[i])
x = (ytest[i] - preds[i]) **2
er.append(x)
g = g + x
x = 0
for i in range(len(er)):
x = x + er[i]
print (“MSE”, x / len(er))
v = np.var(er)
print (“variance”, v)
print ("average of errors ", np.mean(er))
m = np.mean(ytest)
print (“average of observed values”, m)
y = 0
for i in range(len(ytest)):
y = y + ((ytest[i] - m) ** 2)
print (“total sum of squares”, y)
print ("ẗotal sum of residuals ", g)
print (“r2 calculated”, 1 - (g / y))