使用相同random_state的RandomizedSearchCV产生不同结果

我使用管道(pipeline)来进行特征选择和超参数优化,使用的是RandomizedSearchCV。以下是代码摘要:

from sklearn.cross_validation import train_test_splitfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.feature_selection import SelectKBestfrom sklearn.grid_search import RandomizedSearchCVfrom sklearn.pipeline import make_pipelinefrom scipy.stats import randint as sp_randintrng = 44X_train, X_test, y_train, y_test =    train_test_split(data[features], data['target'], random_state=rng)clf = RandomForestClassifier(random_state=rng)kbest = SelectKBest()pipe = make_pipeline(kbest,clf)upLim = X_train.shape[1]param_dist = {'selectkbest__k':sp_randint(upLim/2,upLim+1),  'randomforestclassifier__n_estimators': sp_randint(5,150),  'randomforestclassifier__max_depth': [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, None],  'randomforestclassifier__criterion': ["gini", "entropy"],  'randomforestclassifier__max_features': ['auto', 'sqrt', 'log2']}clf_opt = RandomizedSearchCV(pipe, param_distributions= param_dist,                              scoring='roc_auc', n_jobs=1, cv=3, random_state=rng)clf_opt.fit(X_train,y_train)y_pred = clf_opt.predict(X_test)

我在train_test_splitRandomForestClassiferRandomizedSearchCV中使用了相同的random_state。然而,如果我多次运行上述代码,结果会略有不同。更具体地说,我的代码中有几个测试单元,这些略有不同的结果导致测试单元失败。难道不是因为使用了相同的random_state,我应该得到相同的结果吗?我在代码中是否遗漏了什么,导致代码的某些部分产生了随机性?


回答:

我通常会自己回答自己的问题!我将它留在这里,以帮助有类似问题的人:

为了确保避免任何随机性,我定义了一个随机种子。代码如下:

from sklearn.cross_validation import train_test_splitfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.feature_selection import SelectKBestfrom sklearn.grid_search import RandomizedSearchCVfrom sklearn.pipeline import make_pipelinefrom scipy.stats import randint as sp_randintseed = np.random.seed(22)X_train, X_test, y_train, y_test =    train_test_split(data[features], data['target'])clf = RandomForestClassifier()kbest = SelectKBest()pipe = make_pipeline(kbest,clf)upLim = X_train.shape[1]param_dist = {'selectkbest__k':sp_randint(upLim/2,upLim+1),  'randomforestclassifier__n_estimators': sp_randint(5,150),  'randomforestclassifier__max_depth': [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, None],  'randomforestclassifier__criterion': ["gini", "entropy"],  'randomforestclassifier__max_features': ['auto', 'sqrt', 'log2']}clf_opt = RandomizedSearchCV(pipe, param_distributions= param_dist,                              scoring='roc_auc', n_jobs=1, cv=3)clf_opt.fit(X_train,y_train)y_pred = clf_opt.predict(X_test)

希望这对其他人有所帮助!

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注