<jupyter_start><jupyter_text>kNN 基础<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
raw_data_X = [
    [3.393533211, 2.331273381],
    [3.110073483, 1.781539638],
    [1.343808831, 3.368360954],
    [3.582294042, 4.679179110],
    [2.280362439, 2.866990263],
    [7.423436942, 4.696522875],
    [5.745051997, 3.533989803],
    [9.172168622, 2.511101045],
    [7.792783481, 3.424088941],
    [7.939820817, 0.791637231]
]  # 两列分别代表肿瘤的长和宽
raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] # 0是良性肿瘤，1是恶性肿瘤
X_train = np.array(raw_data_X)
y_train = np.array(raw_data_y)
X_train
y_train
plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g', marker="+") # X_train[y_train==0,0]表示从X_train中取y_train中等于0的索引对应的行的第0列
plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r', marker="*")
plt.show()
x = np.array([8.093607318, 3.365731514])
plt.scatter(x[0], x[1], color="blue", marker="o")
plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g', marker="+")
plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r', marker="*")
plt.show()<jupyter_output><empty_output><jupyter_text>kNN的过程<jupyter_code>x # x是待预测的点，从上面图可以看出属于y_train中的1，下面来用理论证实下
from math import sqrt
distances = []
# 求出每个点到制定点的距离，所有的距离组成距离数组distances
for x_train in X_train:
    d = sqrt(np.sum((x_train - x)**2)) # np.sum默认沿着行方向(axis=0)，每行算一个点去求和
    distances.append(d)
distances
np.argsort(distances) # 排序后返回元素的索引
nearest = np.argsort(distances) # 排序后返回元素的索引
nearest
k = 6 # K近邻算法，取最近的6个点
topK_y = [y_train[i] for i in nearest[:k]]  # 取最近的k个点的下表，对应着去y_train中把指定下标的元素取出来
topK_y  # 最近的6个点，5个对应的结果都是同一类
# 下面对topk_y中的元素分布做一下统计
from collections import Counter
Counter(topK_y)
votes = Counter(topK_y)
votes.most_common(2) # 找出票数最多的n个元素，2就是n的具体值
votes.most_common(1) # 找出票数最多的n个元素，1就是n的具体值
predict_y = votes.most_common(1)[0][0]  # 注意取出具体的预测的值的方法
predict_y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16411609498680738, 'fraction_numerical': 0.14142480211081795, 'mean_word_length': 6.553784860557769, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '127894', 'license': 'MulanPSL-1.0', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1094, 'n_words': 126, 'path': 'Part5Improve/04-kNN/01-kNN-Basics/01-kNN-Basics.ipynb', 'repo_name': '19920625lsg/itcast-python', 'size': '1895'}