-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple linear regression
More file actions
37 lines (30 loc) · 1.02 KB
/
simple linear regression
File metadata and controls
37 lines (30 loc) · 1.02 KB
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
# importing dataset
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset=pd.read_csv('Salary_Data.csv')
X=dataset.iloc[:,:-1].values
Y=dataset.iloc[:,1].values
#training and testing test splitting
from sklearn.cross_validation import train_test_split
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=1/3,random_state=0)
#fitting simple linear regression to training set
from sklearn.linear_model import LinearRegression
regressor=LinearRegression()
regressor.fit(X_train,Y_train)
#predict the testing set
Y_pred=regressor.predict(X_test)
#plotting training test
plt.scatter(X_train,Y_train,color='red')
plt.plot(X_train,regressor.predict(X_train),color='blue')
plt.title('Salary vs Year of Experience(training set)')
plt.xlabel('year of experience')
plt.ylabel('salary')
plt.show()
#plotting testing test
plt.scatter(X_test,Y_test,color='red')
plt.plot(X_test,Y_pred,color='blue')
plt.title('Salary vs Year of Experience(training set)')
plt.xlabel('year of experience')
plt.ylabel('salary')
plt.show()