<jupyter_start><jupyter_text><jupyter_code># k-nearest neighbors algorithm (k-NN)
# author: <NAME>
# license: MIT License # code: github.com/gressling/examples
# activity: single example # index: 36-1 
# case k = 3 (solid circle) -> assigned to triangle
 # (2 triangles and 1 square in circle)
# case k = 5 (dashed circle) -> assigned to square
 # (3 squares and 2 triangles in circle)
# https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KNeighborsClassifier.html
from sklearn.neighbors import KNeighborsClassifier
X = [[0], [1], [2], [3]]
y = [0, 0, 1, 1]
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X, y)
print(neigh.predict([[1.1]]))
print(neigh.predict_proba([[0.9]]))<jupyter_output>[[0.66666667 0.33333333]]
{'fraction_non_alphanumeric': 0.15519568151147098, 'fraction_numerical': 0.05398110661268556, 'mean_word_length': 5.396551724137931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '634315', 'license': 'MIT', 'n_tokens_mistral': 309, 'n_tokens_neox': 275, 'n_words': 69, 'path': '36-1.1 k-nearest neighbors algorithm (k-NN).ipynb', 'repo_name': 'Gressling/examples', 'size': '747'}