Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 2 - Regression/Simple Linear Regression/simple_linear_regression.py
1009 views
1
# Simple Linear Regression
2
3
# Importing the libraries
4
import numpy as np
5
import matplotlib.pyplot as plt
6
import pandas as pd
7
8
# Importing the dataset
9
dataset = pd.read_csv('Salary_Data.csv')
10
X = dataset.iloc[:, :-1].values
11
y = dataset.iloc[:, 1].values
12
13
# Splitting the dataset into the Training set and Test set
14
from sklearn.cross_validation import train_test_split
15
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)
16
17
# Feature Scaling
18
"""from sklearn.preprocessing import StandardScaler
19
sc_X = StandardScaler()
20
X_train = sc_X.fit_transform(X_train)
21
X_test = sc_X.transform(X_test)
22
sc_y = StandardScaler()
23
y_train = sc_y.fit_transform(y_train)"""
24
25
# Fitting Simple Linear Regression to the Training set
26
from sklearn.linear_model import LinearRegression
27
regressor = LinearRegression()
28
regressor.fit(X_train, y_train)
29
30
# Predicting the Test set results
31
y_pred = regressor.predict(X_test)
32
33
# Visualising the Training set results
34
plt.scatter(X_train, y_train, color = 'red')
35
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
36
plt.title('Salary vs Experience (Training set)')
37
plt.xlabel('Years of Experience')
38
plt.ylabel('Salary')
39
plt.show()
40
41
# Visualising the Test set results
42
plt.scatter(X_test, y_test, color = 'red')
43
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
44
plt.title('Salary vs Experience (Test set)')
45
plt.xlabel('Years of Experience')
46
plt.ylabel('Salary')
47
plt.show()
48