I'm trying to get the best k from iris dataset using KNeighbors so I write the below code using scikit-learn.
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC, SVC
from sklearn.datasets import load_iris
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cross_validation import StratifiedKFold
from sklearn.metrics import f1_score
dataset = load_iris()
X = dataset.data
y = dataset.target
skf = StratifiedKFold(y, n_folds = 2, shuffle = True)
tst_f1_results= np.zeros(shape = (len(skf), len(range(1, 25))))
i = 0
for train_index, test_index in skf:
i += 1
# Gettin data
(X_train,
X_test,
y_train,
y_test) = X[train_index], X[test_index], y[train_index], y[test_index]
for k in range(1, 25):
classifier = KNeighborsClassifier(n_neighbors = k)
# Training the classifier
classifier.fit(X_train, y_train)
# Predicting
y_pred_test = classifier.predict(X_test)
# Save Results
tst_f1_results[i - 1, k - 1] = f1_score(y_test, y_pred_test, average='macro')
f1_scores = np.average(tst_f1_results, axis=0)
plt.grid()
plt.plot(range(1, 25), f1_scores, 'o-', color="g", label="Cross-validation F1-score")
plt.show()
I was running too many times with differents results as you can see:

- First image suggests that I should choise k (n_neighbors) = between 5 and 11
- Second image suggests that I should choise k (n_neighbors) = 1 or 19.
- Third image suggests that I should choise k (n_neighbors) = 3, 7, 8 or 9.
- Fourth image suggests that I should choise k (n_neighbors) = 5
That results confuses me. Each time, the curve varies so much. I newbiw on machine learning so maybe I'm doing somthing wrong. Is it the correct way to validate a model? Can anyone explain me what should I do?