-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_linear_regression.py
43 lines (34 loc) · 1.36 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
43
# Simple Linear Regression
# Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Import dataset
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values
# Splittig the dataset into Train and Test
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)
# Feature Scaling
# None
# Fit Simple Linear Regression to Training Set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# Predicting the Test Set Results
y_pred = regressor.predict(X_test)
# Visualizing the Training Set Results
plt.scatter(X_train, y_train, color ='red') # Plotting the real values
plt.plot(X_train, regressor.predict(X_train), color = 'blue') # y-values are the prediction of the X_train years of experience
plt.title("Salary vs Experience (Training Set)")
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
# Visualizing the Test Set Results
plt.scatter(X_test, y_test, color ='red') # Plotting the real values
plt.plot(X_train, regressor.predict(X_train), color = 'blue') # y-values are the prediction of the X_train years of experience
plt.title("Salary vs Experience (Test Set)")
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()