Machine learning algorithms to achieve grid search GridSearch (k- nearest neighbor algorithm to find the optimal parameters for example)

Grid search machine learning algorithm parameters to achieve:

//2019.08.03
1, the method scikitlearn library calls grid search as: Grid Search , a more unified way to search it simple, its criticism of the standard algorithm is complex, is a composite cross criticize the way, just exactly rate . The specific implementation is as follows (with three parameters commonly used ultra-KNN algorithm as an example):
# use scikitlearn in gridsearch to grid search over the best way to machine learning algorithm parameters of
# 1-1 first way to use the dictionary different parameter combinations super KNN algorithm defined
param_grid = [{
"weights": [ "Uniform"],
"N_NEIGHBORS": [I for I in Range (1,11)]
},
{ "weights": [ " Distance "],
" N_NEIGHBORS ": [I for I in Range (1,11)],
" P ": [I for I in Range (1,6)]
}
] # first, define different combinations of hyper-parameters of the machine learning algorithm , using the dictionary mode, two parameters employed for a particular super-list data structure
# 1-2 followed by a definition of the need to adjust parameters of the machine learning algorithm
knn_clf = KNeighborsClassifier ()
# 1-3 grid search function introduced in GridSearchCV scikitlearn, and define respective grid search mode (initialization input parameters: a machine learning algorithm, the parameter combination of ultra-2 list, 3n_jobs (the number of selected cores in parallel, all represented by -1 ), 4verbose = 2 represents the output of the respective search process)
from sklearn.model_selection Import GridSearchCV
grid_search = GridSearchCV (knn_clf, param_grid, n_jobs = -1, verbose = 2)
# 1-4 using the training data set grid search process
grid_search.fit (x_train, y_train)
# 1-5 corresponding output accuracy when playing grid search Hu optimal combination of parameters results and optimum parameters
Print (grid_search.best_estimator_)
Print (grid_search.best_params_)
Print (grid_search.best_score_)

Specific implementation code and results are as follows:

Guess you like

Origin www.cnblogs.com/Yanjy-OnlyOne/p/11294852.html