In [199]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [200]:
np.set_printoptions(precision=3, suppress=True)
In [201]:
dataset = pd.read_csv('Position_Salaries.csv')
In [202]:
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, -1].values
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]
In [205]:
from sklearn.svm import SVR
In [206]:
regressor = SVR(kernel='rbf')
regressor.fit(X, y)
Out[206]:
In [207]:
sc_y.inverse_transform(
regressor.predict(
sc_X.transform(np.array([[6.5]]))
)
)
Out[207]:
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()
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()