# generate data set xMin, xMax = -5, 5 np.random.seed(5) Xc = np.ones([n, 1]) Xc[:, 0] = np.linspace(xMin, xMax, n) Yc = 10 * np.sinc(Xc[:, 0]) + np.random.normal(0, 1, n) X, x, Y, y = train_test_split(Xc, Yc, test_size=0.5, random_state=0) # train rvr rvr = RVR(gamma=1, kernel='rbf') mem_us_rvr = memory_usage((rvr.fit, (X, Y)), interval=0.1) print(mem_us_rvr) t1 = time.time() rvr.fit(X, Y) t2 = time.time() rvr_err = mean_squared_error(rvr.predict(x), y) rvs = np.sum(rvr.active_) print "RVR error on test set is {0}, number of relevant vectors is {1}, time {2}".format( rvr_err, rvs, t2 - t1) # train svr svr = GridSearchCV(SVR(kernel='rbf', gamma=1), param_grid={'C': [0.001, 0.1, 1, 10, 100]}, cv=10) mem_us_svr = memory_usage((svr.fit, (X, Y)), interval=0.1) t1 = time.time() svr.fit(X, Y) t2 = time.time()
from sklearn.metrics import mean_squared_error # parameters n = 5000 # generate data set np.random.seed(0) Xc = np.ones([n, 1]) Xc[:, 0] = np.linspace(-5, 5, n) Yc = 10 * np.sinc(Xc[:, 0]) + np.random.normal(0, 1, n) X, x, Y, y = train_test_split(Xc, Yc, test_size=0.5, random_state=0) # train rvr rvm = RVR(gamma=1, kernel='rbf') t1 = time.time() rvm.fit(X, Y) t2 = time.time() y_hat, var = rvm.predict_dist(x) rvm_err = mean_squared_error(y_hat, y) rvs = np.sum(rvm.active_) print("RVM error on test set is {0}, number of relevant vectors is {1}, time {2}".format(rvm_err, rvs, t2 - t1)) # train svr svr = GridSearchCV(SVR(kernel='rbf', gamma=1), param_grid={'C': [0.001, 0.1, 1, 10, 100]}, cv=10) t1 = time.time() svr.fit(X, Y) t2 = time.time() svm_err = mean_squared_error(svr.predict(x), y) svs = svr.best_estimator_.support_vectors_.shape[0] print(print() )
y_lin = svr_lin.fit(x_val, y_val).predict(x_val) t2 = time.time() t_svr_lin = t2 - t1 print('Support Vector Regression with linear kernel takes {} s'.format( t_svr_lin)) t1 = time.time() y_poly = svr_poly.fit(x_val, y_val).predict(x_val) t2 = time.time() t_svr_poly = t2 - t1 print('Support Vector Regression with polynomial kernel takes {} s'.format( t_svr_poly)) # Proceed reression using Relevance Vector Regression (RVR) t1 = time.time() y_rvr = rvm.fit(x_val, y_val).predict(x_val) t2 = time.time() t_rvr = t2 - t1 print('Relevance Vector Regression takes {} s'.format(t_rvr)) # Plot Data plt.scatter(x_val, y_val, color='red', label='Datapoints') plt.hold('on') plt.xlim([x_min, x_max]) plt.plot(x2_val, [sin(x2_val[i]) for i in range(len(x2_val))], c='k', label='Original function') # Regression Plot lw = 2 plt.plot(x_val, y_rbf, color='navy', lw=lw, label='RBF model') plt.plot(x_val, y_lin, color='c', lw=lw, label='Linear model')