经过这么多的尝试和错误,我终于成功地建立了我自己的堆叠模型。但我不能每次都做出同样的(准确度)。我知道我必须将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)
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中改变洗牌。另外,如何初始化类方法中的参数?在