<jupyter_start><jupyter_text>K Cross Fold Validation<jupyter_code>from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
iris = load_iris()
x,y =iris.data,iris.target
classifier = KNeighborsClassifier()
# First step Shuffling of data           
import numpy as np
rng = np.random.RandomState(0)
permutation = rng.permutation((len(x)))# shuffling Randomly of Data
x , y = x[permutation],y[permutation]
k=5
n_samples = len(x)
fold_size = n_samples//k
scores = []
masks = []
for fold in range(k):
    test_mask = np.zeros(n_samples,dtype = bool)
    test_mask[fold * fold_size :(fold+1)*fold_size]=True
    masks.append(test_mask)
    x_test,y_test = x[test_mask],y[test_mask]
    x_train , y_train = x[~test_mask],y[~test_mask]
    classifier.fit(x_train,y_train)
    scores.append(classifier.score(x_test,y_test))
print(scores)
print(np.mean(scores))
import matplotlib.pyplot as plt
%matplotlib inline
plt.matshow(masks,cmap = 'gray_r')
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import ShuffleSplit
from sklearn.model_selection import StratifiedKFold
cv = ShuffleSplit(n_splits=5,test_size=.2)
scores = cross_val_score(classifier,x,y,cv=cv)
print(scores)
print(np.mean(scores))
def plot_cv(cv,features,labels):
    masks=[]
    for train ,test in cv.split(features,labels):
        mask = np.zeros(len(labels),dtype=bool)
        mask[test]=1;
        masks.append(test)
        
    plt.matshow(masks,cmap='gray_r')
plot_cv(StratifiedKFold(n_splits=5),iris.data,iris.target)
from sklearn.model_selection import KFold
plot_cv(KFold(n_splits=5),iris.data,iris.target)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12043139604553625, 'fraction_numerical': 0.004793289394847214, 'mean_word_length': 5.761133603238866, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '313729', 'license': 'Apache-2.0', 'n_tokens_mistral': 646, 'n_tokens_neox': 633, 'n_words': 109, 'path': '02-KCrossValidation/Cross Validation using Scikit learn.ipynb', 'repo_name': 'Shivamsharma009/Machine-Learning-Algorithms', 'size': '1669'}