sklearn SVR与KRR对比

来源:互联网 发布:网络发票查询 编辑:程序博客网 时间:2024/06/07 04:48
ravel方法为矩阵按行拉直。
::这种切片运算可以通过设置步长来进行切片,由于切片属于view, 即对切片的修改会改变原数组。
对数组进行升高维度的工作可以通过切片运算后加一个None(在新维度上取None完成)
 Ex: np.array([[1, 2], [3, 4]])[:,:,None]

SVR由于没有显式解拟合较慢,但在预测上由于仅用到支持向量故比KRR快:
简单代码:
from __future__ import division import timeimport numpy as np from sklearn.svm import SVRfrom sklearn.grid_search import GridSearchCVfrom sklearn.kernel_ridge import KernelRidge rng = np.random.RandomState(0)X = 5 * rng.rand(10000, 1)y = np.sin(X).ravel()y[::5] += 3 * (0.5 - rng.rand(X.shape[0]/5))X_plot = np.linspace(0, 5, 100000)[:, None]train_size = 100svr = GridSearchCV(SVR(kernel = 'rbf', gamma = 0.1), cv = 5,  param_grid = {"C": [1e0,1e1,1e2,1e3], "gamma": np.logspace(-2, 2, 5)})kr = GridSearchCV(KernelRidge(kernel = 'rbf', gamma = 0.1), cv = 5, param_grid = {"alpha": [1e0,0.1,1e-2,1e-3], "gamma": np.logspace(-2,2,5)})t0 = time.time()svr.fit(X[:train_size], y[:train_size])svr_fit = time.time() - t0 print "SVR complexity and bandwidth selected and model fitted in %.3f s" % svr_fitt0 = time.time()kr.fit(X[:train_size], y[:train_size])kr_fit = time.time() - t0 print "KRR complexity and bandwidth selected and model fitted in %.3f s" % kr_fitsv_ratio = svr.best_estimator_.support_.shape[0] / train_sizeprint "Support vector ratio: %.3f" % sv_ratiot0 = time.time()y_svr = svr.predict(X_plot)svr_predict = time.time() - t0 print "SVR prediction for %d inputs in %.3f s" % (X_plot.shape[0], svr_predict)t0 = time.time()y_kr = kr.predict(X_plot)kr_predict = time.time() - t0 print "KRR prediction for %d inputs in %.3f s" % (X_plot.shape[0], kr_predict)





0 0