Wednesday, December 4, 2019

Understanding Simple Linear Regression, Multiple Linear Regression and Polynomial Regression - ML


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd                                                                                      
dataset = pd.read_csv('D:\SEM 7\ML\data.csv')
X = dataset.iloc[:, :-1].values
Y = dataset.iloc[:, 1].values
fromsklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 1/3, random_state = 0)
fromsklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)
fromsklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, Y_train)
Y_pred=regressor.predict(X_test)
plt.scatter(X_train, Y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
plt.scatter(X_test, Y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()



Dataset:-

Y_test and Y_train :-



Variable Explorer:- 

Graph:-



No comments:

Post a Comment