-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLinearRegression.py
More file actions
51 lines (40 loc) · 1.24 KB
/
SimpleLinearRegression.py
File metadata and controls
51 lines (40 loc) · 1.24 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
40
41
42
43
44
45
46
47
48
49
50
51
# Simple Linear Regression
#
# **y = b0 + b1*x1**
#
#- y = dependent variable
#- x = independent variable
#- b1 = coefficient ( a unit change in x1 and its effect in y )-slope of the line
#- b0 = constant ( point where line crosses the vertical axes ) when x1 is 0
#
# **Finds a line with minimum sum of error**
#
# SUM(y - y')^2
#DATA PREPROCESSING
#IMPORTING THE DATASETS
#numpy is a mathematicaltool
import numpy as np
#matplotlib for visualization
import matplotlib.pyplot as plt
#panads for managing dataset
import pandas as pd
#< --- IMPORTING THE DATASET ---->
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:,0].values
y = dataset.iloc[:,1].values
#splitting data into train, test, split
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)
X_train= X_train.reshape(-1, 1)
y_train= y_train.reshape(-1, 1)
X_test = X_test.reshape(-1, 1)
y_test = y_test.reshape(-1, 1)
#training the SIMPLE LINEAR REGRESSION model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train,y_train)
#predicting
y_pred = regressor.predict(X_test)
#plotting the values
plt.scatter(X_train, y_train)
plt.plot(X_test, y_pred)