本文整理汇总了Python中sklearn.kernel_ridge.KernelRidge方法的典型用法代码示例。如果您正苦于以下问题:Python kernel_ridge.KernelRidge方法的具体用法?Python kernel_ridge.KernelRidge怎么用?Python kernel_ridge.KernelRidge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.kernel_ridge
的用法示例。
在下文中一共展示了kernel_ridge.KernelRidge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: model_builder
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def model_builder(model_dir):
sklearn_model = KernelRidge(kernel="rbf", alpha=1e-3, gamma=0.05)
return dc.models.SklearnModel(sklearn_model, model_dir)
示例2: model_builder
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def model_builder(model_dir):
sklearn_model = KernelRidge(kernel="rbf", alpha=5e-4, gamma=0.008)
return dc.models.SklearnModel(sklearn_model, model_dir)
示例3: test_kernel_ridge
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_kernel_ridge():
pred = Ridge(alpha=1, fit_intercept=False).fit(X, y).predict(X)
pred2 = KernelRidge(kernel="linear", alpha=1).fit(X, y).predict(X)
assert_array_almost_equal(pred, pred2)
示例4: test_kernel_ridge_csr
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_kernel_ridge_csr():
pred = Ridge(alpha=1, fit_intercept=False,
solver="cholesky").fit(Xcsr, y).predict(Xcsr)
pred2 = KernelRidge(kernel="linear", alpha=1).fit(Xcsr, y).predict(Xcsr)
assert_array_almost_equal(pred, pred2)
示例5: test_kernel_ridge_csc
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_kernel_ridge_csc():
pred = Ridge(alpha=1, fit_intercept=False,
solver="cholesky").fit(Xcsc, y).predict(Xcsc)
pred2 = KernelRidge(kernel="linear", alpha=1).fit(Xcsc, y).predict(Xcsc)
assert_array_almost_equal(pred, pred2)
示例6: test_kernel_ridge_singular_kernel
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_kernel_ridge_singular_kernel():
# alpha=0 causes a LinAlgError in computing the dual coefficients,
# which causes a fallback to a lstsq solver. This is tested here.
pred = Ridge(alpha=0, fit_intercept=False).fit(X, y).predict(X)
kr = KernelRidge(kernel="linear", alpha=0)
ignore_warnings(kr.fit)(X, y)
pred2 = kr.predict(X)
assert_array_almost_equal(pred, pred2)
示例7: test_kernel_ridge_precomputed
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_kernel_ridge_precomputed():
for kernel in ["linear", "rbf", "poly", "cosine"]:
K = pairwise_kernels(X, X, metric=kernel)
pred = KernelRidge(kernel=kernel).fit(X, y).predict(X)
pred2 = KernelRidge(kernel="precomputed").fit(K, y).predict(K)
assert_array_almost_equal(pred, pred2)
示例8: test_kernel_ridge_sample_weights
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_kernel_ridge_sample_weights():
K = np.dot(X, X.T) # precomputed kernel
sw = np.random.RandomState(0).rand(X.shape[0])
pred = Ridge(alpha=1,
fit_intercept=False).fit(X, y, sample_weight=sw).predict(X)
pred2 = KernelRidge(kernel="linear",
alpha=1).fit(X, y, sample_weight=sw).predict(X)
pred3 = KernelRidge(kernel="precomputed",
alpha=1).fit(K, y, sample_weight=sw).predict(K)
assert_array_almost_equal(pred, pred2)
assert_array_almost_equal(pred, pred3)
示例9: test_kernel_ridge_multi_output
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_kernel_ridge_multi_output():
pred = Ridge(alpha=1, fit_intercept=False).fit(X, Y).predict(X)
pred2 = KernelRidge(kernel="linear", alpha=1).fit(X, Y).predict(X)
assert_array_almost_equal(pred, pred2)
pred3 = KernelRidge(kernel="linear", alpha=1).fit(X, y).predict(X)
pred3 = np.array([pred3, pred3]).T
assert_array_almost_equal(pred2, pred3)
示例10: fit
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def fit(self):
"""Scale data and train the model with the indicated algorithm.
Do not forget to tune the hyperparameters.
Parameters
----------
algorithm : String,
"KernelRidge", "SVM", "LinearRegression", "Lasso", "ElasticNet", "NeuralNet", "BaggingNeuralNet", default = "SVM"
"""
self.X_scaler.fit(self.X_train)
self.Y_scaler.fit(self.y_train)
# scaling the data in all cases, it may not be used during the fit later
self.X_train_sc = self.X_scaler.transform(self.X_train)
self.y_train_sc = self.Y_scaler.transform(self.y_train)
self.X_test_sc = self.X_scaler.transform(self.X_test)
self.y_test_sc = self.Y_scaler.transform(self.y_test)
if self.algorithm == "KernelRidge":
clf_kr = KernelRidge(kernel=self.user_kernel)
self.model = sklearn.model_selection.GridSearchCV(clf_kr, cv=5, param_grid=self.param_kr)
elif self.algorithm == "SVM":
clf_svm = SVR(kernel=self.user_kernel)
self.model = sklearn.model_selection.GridSearchCV(clf_svm, cv=5, param_grid=self.param_svm)
elif self.algorithm == "Lasso":
clf_lasso = sklearn.linear_model.Lasso(alpha=0.1,random_state=self.rand_state)
self.model = sklearn.model_selection.GridSearchCV(clf_lasso, cv=5,
param_grid=dict(alpha=np.logspace(-5,5,30)))
elif self.algorithm == "ElasticNet":
clf_ElasticNet = sklearn.linear_model.ElasticNet(alpha=0.1, l1_ratio=0.5,random_state=self.rand_state)
self.model = sklearn.model_selection.GridSearchCV(clf_ElasticNet,cv=5,
param_grid=dict(alpha=np.logspace(-5,5,30)))
elif self.algorithm == "LinearRegression":
self.model = sklearn.linear_model.LinearRegression()
elif self.algorithm == "NeuralNet":
self.model = MLPRegressor(**self.param_neurons)
elif self.algorithm == "BaggingNeuralNet":
nn_m = MLPRegressor(**self.param_neurons)
self.model = BaggingRegressor(base_estimator = nn_m, **self.param_bag)
if self.scaling == True:
self.model.fit(self.X_train_sc, self.y_train_sc.reshape(-1,))
predict_train_sc = self.model.predict(self.X_train_sc)
self.prediction_train = self.Y_scaler.inverse_transform(predict_train_sc.reshape(-1,1))
predict_test_sc = self.model.predict(self.X_test_sc)
self.prediction_test = self.Y_scaler.inverse_transform(predict_test_sc.reshape(-1,1))
else:
self.model.fit(self.X_train, self.y_train.reshape(-1,))
self.prediction_train = self.model.predict(self.X_train)
self.prediction_test = self.model.predict(self.X_test)
示例11: __init__
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def __init__(self, alpha=1, kernel='linear', gamma=None, degree=3, coef0=1, kernel_params=None):
self._hyperparams = {
'alpha': alpha,
'kernel': kernel,
'gamma': gamma,
'degree': degree,
'coef0': coef0,
'kernel_params': kernel_params}
self._wrapped_model = Op(**self._hyperparams)
示例12: ridge_regression
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def ridge_regression(K1, K2, y1, y2, alpha, c):
n_val, n_train = K2.shape
clf = KernelRidge(kernel = "precomputed", alpha = alpha)
one_hot_label = np.eye(c)[y1] - 1.0 / c
clf.fit(K1, one_hot_label)
z = clf.predict(K2).argmax(axis = 1)
return 1.0 * np.sum(z == y2) / n_val
示例13: __init__
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def __init__(self, options):
self.handle_options(options)
out_params = convert_params(options.get('params', {}), floats=['gamma'])
out_params['kernel'] = 'rbf'
self.estimator = _KernelRidge(**out_params)
示例14: test_KernelRidge
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_KernelRidge(self):
KernelRidgeAlgo.register_codecs()
self.regressor_util(KernelRidge)
示例15: test_objectmapper
# 需要导入模块: from sklearn import kernel_ridge [as 别名]
# 或者: from sklearn.kernel_ridge import KernelRidge [as 别名]
def test_objectmapper(self):
df = pdml.ModelFrame([])
self.assertIs(df.kernel_ridge.KernelRidge, kr.KernelRidge)