max_k =50
accuracies = np.zeros(max_k, dtype='float64')
cv = model_selection.StratifiedKFold(n_splits=5)
for k in range(max_k + 1)[1:]:
    estimator = neighbors.KNeighborsClassifier(n_neighbors=k)
    accuracies[k - 1] = model_selection.cross_val_score(estimator, 
                                                        x, y, cv=cv).mean()

plt.figure(figsize=(14, 8))
plt.title('Accuracy(k), Stratified')
plt.xlabel('k')
plt.ylabel('Accuracy')
plt.grid(True)
plt.plot(range(max_k + 1)[1:], accuracies)
plt.show()

print 'for 4 classes best k is', accuracies.argmax() + 1

"""
Explanation: Посмотрим на точность в зависимости от $k$ при кросс-валидации на 5 стратифицированных фолдов.
End of explanation
"""


max_k =50
accuracies = np.zeros(max_k, dtype='float64')
cv = model_selection.KFold(n_splits=5)
for k in range(max_k + 1)[1:]:
    estimator = neighbors.KNeighborsClassifier(n_neighbors=k)
    accuracies[k - 1] = model_selection.cross_val_score(estimator, 
                                                        x, y, cv=cv).mean()
    
plt.figure(figsize=(14, 8))
plt.title('Accuracy(k)')
plt.xlabel('k')
plt.ylabel('Accuracy')
plt.grid(True)
plt.plot(range(max_k + 1)[1:], accuracies)
plt.show()

print 'for 4 classes best k is', accuracies.argmax() + 1

"""
Explanation: Посмотрим на точность в зависимости от $k$ при кросс-валидации на 5 обычных фолдов.
End of explanation
"""


(x, y) = datasets.make_classification(n_samples=500, n_features=2, 
                                                      n_informative=2, n_classes=3, 
                                                      n_redundant=0, 
                                                      n_clusters_per_class=1, 
                                                      random_state=42)

max_k =50
accuracies = np.zeros(max_k, dtype='float64')
cv = model_selection.StratifiedKFold(n_splits=5)
for k in range(max_k + 1)[1:]:
    estimator = neighbors.KNeighborsClassifier(n_neighbors=k)
    accuracies[k - 1] = model_selection.cross_val_score(estimator, 
                                                        x, y, cv=cv).mean()
    
plt.figure(figsize=(14, 8))
plt.title('Accuracy(k)')
plt.xlabel('k')
plt.ylabel('Accuracy')
plt.grid(True)
plt.plot(range(max_k + 1)[1:], accuracies)
plt.show()
    
print 'for 3 classes best k is', accuracies.argmax() + 1

"""
Explanation: Посмотрим, что будет на 3 классах.
End of explanation
"""


(x, y) = datasets.make_classification(n_samples=500, n_features=2, 
                                                      n_informative=2, n_classes=2, 
                                                      n_redundant=0, 
                                                      n_clusters_per_class=1, 
                                                      random_state=42)

max_k =50
accuracies = np.zeros(max_k, dtype='float64')
cv = model_selection.StratifiedKFold(n_splits=5)
for k in range(max_k + 1)[1:]:
    estimator = neighbors.KNeighborsClassifier(n_neighbors=k)
    accuracies[k - 1] = model_selection.cross_val_score(estimator, 
                                                        x, y, cv=cv).mean()
    
plt.figure(figsize=(14, 8))
plt.title('Accuracy(k)')
plt.xlabel('k')
plt.ylabel('Accuracy')
plt.grid(True)
plt.plot(range(max_k + 1)[1:], accuracies)
plt.show()
    
print 'for 2 classes best k is', accuracies.argmax() + 1

"""
Explanation: Посмотрим, что будет на 2 классах
End of explanation
"""


