In [199]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

In [200]:
np.set_printoptions(precision=3, suppress=True)

Importing a dataset


In [201]:
dataset = pd.read_csv('Position_Salaries.csv')

In [202]:
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, -1].values

Feature Scaling


In [203]:
from sklearn.preprocessing import StandardScaler

In [204]:
sc_X = StandardScaler()
X = sc_X.fit_transform(X)
sc_y = StandardScaler()
y = sc_y.fit_transform(y.reshape(-1, 1))[:, 0]


/usr/local/opt/pyenv/versions/3.6.0/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/validation.py:429: DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.
  warnings.warn(msg, _DataConversionWarning)

Fitting SVR to the dataset


In [205]:
from sklearn.svm import SVR

In [206]:
regressor = SVR(kernel='rbf')
regressor.fit(X, y)


Out[206]:
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
  kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)

Predicting a new result


In [207]:
sc_y.inverse_transform(
    regressor.predict(
        sc_X.transform(np.array([[6.5]]))
    )
)


Out[207]:
array([ 170370.02])

Visualising the SVR results


In [208]:
plt.scatter(X, y, color='red')
plt.plot(X, regressor.predict(X), color='blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()


Visualising the SVR results (for higher resolution and smoother curve)


In [209]:
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
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color='red')
plt.plot(X_grid, regressor.predict(X_grid), color='blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()