<jupyter_start><jupyter_text>Load Digit Dataset<jupyter_code>from sklearn.datasets import load_digits
digits = load_digits()<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2)<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>lr = LogisticRegression(max_iter=10000)
lr.fit(x_train, y_train)
lr.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>svm = SVC()
svm.fit(x_train, y_train)
svm.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>Random Forest Classifier<jupyter_code>rfc = RandomForestClassifier()
rfc.fit(x_train, y_train)
rfc.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>Model Score Function<jupyter_code>def get_score(model, x_train, x_test, y_train, y_test):
    model.fit(x_train, y_train)
    return model.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>Model Objects<jupyter_code>lr = LogisticRegression(max_iter=10000)
svm = SVC()
rfc = RandomForestClassifier()<jupyter_output><empty_output><jupyter_text>Store scores in lists<jupyter_code>score_lr = []
score_svm = []
score_rfc = []<jupyter_output><empty_output><jupyter_text>Stratified K Fold<jupyter_code>from sklearn.model_selection import StratifiedKFold

skfold = StratifiedKFold(n_splits=10)

for train_index, test_index in skfold.split(digits.data, digits.target):
    x_train, x_test, y_train, y_test = digits.data[train_index], digits.data[test_index], \
                                       digits.target[train_index], digits.target[test_index]
    
    score_lr.append(get_score(lr, x_train, x_test, y_train, y_test))
    score_svm.append(get_score(svm, x_train, x_test, y_train, y_test))
    score_rfc.append(get_score(rfc, x_train, x_test, y_train, y_test))
score_lr
score_svm
score_rfc<jupyter_output><empty_output><jupyter_text>Cross Validation Same as Stratified K Fold, can be directly use the Cross Validation<jupyter_code>from sklearn.model_selection import cross_val_score

score_lr = cross_val_score(lr, digits.data, digits.target, cv=10)
score_lr<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1059190031152648, 'fraction_numerical': 0.007120605251446373, 'mean_word_length': 8.366666666666667, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '365343', 'license': 'Apache-2.0', 'n_tokens_mistral': 904, 'n_tokens_neox': 870, 'n_words': 123, 'path': '11_K_Fold_Cross_Validation/K_Fold_Cross_Validation.ipynb', 'repo_name': 'Yeasmin-Tasnima/ML', 'size': '2247'}