SVM (Support Vector Machine) Regression

SVM (Support Vector Machine) Regression

Just tried SVM to regress.

Code


import numpy as np
import matplotlib.pyplot as plt
# y = sin(x) + noise
np.random.seed(1)
x = np.random.uniform(0, 2*np.pi,200)
y = np.sin(x) + np.random.normal(loc=0, scale=0.5, size=len(x))
# change the shape of x for scikit-learn
x = x.reshape((len(x),1))

from sklearn import svm
from sklearn import cross_validation
# take 20% for test and use 70% as training data
x_train, x_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size=0.2)
# sort x_test and y_test to plota line graph
index = x_test.argsort(0).reshape(len(x_test))
x_test = x_test[index]
y_test = y_test[index]
# train a support vector machine
reg = svm.SVR(kernel='rbf', C=1).fit(x_train, y_train)
# the coefficient of determination R^2 of the prediction
print('The coefficient of determination R^2 is {0}'.format(reg.score(x_test, y_test)))
# plot
fig, ax = plt.subplots(1)
ax.plot(x_test, y_test, 'bo')
ax.plot(x_test, reg.predict(x_test), 'ro-', label='$R^2={0}$'.format(round(reg.score(x_test, y_test),2)))
ax.legend()
fig.savefig('SupportVectorRegression')
fig.show()

The coefficient of determination R^2 is 0.76986793941


Bibliography:


コメント

人気の投稿