<jupyter_start><jupyter_text>Digits - Cross-validation  Import Libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC<jupyter_output><empty_output><jupyter_text>Data Loading<jupyter_code>digits = load_digits()<jupyter_output><empty_output><jupyter_text>Data Splitting<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(
    digits.data, digits.target)<jupyter_output><empty_output><jupyter_text>Cross-Validation<jupyter_code>cross_val_score(KNeighborsClassifier(),
                X_train, y_train, cv=5)
from sklearn.model_selection import KFold, RepeatedStratifiedKFold
cross_val_score(KNeighborsClassifier(),
                X_train, y_train, cv=KFold(n_splits=10, shuffle=True, random_state=42))
cross_val_score(KNeighborsClassifier(),
                X_train, y_train,
                cv=RepeatedStratifiedKFold(n_splits=10, n_repeats=10, random_state=42))<jupyter_output><empty_output><jupyter_text>Grid Searches with Cross-Validation for K-Neighbors  Define Parameter Grid<jupyter_code>param_grid = {'n_neighbors': [1, 3, 5, 7, 10]}<jupyter_output><empty_output><jupyter_text>Create & Fit Model GridSearchCV<jupyter_code>grid = GridSearchCV(KNeighborsClassifier(), param_grid=param_grid,
                    return_train_score=True, cv=5)
grid.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Get Results<jupyter_code>grid.best_params_
grid.best_estimator_
grid.score(X_train, y_train)
grid.score(X_test, y_test)
results = grid.cv_results_
plt.plot(param_grid['n_neighbors'], results['mean_train_score'], 
         label="train")
plt.plot(param_grid['n_neighbors'], results['mean_test_score'], 
         label="test")
plt.legend()<jupyter_output><empty_output><jupyter_text>Grid Searches with Cross Validation for SVC  Define Parameter Grid<jupyter_code>param_grid = {'C': 10. ** np.arange(-3, 3),
              'gamma' : 10. ** np.arange(-5, 0)}
np.set_printoptions(suppress=True)
print(param_grid)<jupyter_output>{'C': array([  0.001,   0.01 ,   0.1  ,   1.   ,  10.   , 100.   ]), 'gamma': array([0.00001, 0.0001 , 0.001  , 0.01   , 0.1    ])}<jupyter_text>Create & Fit Model GridSearchCV<jupyter_code>grid_search = GridSearchCV(SVC(), param_grid, verbose=3, cv=5)
grid_search.fit(X_train, y_train)
grid_search.predict(X_test)
grid_search.score(X_test, y_test)
grid_search.best_params_
grid_search.best_score_
grid_search.best_estimator_
# 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>
{'fraction_non_alphanumeric': 0.11988011988011989, 'fraction_numerical': 0.022311022311022312, 'mean_word_length': 6.472636815920398, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '25631', 'license': 'MIT', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1110, 'n_words': 170, 'path': '2-Linear_Classification/notebooks/.ipynb_checkpoints/3.1-Digits-Cross-validation-checkpoint.ipynb', 'repo_name': 'phermar/aprendiz-machine-learning', 'size': '3003'}