Skip to content

Commit 836415f

Browse files
author
Shimanto
authored
Add files via upload
1 parent 044ec42 commit 836415f

6 files changed

Lines changed: 440 additions & 0 deletions
57.2 KB
Loading

Polynomial Regression with Python & R/Polynomial regression with Python.ipynb

Lines changed: 285 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Position,Level,Salary
2+
Business Analyst,1,45000
3+
Junior Consultant,2,50000
4+
Senior Consultant,3,60000
5+
Manager,4,80000
6+
Country Manager,5,110000
7+
Region Manager,6,150000
8+
Partner,7,200000
9+
Senior Partner,8,300000
10+
C-level,9,500000
11+
CEO,10,1000000
98.8 KB
Loading
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Polynomial Regression
2+
3+
# Importing the dataset
4+
dataset = read.csv('Position_Salaries.csv')
5+
dataset = dataset[2:3]
6+
7+
# Splitting the dataset into the Training set and Test set
8+
# # install.packages('caTools')
9+
# library(caTools)
10+
# set.seed(123)
11+
# split = sample.split(dataset$Salary, SplitRatio = 2/3)
12+
# training_set = subset(dataset, split == TRUE)
13+
# test_set = subset(dataset, split == FALSE)
14+
15+
# Feature Scaling
16+
# training_set = scale(training_set)
17+
# test_set = scale(test_set)
18+
19+
# Fitting Linear Regression to the dataset
20+
lin_reg = lm(formula = Salary ~ .,
21+
data = dataset)
22+
23+
# Fitting Polynomial Regression to the dataset
24+
dataset$Level2 = dataset$Level^2
25+
dataset$Level3 = dataset$Level^3
26+
dataset$Level4 = dataset$Level^4
27+
poly_reg = lm(formula = Salary ~ .,
28+
data = dataset)
29+
30+
# Visualising the Linear Regression results
31+
# install.packages('ggplot2')
32+
library(ggplot2)
33+
ggplot() +
34+
geom_point(aes(x = dataset$Level, y = dataset$Salary),
35+
colour = 'red') +
36+
geom_line(aes(x = dataset$Level, y = predict(lin_reg, newdata = dataset)),
37+
colour = 'blue') +
38+
ggtitle('Truth or Bluff (Linear Regression)') +
39+
xlab('Level') +
40+
ylab('Salary')
41+
42+
# Visualising the Polynomial Regression results
43+
# install.packages('ggplot2')
44+
library(ggplot2)
45+
ggplot() +
46+
geom_point(aes(x = dataset$Level, y = dataset$Salary),
47+
colour = 'red') +
48+
geom_line(aes(x = dataset$Level, y = predict(poly_reg, newdata = dataset)),
49+
colour = 'blue') +
50+
ggtitle('Truth or Bluff (Polynomial Regression)') +
51+
xlab('Level') +
52+
ylab('Salary')
53+
54+
# Visualising the Regression Model results (for higher resolution and smoother curve)
55+
# install.packages('ggplot2')
56+
library(ggplot2)
57+
x_grid = seq(min(dataset$Level), max(dataset$Level), 0.1)
58+
ggplot() +
59+
geom_point(aes(x = dataset$Level, y = dataset$Salary),
60+
colour = 'red') +
61+
geom_line(aes(x = x_grid, y = predict(poly_reg,
62+
newdata = data.frame(Level = x_grid,
63+
Level2 = x_grid^2,
64+
Level3 = x_grid^3,
65+
Level4 = x_grid^4))),
66+
colour = 'blue') +
67+
ggtitle('Truth or Bluff (Polynomial Regression)') +
68+
xlab('Level') +
69+
ylab('Salary')
70+
71+
# Predicting a new result with Linear Regression
72+
predict(lin_reg, data.frame(Level = 6.5))
73+
74+
# Predicting a new result with Polynomial Regression
75+
predict(poly_reg, data.frame(Level = 6.5,
76+
Level2 = 6.5^2,
77+
Level3 = 6.5^3,
78+
Level4 = 6.5^4))
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Polynomial 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('Position_Salaries.csv')
10+
X = dataset.iloc[:, 1:2].values
11+
y = dataset.iloc[:, 2].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 = 0.2, 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+
23+
24+
from sklearn.linear_model import LinearRegression
25+
lin_reg = LinearRegression()
26+
lin_reg.fit(X, y)
27+
28+
# Fitting Polynomial Regression to the dataset
29+
from sklearn.preprocessing import PolynomialFeatures
30+
poly_reg = PolynomialFeatures(degree = 4)
31+
X_poly = poly_reg.fit_transform(X)
32+
poly_reg.fit(X_poly, y)
33+
lin_reg_2 = LinearRegression()
34+
lin_reg_2.fit(X_poly, y)
35+
36+
# Visualising the Linear Regression results
37+
plt.scatter(X, y, color = 'red')
38+
plt.plot(X, lin_reg.predict(X), color = 'blue')
39+
plt.title('Truth or Bluff (Linear Regression)')
40+
plt.xlabel('Position level')
41+
plt.ylabel('Salary')
42+
plt.show()
43+
44+
# Visualising the Polynomial Regression results
45+
plt.scatter(X, y, color = 'red')
46+
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'blue')
47+
plt.title('Truth or Bluff (Polynomial Regression)')
48+
plt.xlabel('Position level')
49+
plt.ylabel('Salary')
50+
plt.show()
51+
52+
# Visualising the Polynomial Regression results (for higher resolution and smoother curve)
53+
X_grid = np.arange(min(X), max(X), 0.1)
54+
X_grid = X_grid.reshape((len(X_grid), 1))
55+
plt.scatter(X, y, color = 'red')
56+
plt.plot(X_grid, lin_reg_2.predict(poly_reg.fit_transform(X_grid)), color = 'blue')
57+
plt.title('Truth or Bluff (Polynomial Regression)')
58+
plt.xlabel('Position level')
59+
plt.ylabel('Salary')
60+
plt.show()
61+
62+
# Predicting a new result with Linear Regression
63+
lin_reg.predict(6.5)
64+
65+
# Predicting a new result with Polynomial Regression
66+
lin_reg_2.predict(poly_reg.fit_transform(6.5))

0 commit comments

Comments
 (0)