<jupyter_start><jupyter_text>Генерация выборки<jupyter_code>data = make_classification(n_samples=500, n_features=2, n_informative=2, n_redundant=0,
                           n_classes=3, n_clusters_per_class=1)
X = data[0]
y = data[1]<jupyter_output><empty_output><jupyter_text>Визуализация kNN для разных k<jupyter_code>import matplotlib.pyplot as plt
%matplotlib notebook

k = 2
density = 30
def visualise_set(X, y, k, density):
    ax = plt.figure().add_subplot(111)
    colors = ['r', 'b', 'g', 'y', 'bl']
    for i in range(len(np.unique(y))):
        ax.scatter(X[y == i][:, 0], X[y == i][:, 1], color=colors[i])
    classifier = KNeighborsClassifier(n_neighbors=k)
    classifier.fit(X, y)
    x_ax = np.linspace(np.min(X[:, 0]) - 1, np.max(X[:, 0]) + 1, density)
    y_ax = np.linspace(np.min(X[:, 1]) - 1, np.max(X[:, 1]) + 1, density)
    dens_x = x_ax[1]-x_ax[0]
    dens_y = y_ax[1]-y_ax[0]
    grid = np.transpose([np.tile(x_ax, len(y_ax)), np.repeat(y_ax, len(x_ax))])
    labels = classifier.predict(grid)
    for i in range(len(grid)):
        ax.fill_between([grid[i][0] - dens_x/2, grid[i][0] + dens_x/2],
                        [grid[i][1] - dens_y/2, grid[i][1] - dens_y/2],
                        [grid[i][1] + dens_y/2, grid[i][1] + dens_y/2], color=colors[labels[i]], alpha=0.2)
    plt.show()
visualise_set(X, y, 10, 40)
visualise_set(X, y, 5, 40)
visualise_set(X, y, 2, 40)
visualise_set(X, y, 1, 40)<jupyter_output><empty_output><jupyter_text>Зависимость accuracy от k<jupyter_code>from sklearn.cross_validation import cross_val_score

def get_score(k, X, y):
    est = KNeighborsClassifier(n_neighbors=k)
    return cross_val_score(est, X, y, scoring='accuracy')
ax = plt.figure().add_subplot(111)
max_k = 50
plt.xlim((1, max_k))
k_s = np.arange(1, max_k)
ax.plot(k_s, map(lambda k: np.min(get_score(k, X, y)), k_s))
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18071012188659247, 'fraction_numerical': 0.03391626921038686, 'mean_word_length': 4.075268817204301, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '32346', 'license': 'MIT', 'n_tokens_mistral': 870, 'n_tokens_neox': 831, 'n_words': 149, 'path': 'hw1/Practise_1.ipynb', 'repo_name': 'IKholopov/ml-mipt-hw', 'size': '1887'}