Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 2 - Regression/Support Vector Regression (SVR)/svr.py
1009 views
1
# SVR
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
sc_y = StandardScaler()
21
X = sc_X.fit_transform(X)
22
y = sc_y.fit_transform(y)
23
24
# Fitting SVR to the dataset
25
from sklearn.svm import SVR
26
regressor = SVR(kernel = 'rbf')
27
regressor.fit(X, y)
28
29
# Predicting a new result
30
y_pred = regressor.predict(6.5)
31
y_pred = sc_y.inverse_transform(y_pred)
32
33
# Visualising the SVR results
34
plt.scatter(X, y, color = 'red')
35
plt.plot(X, regressor.predict(X), color = 'blue')
36
plt.title('Truth or Bluff (SVR)')
37
plt.xlabel('Position level')
38
plt.ylabel('Salary')
39
plt.show()
40
41
# Visualising the SVR results (for higher resolution and smoother curve)
42
X_grid = np.arange(min(X), max(X), 0.01) # choice of 0.01 instead of 0.1 step because the data is feature scaled
43
X_grid = X_grid.reshape((len(X_grid), 1))
44
plt.scatter(X, y, color = 'red')
45
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
46
plt.title('Truth or Bluff (SVR)')
47
plt.xlabel('Position level')
48
plt.ylabel('Salary')
49
plt.show()
50