<jupyter_start><jupyter_text>kNN 算法 应用简单实现根据下述过程可以看到，可以说kNN是一个不需要训练过程的算法。k近邻算法是非常特殊的，可以被认为是没有模型的算法。为了和其他算法统一，可以认为训练数据集就是模型本身。<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

raw_data_X = [[3.393, 2.331],
             [3.110, 1.782],
             [1.343, 3.368],
             [3.582, 4.679],
             [2.280, 2.967],
             [7.423, 4.697],
             [5.745, 3.534],
             [9.172, 2.511],
             [7.792, 3.424],
             [7.940, 0.792]]

raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]

X_train = np.array(raw_data_X)
y_train = np.array(raw_data_y)

x = np.array([8.094, 3.366])
plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g')
plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r')
plt.scatter(x[0], x[1], color='b')
plt.show()
%run kNN_simple_implement.py
predict_y = kNN_classify(k=3, X_train=X_train, y_train=y_train, x=x)
predict_y<jupyter_output><empty_output><jupyter_text>使用scikit-learn中的kNN<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
kNN_classifier = KNeighborsClassifier(n_neighbors=6)
kNN_classifier.fit(X_train, y_train)
# 将要预测的数据放入一个矩阵
X_predict = x.reshape(1, -1)
y_predict = kNN_classifier.predict(X_predict)
y_predict[0]<jupyter_output><empty_output><jupyter_text>重新整理kNN代码<jupyter_code>%run kNN.py
kNN_classifier_re = kNNClassifier(6)
kNN_classifier_re.fit(X_train, y_train)
y_predict_re = kNN_classifier_re.predict(X_predict)
y_predict_re[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15151515151515152, 'fraction_numerical': 0.07744107744107744, 'mean_word_length': 4.67175572519084, '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': '399921', 'license': 'Apache-2.0', 'n_tokens_mistral': 796, 'n_tokens_neox': 737, 'n_words': 89, 'path': 'kNN/1.kNN.ipynb', 'repo_name': 'TobiahShaw/machine-learning-algorithm', 'size': '1485'}