Python:如何在stacked mod中生成可重复的结果

2024-05-29 06:37:38 发布

您现在位置:Python中文网/ 问答频道 /正文

经过这么多的尝试和错误,我终于成功地建立了我自己的堆叠模型。但我不能每次都做出同样的(准确度)。我知道我必须将random_state参数初始化为任何值,但是即使在调用类方法之前显式地将random_state值写入某个值之后,我仍然可以得到随机结果。在

class Stacking(BaseEstimator, ClassifierMixin):
    def __init__(self, BaseModels, MetaModel, nfolds = 3, seed = 1):
        self.BaseModels = BaseModels
        self.MetaModel = MetaModel
        self.nfolds = nfolds
        self.seed = np.random.seed(seed) <---- This fixed my error. thanks to foladev.

    def fit(self, X, y):
        self.BaseModels_ = [list() for model in self.BaseModels]
        self.MetaModel_ = clone(self.MetaModel)
        kf = KFold(n_splits = self.nfolds, shuffle = False, random_state = 6)
        out_of_fold_preds = np.zeros((X.shape[0], len(self.BaseModels_)))

        for index, model in enumerate(self.BaseModels_):
            for train_index, out_of_fold_index in kf.split(X, y):
                instance = clone(model)
                self.BaseModels_[index].append(instance)
                instance.fit(X[train_index], y[train_index])

                preds = instance.predict(X[out_of_fold_index])
                out_of_fold_preds[out_of_fold_index, index] = preds
                #print(model, preds, out_of_fold_preds.shape)
        self.MetaModel_.fit(out_of_fold_preds, y)
        return self

我使用LogisticRegression、SGDClassifier、RandomForestClassifier作为基本模型,XGBoost作为元模型。随机态在所有模型中都存在,但仅对基本模型有效。在

我得到错误“init()在将random\u state放入xgbclassifier中时得到意外的关键字参数'random'u state'”。在

请注意,在调用类之前,我已尝试初始化random_state。尝试在KFold中改变洗牌。另外,如何初始化类方法中的参数?在


Tags: ofinstance模型selfindexmodelrandomfold
1条回答
网友
1楼 · 发布于 2024-05-29 06:37:38

从API来看,xgbclassifier使用了“seed”。在

xgboost.XGBClassifier(max_depth=3, learning_rate=0.1, n_estimators=100, silent=True, objective='binary:logistic', booster='gbtree', n_jobs=1, nthread=None, gamma=0, min_child_weight=1, max_delta_step=0, subsample=1, colsample_bytree=1, colsample_bylevel=1, reg_alpha=0, reg_lambda=1, scale_pos_weight=1, base_score=0.5, random_state=0, seed=None, missing=None, **kwargs)

请问您为什么不设置类级种子并将其应用于所有方法?在

相关问题 更多 >

    热门问题