-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simple_Linear_regression.py
42 lines (36 loc) · 1.3 KB
/
Simple_Linear_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#import the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
#import datasets
dataset=pd.read_csv('Salary_Data.csv')
X=dataset.iloc[:, :-1].values
Y=dataset.iloc[:,1].values
#splitting the dataset into the teaining set and test set
from sklearn.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)
#feaature Scaling
from sklearn.preprocessing import StandardScaler
sc_X=StandardScaler()
X_train=sc_X.fit_transform(X_train)
X_test=sc_X.transform(X_test)
#Fitting simple linear Regression to the training set
from sklearn.linear_model import LinearRegression
regressor=LinearRegression()
regressor.fit(X_train,Y_train)
#predicting the Test set result
Y_pred=regressor.predict(X_test)
#visulaization the training set results
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('Year of experience ')
plt.ylabel('Salary')
plt.show()
#visulaization the test set results
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('Year of experience ')
plt.ylabel('Salary')
plt.show()