- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想将 XGBoost 模型与 PCA 的输入缩放和特征空间缩减相结合。此外,模型的超参数以及 PCA 中使用的组件数量应使用交叉验证进行调整。并且为了防止模型过拟合,应该加入early stopping。
为了组合各个步骤,我决定使用 sklearn 的 Pipeline
功能。
一开始,我在确保 PCA 也应用于验证集时遇到了一些问题。但我认为使用 XGB__eval_set
可以解决问题。
代码实际上运行没有任何错误,但似乎永远运行(在某个时候所有内核的 CPU 使用率下降到零但进程继续运行数小时;不得不在某个时候终止 session )。
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from xgboost import XGBRegressor
# Train / Test split
X_train, X_test, y_train, y_test = train_test_split(X_with_features, y, test_size=0.2, random_state=123)
# Train / Validation split
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=123)
# Pipeline
pipe = Pipeline(steps=[("Scale", StandardScaler()),
("PCA", PCA()),
("XGB", XGBRegressor())])
# Hyper-parameter grid (Test only)
grid_param_pipe = {'PCA__n_components': [5],
'XGB__n_estimators': [1000],
'XGB__max_depth': [3],
'XGB__reg_alpha': [0.1],
'XGB__reg_lambda': [0.1]}
# Grid object
grid_search_pipe = GridSearchCV(estimator=pipe,
param_grid=grid_param_pipe,
scoring="neg_mean_squared_error",
cv=5,
n_jobs=5,
verbose=3)
# Run CV
grid_search_pipe.fit(X_train, y_train, XGB__early_stopping_rounds=10, XGB__eval_metric="rmse", XGB__eval_set=[[X_val, y_val]])
最佳答案
问题是fit
方法需要一个外部创建的评估集,但我们不能在管道转换之前创建评估集。
这有点 hacky,但想法是为 xgboost 回归器/分类器创建一个薄包装器,为内部的评估集做准备。
from sklearn.base import BaseEstimator
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor, XGBClassifier
class XGBoostWithEarlyStop(BaseEstimator):
def __init__(self, early_stopping_rounds=5, test_size=0.1,
eval_metric='mae', **estimator_params):
self.early_stopping_rounds = early_stopping_rounds
self.test_size = test_size
self.eval_metric=eval_metric='mae'
if self.estimator is not None:
self.set_params(**estimator_params)
def set_params(self, **params):
return self.estimator.set_params(**params)
def get_params(self, **params):
return self.estimator.get_params()
def fit(self, X, y):
x_train, x_val, y_train, y_val = train_test_split(X, y, test_size=self.test_size)
self.estimator.fit(x_train, y_train,
early_stopping_rounds=self.early_stopping_rounds,
eval_metric=self.eval_metric, eval_set=[(x_val, y_val)])
return self
def predict(self, X):
return self.estimator.predict(X)
class XGBoostRegressorWithEarlyStop(XGBoostWithEarlyStop):
def __init__(self, *args, **kwargs):
self.estimator = XGBRegressor()
super(XGBoostRegressorWithEarlyStop, self).__init__(*args, **kwargs)
class XGBoostClassifierWithEarlyStop(XGBoostWithEarlyStop):
def __init__(self, *args, **kwargs):
self.estimator = XGBClassifier()
super(XGBoostClassifierWithEarlyStop, self).__init__(*args, **kwargs)
下面是一个测试。
from sklearn.datasets import load_diabetes
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
from sklearn.model_selection import GridSearchCV
x, y = load_diabetes(return_X_y=True)
print(x.shape, y.shape)
# (442, 10) (442,)
pipe = Pipeline([
('pca', PCA(5)),
('xgb', XGBoostRegressorWithEarlyStop())
])
param_grid = {
'pca__n_components': [3, 5, 7],
'xgb__n_estimators': [10, 20, 30, 50]
}
grid = GridSearchCV(pipe, param_grid, scoring='neg_mean_absolute_error')
grid.fit(x, y)
print(grid.best_params_)
如果向开发人员请求功能请求,最简单的扩展是允许 XGBRegressor
在未提供时在内部创建评估集。这样,就不需要对 scikit-learn 进行扩展(我猜)。
关于python - XGBoost 与 GridSearchCV、缩放、PCA 和 sklearn 管道中的早期停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824326/
考虑以下网格搜索: grid = GridSearchCV(clf, parameters, n_jobs =-1, iid=True, cv =5) grid_fit = grid.fit(X_tr
我正在做一个 GridSearchCV,我已经监控了核心的百分比,并且我看到当我运行一个简单的神经网络时,4 个核心具有相同的百分比,但是当网格搜索 cv (n_jobs = 1) 开始时在情节的线条
我在带有 RBF 内核的 SVM 上进行了网格搜索 + 交叉验证,以使用类 GridShearchCV 找到参数 C 和 gamma 的最佳值。现在我想以表格格式获得结果,例如 C/gamma 1e-
我正在尝试为 sklearn 的 GridSearchCV 结果生成热图。我喜欢的东西sklearn-evaluation是因为生成热图真的很容易。但是,我遇到了一个问题。当我将参数设为 None 时
我想提高这个的参数GridSearchCV 对于 随机森林回归器 . def Grid_Search_CV_RFR(X_train, y_train): from sklearn.model_
我正在尝试设置 GridSearchCV 的实例来确定哪一组超参数将产生最低的平均绝对误差。 This scikit documentation表示分数指标可以在创建 GridSearchCV 时传递
当使用网格搜索在 python 中使用此函数 GridSearchCV() 进行分类器时,假设我们有一个参数区间来调整形式 1 到 100,我们如何能够指定它(1:100 不起作用)? p> 最佳答案
我是机器学习的新手,并且一直坚持这个。 当我尝试在线性模型中实现多项式回归时,例如使用多个次数的多项式范围(1,10)并获得不同的 MSE。我实际上使用 GridsearchCV 方法来查找多项式的最
我想在一系列 alpha(拉普拉斯平滑参数)上使用 GridSearchCV 来检查哪个为伯努利朴素贝叶斯模型提供了最佳准确度。 def binarize_pixels(data, threshold
使用 sklearn 在随机森林分类器上运行网格搜索。这个运行的时间比我想象的要长,我正在尝试估计这个过程还剩多少时间。我认为它的总拟合次数是 3*3*3*3*5 = 405。 clf = Rando
我正在尝试使用网格搜索找出要在 PCA 中使用的 n_components 的最佳值: from sklearn.decomposition import PCA from sklearn.grid_
我正在尝试 GridsearchCV 但我希望在 param grid 中有一些异常(exception)。这是我的网格搜索代码: from sklearn.model_selection impor
我很难找出 GridSearchCV 中的参数 return_train_score。来自docs : return_train_score : boolean, optional If
我必须进行多类分类 (3)。我使用 GridSearchCV 为我的分类器搜索最佳参数。 但我有一个不平衡的 x_train(和 x_test):0 有 3079 个实例,1 有 12 个实例,3 有
有没有办法访问在 GridSearchCV 过程中计算的预测值? 我希望能够根据实际值(来自测试/验证集)绘制预测的 y 值。 网格搜索完成后,我可以使用 将其与其他一些数据相匹配 ypred =
我正在使用GridsearchCV来调整超参数,现在我想在训练和验证步骤中进行最小-最大Normalization(StandardScaler())。但我认为我不能做到这一点。 问题是: 如果我对整
我正在使用 scikit learn 进行多标签分类。我使用 RandomForestClassifier 作为基本估计器。我想使用 GridSearchCV 优化每个标签的参数。目前我正在按以下方式
好的,我只想说,我对 SciKit-Learn 和数据科学完全陌生。但这是问题所在,也是我目前对该问题的研究。代码在底部。 总结 我正尝试使用 BernoulliRBM 进行类型识别(例如数字),并尝
我正在使用 GridSearchCV ,并且在每次迭代之后,我想将 clf.cv_results_ 属性保存到一个文件中(以防进程在中间崩溃)。 我尝试寻找解决方案,但就是想不通。 我们将不胜感激。
我正在尝试自学如何在基本的多层神经网络中对神经元的数量进行网格搜索。我正在使用 Python 的 GridSearchCV 和 KerasClasifier 以及 Keras。下面的代码适用于其他数据
我是一名优秀的程序员,十分优秀!