【问题标题】:Python: How to produce reproducible results in stacked modelPython:如何在堆叠模型中产生可重现的结果
【发布时间】:2018-02-08 17:48:52
【问题描述】:

经过这么多的试验和错误,我终于设法建立了自己的堆叠模型。但我每次都无法产生(准确度)相同的结果。我知道我必须将 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、RandomForestClassifer 作为我的基本模型,并使用 XGBoost 作为我的元模型。 random_state 存在于所有模型中,但仅适用于基本模型。

当将 random_state 放入 xgbclassifier 时,我收到错误“init() got an unexpected keyword argument 'random_state'”。

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

【问题讨论】:

    标签: python machine-learning


    【解决方案1】:

    从 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
    )
    

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

    【讨论】:

    • 类级种子是什么意思?这是我第一次制作类方法,所以我不知道该怎么做。如果你能举个例子,我会很高兴。另外,你能告诉我为什么我得到“__init__() got an unexpected keyword argument 'random_state'”,即使 xgboost 有参数?
    • 请回复。
    • 通过类级别的种子,我的意思是通过'self'在类级别访问'seed'变量(即random_state)。请注意,您的方法签名始终包含一个“自我”,它是为了访问类成员而传递给该方法的对象本身。在这种情况下,您将调用 self.random_seed,而不是分配一个新号码。
    • 我不确定 xgboost 是否有 random_state 变量。 api 表示“种子”而不是“随机状态”。
    • 至于您在 '__init__()' 中的错误,您能否发布您的完整课程,以便我了解您是如何子类化的?我还没有检查过,但我打赌也没有“random_state”变量。变量名也可能是“种子”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多