Path: blob/master/Part 2 - Regression/Support Vector Regression (SVR)/svr.py
1009 views
# SVR12# Importing the libraries3import numpy as np4import matplotlib.pyplot as plt5import pandas as pd67# Importing the dataset8dataset = pd.read_csv('Position_Salaries.csv')9X = dataset.iloc[:, 1:2].values10y = dataset.iloc[:, 2].values1112# Splitting the dataset into the Training set and Test set13"""from sklearn.cross_validation import train_test_split14X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)"""1516# Feature Scaling17from sklearn.preprocessing import StandardScaler18sc_X = StandardScaler()19sc_y = StandardScaler()20X = sc_X.fit_transform(X)21y = sc_y.fit_transform(y)2223# Fitting SVR to the dataset24from sklearn.svm import SVR25regressor = SVR(kernel = 'rbf')26regressor.fit(X, y)2728# Predicting a new result29y_pred = regressor.predict(6.5)30y_pred = sc_y.inverse_transform(y_pred)3132# Visualising the SVR results33plt.scatter(X, y, color = 'red')34plt.plot(X, regressor.predict(X), color = 'blue')35plt.title('Truth or Bluff (SVR)')36plt.xlabel('Position level')37plt.ylabel('Salary')38plt.show()3940# Visualising the SVR results (for higher resolution and smoother curve)41X_grid = np.arange(min(X), max(X), 0.01) # choice of 0.01 instead of 0.1 step because the data is feature scaled42X_grid = X_grid.reshape((len(X_grid), 1))43plt.scatter(X, y, color = 'red')44plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')45plt.title('Truth or Bluff (SVR)')46plt.xlabel('Position level')47plt.ylabel('Salary')48plt.show()4950