Train model:
from sklearn.model_selection import GridSearchCV param_grid = [ # try 6 (3×2) combinations of hyperparameters {‘n_neighbors‘: [3, 5, 7], ‘weights‘: [‘uniform‘,‘distance‘]} ] knn_clf = KNeighborsClassifier() # train across 3 folds, that‘s a total of 6*3=18 rounds of training grid_search = GridSearchCV(knn_clf, param_grid, cv=3, scoring=‘accuracy‘, return_train_score=True, n_jobs=-1) grid_search.fit(X_train, y_train)
Show parameters of best model:
grid_search.best_params_
Show the score of train set:
grid_search.best_score_
Fit on test set:
y_pred = grid_search.predict(X_test)
Show the score of test set:
from sklearn.metrics import accuracy_score accuracy_score(y_test, y_pred)
More about GridSearchCV: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html
原文地址:https://www.cnblogs.com/sherrydatascience/p/10206790.html
时间: 2024-10-02 07:35:39