If you wish to us ‘rbf’ or ‘sigmoid’ in SVR, its highly recommended you standardize the dataset first.
Once you standardize/normalize data, in order to calculate Regression metrics ( Error Criteria) , do not forget to de-standardize/de-normalize the output results first, and then calculate errors.
from sklearn import neighbors
import matplotlib.pyplot as plt
x = np.arange(0.0, 1, 0.01).reshape(-1, 1)
y = np.cos(np.sin(2 * np.pi * x))
knn_reg =neighbors.KNeighborsRegressor(3, weights='uniform’)
knn_reg.fit(x, y)
test_x = np.arange(0.0, 1, 0.05).reshape(-1, 1)
test_y = knn_reg.predict(test_x)
plt.scatter(x, y, c='b' , label='real')
plt.scatter(test_x,test_y, c='r', marker="o", label='KNN Prediction')
plt.legend()
plt.show()
Exercise
SVR
Create a data set consisting of 200 random input and the output would be the sin(x) of the input. Make the inputs between 0 and 5. ‘Sort’ the input in ascending order. Add some noise to the output and then, do the followings: • SVR with ‘rbf’ kernel • SVR with ‘linear’ kernel • SVR with ‘poly’ kernel • Plot the results for each of them together with the dataset itself.
import numpy as np
import matplotlib.pyplot as plt
X = np.sort(5 * np.random.rand(200, 1), axis=0)
y = np.sin(X).ravel()
y[::5] += 3 * (0.5 - np.random.rand(40))
from sklearn.svm import SVR
svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1)
svr_lin = SVR(kernel='linear', C=1e3)
svr_poly = SVR(kernel='poly', C=1e3, degree=3)
y_rbf = svr_rbf.fit(X, y).predict(X)
y_lin = svr_lin.fit(X, y).predict(X)
y_poly = svr_poly.fit(X, y).predict(X)
lw = 2
plt.figure(figsize=(12, 7))
plt.scatter(X, y, color='darkorange', label='data')
plt.plot(X, y_rbf, color='navy', lw=lw, label='RBF model')
plt.plot(X, y_lin, color='c', lw=lw, label='Linear model')
plt.plot(X, y_poly, color='cornflowerblue', lw=lw, label='Polynomial
model') # ’poly’ Sometimes takes time to perform, be patient !!
plt.xlabel('data')
plt.ylabel('target')
plt.title('Support Vector Regression')
plt.legend()
plt.show()
MLP
For the a synthetic dataset containing 100 samples, and noisy output, create MLP regression for TWO activation function i.e. ‘Tanh’ and ‘Relu’ , plot each regression separately to compare the results. Hint: use makeregression library to create data