# How to calculate RSE, MAE, RMSE, R-square in python

Take same sales data from previous python example

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error

plt.rcParams['figure.figsize'] = (12.0, 9.0)
data = pd.read_csv('sales.csv')  # load data set

X = data['Advertising'].values.reshape(-1, 1)  # values converts it into a numpy array
Y = data['Sales'].values.reshape(-1, 1)  # -1 means that calculate the dimension of rows, but have 1 column

linear_regressor = LinearRegression()  # create object for the class
linear_regressor.fit(X, Y)  # perform linear regression
plt.show()

print('Coefficient is:',linear_regressor.coef_)
print('Intercept is:',linear_regressor.intercept_)

num_data = X.shape[0]
print('num_data::',num_data)    # number of records- 4
Y_pred = linear_regressor.predict(X)

mse = mean_squared_error(Y,Y_pred)
rmse = math.sqrt(mse/num_data)
rse = math.sqrt(mse/(num_data-2))
rsquare=linear_regressor.score(X,Y)
mae=mean_absolute_error(Y,Y_pred)

print('RSE=',rse)
print('R-Square=',rsquare)
print('rmse=',rmse)
print('mae=',mae)
Here is the output.
Coefficient is: [[6.23809524]]
Intercept is: [476.9047619]
num_data:: 4
RSE= 11.273124382057263
R-Square= 0.9723497081987647
rmse= 7.971302695712098
mae= 14.047619047619094