Linear and Poly regressor

 # -*- coding: utf-8 -*-

"""

Created on Mon Oct 23 13:17:55 2023


@author: Syed Kamran Bukhari

"""


import numpy as np

import pandas as pd

import matplotlib.pyplot as plt


#import CSV file

dataset= pd.read_csv('Position_Salaries.csv')


X= dataset.iloc[:,1:2].values

Y=dataset.iloc[:,-1].values


#regressor model

from sklearn.linear_model import LinearRegression

Linear_regressor = LinearRegression()

Linear_regressor.fit(X,Y)


#Slope and Intercept

print('The Slope is = ', Linear_regressor.coef_)

print('The X intercept is = ',Linear_regressor.intercept_)


from sklearn.preprocessing import PolynomialFeatures

poly_reg =PolynomialFeatures(degree=4)

X_poly = poly_reg.fit_transform(X)

poly_reg1 =LinearRegression()

poly_reg1.fit(X_poly, Y)


#linear Regressor Plot

plt.scatter(X, Y, color='red')

plt.plot(X,Linear_regressor.predict(X),color = 'blue')

plt.plot(X,poly_reg1.predict(X_poly), color = 'purple')

plt.xlabel('Position')

plt.ylabel('Salary')

plt.legend(['Data','Linear','Poly'])

plt.title('Position Vs Salary')

plt.show()



X_grid = np.arange(min(X),max(X), 0.1)

X_grid = X_grid.reshape(len(X_grid),1)

xpoly1=poly_reg.fit_transform(X_grid)

plt.scatter(X,Y, color='red')

plt.plot(X_grid, poly_reg1.predict(xpoly1), color ='blue')

plt.xlabel('Position level')

plt.ylabel('Salary')

plt.title('Truth or Bluff')

plt.show()


#predicting Values through Liear Regression

Y_pred_lin= Linear_regressor.predict([[6.5]])

print('Salary is = ',Y_pred_lin)


#predicting values through Poly Regression

Y_pred_poly= poly_reg1.predict(poly_reg.fit_transform([[6.5]]))

print('Salary is = ',Y_pred_poly)



print("\n")

#RMSE score

residual = abs(Y)- abs(Linear_regressor.predict(X))

ss= residual**2

ss=ss.sum()

import math

RMSE=math.sqrt(ss)

print('The RMSE score is =', RMSE)


#R2 score

from sklearn.metrics import r2_score

R2= r2_score(Y, Linear_regressor.predict(X))

print('The R2 score is =',R2)


#Durbin Watson Statistics

from statsmodels.stats.stattools import durbin_watson

DW = durbin_watson(residual)

print('Durbin Watson Statistics =',DW)



print("\n")


#for poly 

#RMSE score

residual = abs(Y)- abs(poly_reg1.predict(X_poly))

ss= residual**2

ss=ss.sum()

import math

RMSE=math.sqrt(ss)

print('The RMSE score is =', RMSE)


#R2 score

from sklearn.metrics import r2_score

R2= r2_score(Y, poly_reg1.predict(X_poly))

print('The R2 score is =',R2)


#Durbin Watson Statistics

from statsmodels.stats.stattools import durbin_watson

DW = durbin_watson(residual)

print('Durbin Watson Statistics =',DW)

Comments

Popular posts from this blog

PAKISTAN ANSWERS BACK TO INDIA

PRIME MINISTER OF PAKISTAN WINNING THE HEARTS OF THE POOR PAKISTANI PEOPLE.

LOCAL GOVERMENT SYSTEM OF ISLAMABAD THE CAPITAL