<jupyter_start><jupyter_text>Cross-validation<jupyter_code>%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
digits = load_digits()
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target)
from sklearn.model_selection import cross_val_score
from sklearn.neighbors import KNeighborsClassifier
cross_val_score(KNeighborsClassifier(), X_train, y_train, cv=5)
cross_val_score(KNeighborsClassifier(), X_train, y_train, cv=10)<jupyter_output><empty_output><jupyter_text>Grid Searches================= Grid-Search with build-in cross validation<jupyter_code>from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC<jupyter_output><empty_output><jupyter_text>Define parameter grid:<jupyter_code>import numpy as np

param_grid = {'C': 10. ** np.arange(-3, 3),
              'gamma' : 10. ** np.arange(-5, 0)}
              

np.set_printoptions(suppress=True)
print(param_grid)
grid_search = GridSearchCV(SVC(), param_grid, verbose=3, cv=5)<jupyter_output><empty_output><jupyter_text>A GridSearchCV object behaves just like a normal classifier.<jupyter_code>grid_search.fit(X_train, y_train)
grid_search.predict(X_test)
grid_search.score(X_test, y_test)
grid_search.best_params_
# We extract just the scores

scores = grid_search.cv_results_['mean_test_score']
scores = np.array(scores).reshape(6, 5)

plt.matshow(scores)
plt.xlabel('gamma')
plt.ylabel('C')
plt.colorbar()
plt.xticks(np.arange(5), param_grid['gamma'])
plt.yticks(np.arange(6), param_grid['C']);<jupyter_output><empty_output><jupyter_text>ExercisesUse GridSearchCV to adjust n_neighbors of KNeighborsClassifier.<jupyter_code># %load solutions/grid_search_k_neighbors.py<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11735537190082644, 'fraction_numerical': 0.009366391184573003, 'mean_word_length': 8.26530612244898, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '128270', 'license': 'MIT', 'n_tokens_mistral': 682, 'n_tokens_neox': 649, 'n_words': 115, 'path': 'notebooks/03 - Cross-validation and Grid Search.ipynb', 'repo_name': 'YiGaolooking/ODSC_ML_advanced_workshop', 'size': '1815'}