网格搜索使星号(*)保持运行状态,并且从不结束,没有任何输出

时间:2019-06-04 05:49:09

标签: python parameters cross-validation grid-search

代码不停地运行(*)。完成后,将传递值并使用来自gridsearch的最佳参数重新测试模型(物流,SVM和森林)

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score

param_range = [0.0001, 0.001, .005, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]
classifiers = [
               {"clf":LogisticRegression(random_state=0), "param_grid":[{'C': ``param_range}]}, 
              #{"clf":KNeighborsClassifier(n_neighbors=5), "param_grid":[{'n_neighbors': param_range}]},
              #{"clf": MLPClassifier(), "param_grid":[{'C': param_range}]},
               {"clf":SVC(random_state=0), "param_grid":[{'C': param_range, 'gamma': param_range, 'kernel': ['linear','rbf']}]}, 
               {"clf":RandomForestClassifier(random_state=0), "param_grid":[{'max_depth': [1, 2, 3, 4, 5, 6, 7, None], 
                                                                             'max_features': [None, 'auto'],
                                                                            'n_estimators': [10, 100, 1000]}]}
              ]


model_scores = []
for classifier in classifiers:
    # Inner Cross Validation, searches for the best parameters
    gs = GridSearchCV(estimator=classifier["clf"], param_grid=classifier["param_grid"], scoring='accuracy', cv=3)
    # Outer Cross Validation, evaluates the model
    model_scores.append(cross_val_score(gs, X_train_std, y_train, scoring='accuracy', cv=10))

models_df = pd.DataFrame(model_scores, columns=[1,2,3,4,5,6,7,8,9,10],
                         index=["LR", "SVM", "Forest"])
models_df["Mean"] = models_df.mean(axis=1)
models_df

0 个答案:

没有答案