我正在使用管道来执行特征选择和超参数优化RandomizedSearchCV。下面是代码的摘要:
from sklearn.cross_validation import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectKBest
from sklearn.grid_search import RandomizedSearchCV
from sklearn.pipeline import make_pipeline
from scipy.stats import randint as sp_randint
rng = 44
X_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, …Run Code Online (Sandbox Code Playgroud) python machine-learning scikit-learn random-seed grid-search