<jupyter_start><jupyter_text>Grid Search<jupyter_code>import pandas as pd, numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import GridSearchCV
digits = load_digits()
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.tree import DecisionTreeClassifier
model_params = {
    'svm': {
        'model': SVC(),
        'params' : {
            'C': [1, 10, 20],
            'kernel': ['rbf', 'linear'],
            'gamma': ['auto', 'scale'],
        }
    },
    'random_forest': {
        'model': RandomForestClassifier(),
        'params' : {
            'n_estimators': [1, 5, 10, 20, 30, 40]
        }
    },
    'logistic_regression' : {
        'model': LogisticRegression(solver='liblinear', multi_class='auto'),
        'params': {
            'C': [1, 5, 10]
        }
    },
    'GNB' : {
        'model': GaussianNB(),
        'params': {
        }
    },
    'MNB' : {
        'model': MultinomialNB(),
        'params': {
        }
    },
    'decition_tree' : {
        'model': DecisionTreeClassifier(),
        'params': {
            'criterion': ['gini', 'entropy'],
        }
    },
}
scores = []

for model_name, mp in model_params.items():
    clf =  GridSearchCV(mp['model'], mp['params'], cv=10, return_train_score=False)
    clf.fit(digits.data, digits.target)
    scores.append({
        'model': model_name,
        'best_score': clf.best_score_,
        'best_params': clf.best_params_
    })
    
df = pd.DataFrame(scores,columns=['model', 'best_score', 'best_params'])
df<jupyter_output><empty_output><jupyter_text>So, Best model is SVM with C=10, kernel=rbf, gamma=scale (Score : 98.16 %)<jupyter_code>model = SVC(C=10, kernel='rbf', gamma='scale')
from sklearn.model_selection import cross_val_score
np.average(cross_val_score(model, digits.data, digits.target, cv=10))
model.fit(digits.data, digits.target)
model.score(digits.data, digits.target)
model.predict(digits.data[[100]])
digits.target[100]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14804597701149425, 'fraction_numerical': 0.017011494252873564, 'mean_word_length': 3.129032258064516, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '580995', 'license': 'MIT', 'n_tokens_mistral': 790, 'n_tokens_neox': 737, 'n_words': 145, 'path': '11_gridsearch.ipynb', 'repo_name': 'codernayeem/ml-practice', 'size': '2175'}