mirror of
https://github.com/fenago/data-science.git
synced 2026-07-20 21:21:29 +00:00
5.1 KiB
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.dataIn [ ]:
# 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')