Created
August 22, 2020 02:06
-
-
Save tharunpeddisetty/3fe7c29e9e56c3e17eb41a376e666083 to your computer and use it in GitHub Desktop.
Implementing Support Vector Regression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing the libraries | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
# Importing the dataset | |
dataset = pd.read_csv('/Users/tharunpeddisetty/Position_Salaries.csv') | |
X = dataset.iloc[:,1:-1].values | |
y = dataset.iloc[:, -1].values | |
#Feature Scaling. Required for SVR. Since there's no concept of coefficients | |
print(y) | |
#we need to reshape y because standard scaler class expects a 2D array | |
y=y.reshape(len(y),1) | |
from sklearn.preprocessing import StandardScaler | |
sc_X = StandardScaler() | |
sc_y = StandardScaler() | |
X= sc_X.fit_transform(X) | |
# create a new sc object because the first one calcualtes the mean and SD of X. We need different values for Y | |
y= sc_y.fit_transform(y) | |
print(y) | |
#Trainig SVR model | |
from sklearn.svm import SVR | |
regressor = SVR(kernel = 'rbf') | |
regressor.fit(X, y) | |
#Predicting the result. WE also need to inverse transform to order to get the final result | |
sc_y.inverse_transform(regressor.predict(sc_X.transform([[6.5]]))) | |
#Visualizing the SVR Results | |
#Inverse scaling in order to plot the original points | |
plt.scatter(sc_X.inverse_transform(X),sc_y.inverse_transform(y),c='red') | |
plt.plot(sc_X.inverse_transform(X),sc_y.inverse_transform(regressor.predict(X)), c='blue') #X is already scaled, so no need to transform in predict | |
plt.title('Support Vector Regression') | |
plt.xlabel("Position Level") | |
plt.ylabel('Salary') | |
plt.show() | |
#Visualizing the SVR results with higher resolution | |
X_grid = np.arange(min(sc_X.inverse_transform(X)), max(sc_X.inverse_transform(X)), 0.1) | |
X_grid = X_grid.reshape((len(X_grid), 1)) | |
plt.scatter(sc_X.inverse_transform(X), sc_y.inverse_transform(y), color = 'red') | |
plt.plot(X_grid, sc_y.inverse_transform(regressor.predict(sc_X.transform(X_grid))), color = 'blue') | |
plt.title('Support Vector Regression') | |
plt.xlabel('Position level') | |
plt.ylabel('Salary') | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Position | Level | Salary | |
---|---|---|---|
Business Analyst | 1 | 45000 | |
Junior Consultant | 2 | 50000 | |
Senior Consultant | 3 | 60000 | |
Manager | 4 | 80000 | |
Country Manager | 5 | 110000 | |
Region Manager | 6 | 150000 | |
Partner | 7 | 200000 | |
Senior Partner | 8 | 300000 | |
C-level | 9 | 500000 | |
CEO | 10 | 1000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment