Ank*_*ita 3 python numpy scikit-learn hyperparameters gaussian-process
我开始使用 Sklearn 库使用我自己的数据点学习高斯回归,如下所示。虽然我得到的结果是不准确的,因为我没有进行超参数优化。我做了一些谷歌搜索并编写了gridsearch
代码。但代码没有按预期运行。我不知道我在哪里犯了错误,请帮助并提前致谢。
输入和输出数据的样本如下
X_tr= [10.8204 7.67418 7.83013 8.30996 8.1567 6.94831 14.8673 7.69338 7.67702 12.7542 11.847]
y_tr= [1965.21 854.386 909.126 1094.06 1012.6 607.299 2294.55 866.316 822.948 2255.32 2124.67]
X_te= [7.62022 13.1943 7.76752 8.36949 7.86459 7.16032 12.7035 8.99822 6.32853 9.22345 11.4751]
Run Code Online (Sandbox Code Playgroud)
X_tr, y_tr
和X_te
是训练数据点,是重塑值,并且具有“float64 数组”类型
这是我的网格搜索代码
from sklearn.model_selection import GridSearchCV
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
'C': [1, 10, 100, 1000]},
{'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
scores = ['precision', 'recall']
for score in scores:
print("# Tuning hyper-parameters for %s" % score)
print()
clf = GridSearchCV(
gp(), tuned_parameters, scoring='%s_macro' % score
)
clf.fit(X_tr, y_tr)
Run Code Online (Sandbox Code Playgroud)
这是我的代码示例,没有进行超参数优化:
import sklearn.gaussian_process as gp
kernel = gp.kernels.ConstantKernel(1.0, (1e-1, 1e3)) * gp.kernels.RBF(10.0, (1e-3, 1e3))
model = gp.GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=10, alpha=0.1, normalize_y=True)
X_tr=np.array([X_tr])
X_te=np.array([X_te])
y_tr=np.array([y_tr])
model.fit(X_tr, y_tr)
params = model.kernel_.get_params()
X_te = X_te.reshape(-1,1)
y_pred, std = model.predict(X_te, return_std=True)
Run Code Online (Sandbox Code Playgroud)
您提供的代码片段中存在一些问题,下面是一个工作示例:
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.gaussian_process.kernels import RBF, DotProduct
import numpy as np
X_tr = np.array([10.8204, 7.67418, 7.83013, 8.30996, 8.1567, 6.94831, 14.8673, 7.69338, 7.67702, 12.7542, 11.847])
y_tr = np.array([1965.21, 854.386, 909.126, 1094.06, 1012.6, 607.299, 2294.55, 866.316, 822.948, 2255.32, 2124.67])
X_te = np.array([7.62022, 13.1943, 7.76752, 8.36949, 7.86459, 7.16032, 12.7035, 8.99822, 6.32853, 9.22345, 11.4751])
param_grid = [{
"alpha": [1e-2, 1e-3],
"kernel": [RBF(l) for l in np.logspace(-1, 1, 2)]
}, {
"alpha": [1e-2, 1e-3],
"kernel": [DotProduct(sigma_0) for sigma_0 in np.logspace(-1, 1, 2)]
}]
# scores for regression
scores = ['explained_variance', 'r2']
gp = GaussianProcessRegressor()
for score in scores:
print("# Tuning hyper-parameters for %s" % score)
print()
clf = GridSearchCV(estimator=gp, param_grid=param_grid, cv=4,
scoring='%s' % score)
clf.fit(X_tr.reshape(-1, 1), y_tr)
print(clf.best_params_)
Run Code Online (Sandbox Code Playgroud)
我现在想将其分解以提供一些解释。第一部分是数据。您将需要更多数据(大概您在这里只提供了一个示例),但您还需要重新调整它以使高斯过程有效地工作。
第二部分是param_grid
. 参数网格可以是字典或字典列表。我使用了字典列表,因为您似乎对测试不同内核的性能感兴趣。当您添加更多数据时,参数网格的粒度非常低,我建议通过添加更多测试变量并alpha
增加np.logpspace
步骤和界限来增加粒度。
第三部分是测试分数。在上面的代码片段中,您有分类算法的分数,因为您对回归感兴趣,所以我使用了回归分数。
第四部分运行模型。它应该打印每个分数的最佳参数。我无法获得任何可靠的拟合,因为数据集确实有限。请注意输入的重塑,X_tr
因为它是一维的。
归档时间: |
|
查看次数: |
6579 次 |
最近记录: |