Files
mlessentials/Lab08/Examples/tuning_using_gridsearchcv.ipynb
T
2021-02-05 20:44:51 +00:00

5.1 KiB

In [ ]:
from sklearn import model_selection, datasets, neighbors

# load the data
cancer = datasets.load_breast_cancer()

# target
y = cancer.target

# features
X = cancer.data
In [ ]:
# initalize the estimator
knn = neighbors.KNeighborsClassifier()
In [ ]:
# grid contains k and the weight function
grid = {
    'n_neighbors': [1, 3, 5, 7],
    'weights': ['uniform', 'distance']
}
In [ ]:
# set up the grid search with scoring on precsions and number of folds = 10
gscv = model_selection.GridSearchCV(estimator=knn, param_grid=grid, scoring='precision', cv=10)
In [ ]:
# start the search
gscv.fit(X, y)
In [ ]:
# view the results
print(gscv.cv_results_)
In [ ]:
import pandas as pd

# convert the results dictionary to a dataframe
results = pd.DataFrame(gscv.cv_results_)

# select just the hyperparameterizations tried, the mean test scores, order by score and show the top 5 models
print(
results.loc[:,['params','mean_test_score']].sort_values('mean_test_score', ascending=False).head(5)
)
In [ ]:
# visualize the result
results.loc[:,['params','mean_test_score']].plot.barh(x = 'params')