svr.fit(X, Y) t2 = time.time() print(mem_us_svr) svm_err = mean_squared_error(svr.predict(x), y) svs = svr.best_estimator_.support_vectors_.shape[0] print "SVM error on test set is {0}, number of support vectors is {1}, time {2}".format( svm_err, svs, t2 - t1) # Time # Memory usage # Number of SV # Create Prediction nPred = 1000 xPred = np.linspace(min(Xc), max(Xc), nPred).reshape((nPred, 1)) y_RVR_pred, var_RVR_pred = rvr.predict_dist(xPred) #y_SVR_pred, var_SVR_pred = svr.predict_dist(xPred) NOT possible? y_SVR_pred = svr.predict(xPred) y_real = 10 * np.sinc(xPred) # plot test vs predicted datax plt.figure(figsize=(16, 10)) plt.plot(X[:, 0], Y, "k+", markersize=3, label="train data") plt.plot(x[:, 0], y, "b+", markersize=3, label="test data") plt.plot(xPred[:, 0], y_RVR_pred, "b", markersize=3, label="RVR prediction") #plt.plot(xPred[:,0],y_RVR_pred + np.sqrt(var_RVR_pred),"c", markersize = 3, label = "y_hat +- std") #plt.plot(xPred[:,0],y_RVR_pred - np.sqrt(var_RVR_pred),"c", markersize = 3) plt.plot(rvr.relevant_vectors_, Y[rvr.active_], "co",
# 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() ) # # plot test vs predicted data
mem_us_svr = memory_usage((svr.fit,(X,Y)),interval=0.1) #svr.fit(X,Y) t2 = time.time() minMem_svr = min(mem_us_svr) maxMem_svr = max(mem_us_svr) svm_err = mean_squared_error(svr.predict(x),y) svs = svr.best_estimator_.support_vectors_.shape[0] print "SVR -- NMSR {0}, # SV {1}, time {2}, min Memory {3}, max. Memory {4}".format(svm_err, svs, t2 - t1, minMem_svr, maxMem_svr) # Time # Memory usage # Number of SV # Create Prediction dimTrain = Xc.shape nPred = 1000 xPred = np.zeros((nPred,dimTrain[1])) for i in range(dimTrain[1]): xPred[:,i] = np.linspace(min(Xc[:,i]),max(Xc[:,i]),nPred).reshape((nPred,)) y_RVR_pred, var_RVR_pred = rvr.predict_dist(xPred) y_SVR_pred = svr.predict(xPred) print('program succesfully terminated')