<jupyter_start><jupyter_text>Split train test<jupyter_code>X_ = X/7847.1504
split_pct = 0.8
indexes = list(range(len(X)))
random.shuffle(indexes)

train_idx = indexes[:int(split_pct*len(indexes))]
test_idx = indexes[int(split_pct*len(indexes)):]

print(len(train_idx), len(test_idx))
np.sum(labels_[train_idx]==1), np.sum(labels_[train_idx]==0)
np.sum(labels_[test_idx]==1), np.sum(labels_[test_idx]==0)
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import precision_score, recall_score, f1_score, precision_recall_curve, confusion_matrix

from sklearn.metrics import plot_confusion_matrix

knn = KNeighborsClassifier(n_neighbors=8, p=2)
knn.fit(X_[train_idx], labels_[train_idx])
knn.score(X_[train_idx], labels_[train_idx])
knn.score(X_[test_idx], labels_[test_idx])
test_output = knn.predict(X_[test_idx])
precision_score(labels_[test_idx], test_output), recall_score(labels_[test_idx], test_output)
f1_score(labels_[test_idx], test_output)
# knn.predict_proba(X_[test_idx])
precision, recall, thresh = precision_recall_curve(
                                labels_[test_idx],
                                knn.predict_proba(X_[test_idx])[..., 1]
                            )
precision
plt.plot(precision)
plt.plot(recall)

plt.legend(('precision', 'recall'))
plt.show()
cm = confusion_matrix(
        y_true=labels_[test_idx],
        y_pred=knn.predict(X_[test_idx])
    )
disp = plot_confusion_matrix(knn, X_[test_idx], labels_[test_idx], 
                          labels=(0, 1),
                          display_labels=('not covid', 'covid'),
                          cmap=plt.cm.Oranges
                         )

disp.ax_.set_title('Confusion Matrix of KNN score')
plt.savefig('../images/knn-confusion-matrix.png')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.135706340378198, 'fraction_numerical': 0.01167964404894327, 'mean_word_length': 3.9833795013850417, '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': '404670', 'license': 'MIT', 'n_tokens_mistral': 666, 'n_tokens_neox': 633, 'n_words': 87, 'path': 'nbs/02-01-chexnet-knn.ipynb', 'repo_name': 'aniketmaurya/covid-research', 'size': '1798'}