<jupyter_start><jupyter_text>KFold<jupyter_code>from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold , train_test_split
import numpy as np

iris = load_iris()
features = iris.data
label = iris.target

print('Iris Data Size : {}'.format(features.shape[0]) )
print(features.shape)
dt_clf = DecisionTreeClassifier(random_state=156)
kfold = KFold(n_splits=10, shuffle=True)
cv_accuracy = []
n_iter = 0

# KFold.split( ) returns index for train and test dataset in array
for train_index, test_index  in kfold.split(features):
    
    X_train, X_test = features[train_index], features[test_index]
    y_train, y_test = label[train_index], label[test_index]
    
    dt_clf.fit(X_train , y_train)    # run DecisionTree Model 
    pred = dt_clf.predict(X_test)
    n_iter += 1
    
    accuracy = np.round(accuracy_score(y_test,pred), 4)
    train_size = X_train.shape[0]
    test_size = X_test.shape[0]
    print('\n#{0} cross_validation_accuracy :{1}, train data size: {2}, validation data size: {3}' \
          .format(n_iter, accuracy, train_size, test_size))
    print('#{0} validation_index:{1}'.format(n_iter,test_index))
    cv_accuracy.append(accuracy)
    
# take the average of accuracy from each iteration
print('\n## average_cross_validation_accuracy:', np.mean(cv_accuracy))<jupyter_output>#1 cross_validation_accuracy :1.0, train data size: 135, validation data size: 15
#1 validation_index:[  7  11  24  32  39  66 109 113 122 127 132 137 141 143 145]

#2 cross_validation_accuracy :0.9333, train data size: 135, validation data size: 15
#2 validation_index:[ 22  36  41  45  47  49  51  55  78  80  82  89 104 131 138]

#3 cross_validation_accuracy :0.8667, train data size: 135, validation data size: 15
#3 validation_index:[  4  25  56  60  67  70  71  91 103 106 114 115 123 147 149]

#4 cross_validation_accuracy :0.9333, train data size: 135, validation data size: 15
#4 validation_index:[ 13  14  15  18  19  52  65  73  96  99 102 118 130 133 146]

#5 cross_validation_accuracy :0.9333, train data size: 135, validation data size: 15
#5 validation_index:[  0   8  28  33  37  42  53  58  83  93 101 107 110 117 135]

#6 cross_validation_accuracy :0.9333, train data size: 135, validation data size: 15
#6 validation_index:[  6  17  20  21  23  38  40  59  61  77  86 116 128 140 [...]<jupyter_text>Stratified KFold<jupyter_code>import pandas as pd

iris = load_iris()

iris_df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
iris_df['label']=iris.target
iris_df['label'].value_counts()
iris_df.head()
from sklearn.model_selection import StratifiedKFold

skf = StratifiedKFold(n_splits=12, shuffle=True, random_state = 120)
n_iter=0
fold = {}

for train_index, test_index in skf.split(iris_df, iris_df['label']):
    n_iter += 1
    fold[n_iter] = train_index, test_index
    
fold
    
dt_clf = DecisionTreeClassifier(random_state=156)

skfold = StratifiedKFold(n_splits=5, shuffle = True)
n_iter=0
cv_accuracy=[]

for train_index, test_index  in skfold.split(features, label): # label(y/target value) has to be added together with features(X)
    X_train, X_test = features[train_index], features[test_index]
    y_train, y_test = label[train_index], label[test_index]
    #train and predict
    dt_clf.fit(X_train , y_train)    
    pred = dt_clf.predict(X_test)

    
    n_iter += 1
    accuracy = np.round(accuracy_score(y_test,pred), 4)
    train_size = X_train.shape[0]
    test_size = X_test.shape[0]
    print('\n#{0} cross_validation_accuracy :{1}, train data size: {2}, validation data size: {3}'\
          .format(n_iter, accuracy, train_size, test_size))
    
    print('#{0} train_index:{1}'.format(n_iter,train_index))
    print('#{0} validation_index:{1}'.format(n_iter,test_index))
    cv_accuracy.append(accuracy)
    
print('\n## each_cross_validation_accuracy:', np.round(cv_accuracy, 4))
print('\n## average_cross_validation_accuracy:', np.mean(cv_accuracy))<jupyter_output>#1 cross_validation_accuracy :0.9, train data size: 120, validation data size: 30
#1 train_index:[  0   1   2   3   4   5   6   7  10  11  12  13  14  15  16  17  18  19
  20  21  22  23  25  26  27  28  29  30  32  34  37  38  39  40  41  42
  43  46  47  48  50  52  53  54  55  56  57  58  59  60  62  63  64  65
  67  69  71  72  73  75  77  78  79  80  81  83  84  85  86  87  88  89
  90  91  93  95  96  97  98  99 101 103 104 107 108 109 110 111 112 113
 114 115 116 118 120 121 122 123 124 125 126 127 128 129 130 131 133 134
 135 136 139 140 141 142 143 144 145 147 148 149]
#1 validation_index:[  8   9  24  31  33  35  36  44  45  49  51  61  66  68  70  74  76  82
  92  94 100 102 105 106 117 119 132 137 138 146]

#2 cross_validation_accuracy :0.9667, train data size: 120, validation data size: 30
#2 train_index:[  0   1   2   3   4   5   6   7   8   9  10  11  13  14  15  16  17  19
  20  22  23  24  30  31  32  33  34  35  36  37  38  39  40  41  42  44
  45  46  48  49  50  51[...]
{'fraction_non_alphanumeric': 0.09270998415213946, 'fraction_numerical': 0.14896988906497624, 'mean_word_length': 3.476063829787234, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '650752', 'license': 'MIT', 'n_tokens_mistral': 2392, 'n_tokens_neox': 1887, 'n_words': 603, 'path': 'ML/Chapter 02_04.ipynb', 'repo_name': 'gurher/Data-Analysis', 'size': '5048'}