### RVR
 # train rvr
 rvr = RVR(gamma = gammaVal, kernel = 'rbf')
 
 t1 = time.time()
 mem_us_rvr = memory_usage((rvr.fit,(X,Y)),interval=0.1)
 #rvr.fit(X,Y)
 t2 = time.time()
 t_train_rvr[itVal].append(t2-t1)
 minMem_rvr = min(mem_us_rvr)
 maxMem_rvr = max(mem_us_rvr)
 
 # test rvr
 t1= time.time()
 rvr_errs[itVal].append(mean_squared_error(rvr.predict(x),y))
 t2 = time.time()
 t_test_rvr[itVal].append(t2-t1)
 
 rvr_s      = np.sum(rvr.active_)
 n_rvr[itVal].append(rvr_s)
 print "RVR -- NMSR {0}, # SV {1}, train time {2}, test time {3}, min Memroy {4}, max Memory {5}".format(rvr_errs[itVal][Ncv], rvr_s,t_train_rvr[itVal][Ncv], t2 - t1,minMem_rvr, maxMem_rvr)
 
 ### SVR
 # train svr
 svr = GridSearchCV(SVR(kernel = 'rbf', gamma = gammaVal), param_grid = {'C':C_vals},cv = 10)
 t1 = time.time()
 mem_us_svr = memory_usage((svr.fit,(X,Y)),interval=0.1) 
 #svr.fit(X,Y)
 t2 = time.time()
 t_train_svr[itVal].append(t2-t1)
Esempio n. 2
0
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()
print(mem_us_svr)
svm_err = mean_squared_error(svr.predict(x), y)
svs = svr.best_estimator_.support_vectors_.shape[0]