"""
 # implimenting Multiple Linear Regression using matrix operation, OLS and Gradient Descent method...   
 
 Now in this notebook we will see three different methods for implimenting MLR with the data of Student Marks
Here the task is predicting End semester exam marks(ESE) on the basis of students Mid semester exam marks(MSE) and his Attendence(Attendance) 

1) Dependent Variable y is ESE

2) Independent Variables are: Attendance (x1), and MSE(x2)

3) One more input feature is added called HRS:->NO. of hour a student studied a day before exam(0HRS to 24HRS)


"""
"""
**Multivarient Regression using Matrix operation:**
"""
"""
**implementing Libraries:**

        Here seaborn and matlot libraries implemented for the Data Visualization and statmodels for implenting MLR.   
"""
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import statsmodels.api as sm 
import math

df = pd.read_csv("../input/predicting-ese-marks/amsPredictionSheet1-201009-150447.csv")
df.head()
df.describe()
"""
Correlation analysis signifies the relation between differeent random variable.
"""
corr=df.corr()
corr.style.background_gradient(cmap='coolwarm')
"""
now we will seperate input vectors from output vectors and add one constant unit vector for the value of bita zero.
"""
endog = df['ESE']
exog = sm.add_constant(df[['MSE','Attendance','HRS']])
print(exog)
"""
To do the matrx operation we have to convert the data frame into numpy array  in whiich we can perform matrix operation. 
here we have converted the input vectore as numpy array in x and output vector n y.
for getting the transposee of the amtricx we have method called np.transpose().
"""
x = exog.to_numpy()
y = endog.to_numpy()
s1_xt=np.transpose(x)
print(s1_xt)
s2_mull=np.matmul(s1_xt,x)
print(s2_mull)
"""
next step is calculate inverse of the  if the result for that numpy provide one method called np.linalg.inv()
"""
s3_inv = np.linalg.inv(s2_mull)
print(s3_inv)
s4_mul = np.matmul(s3_inv,s1_xt)
print(s4_mul)
s5_res = np.matmul(s4_mul,y)
print(s5_res)
"""
**The following code segment build the MLR model using the OLS method from the statsmodel**
"""
mod = sm.OLS(endog, exog)
results = mod.fit()
print (results.summary())
def RSE(y_true, y_predicted):
   
    y_true = np.array(y_true)
    y_predicted = np.array(y_predicted)
    RSS = np.sum(np.square(y_true - y_predicted))

    rse = math.sqrt(RSS / (len(y_true) - 2))
    return rse
yp= results.predict()
ypa = np.array(yp)
yta = df['ESE']
eterms =yta-ypa


df1 = pd.DataFrame(eterms)
df1['ESE'].hist(bins=10)
rse= RSE(df['ESE'],results.predict())
print(rse)

"""
**The following code segment builds the MLR model from Scikit-Learn module**
"""
from sklearn import linear_model
X = df[['MSE','Attendance','HRS']]
y = df['ESE']

lm = linear_model.LinearRegression()
model = lm.fit(X,y)
lm.coef_
lm.intercept_