-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLinearRegression.py
More file actions
41 lines (32 loc) · 1.23 KB
/
Copy pathSimpleLinearRegression.py
File metadata and controls
41 lines (32 loc) · 1.23 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
38
39
# Importing the libraries
import pandas as pd
import matplotlib.pyplot as plt
# Importing the dataset in current directory
dataset = pd.read_csv('SimpleLinearRegression.csv')
#import the independent variable contains all rows and all columns
#import the dependent variable: The last column
X = dataset.iloc[:, :-1].values
Y = dataset.iloc[:, 1].values
#Splitting the dataset into training and tesing
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 10)
#Train the model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, Y_train)
#Do the prediction
prediction =regressor.predict(X_test)
# Visualising 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('Years of Experience')
plt.ylabel('Salary')
plt.show()
# Visualising 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('Years of Experience')
plt.ylabel('Salary')
plt.show()