更新于
2026年7月11日
K近邻#
K近邻(K-Nearest Neighbor, KNN) 是机器学习中最基础且经典的算法之一,主要用于解决分类问题。
示例代码#
from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
def load_data():
data = load_digits()
x, y = data.data, data.target
x_train, x_test, y_train, y_test =
train_test_split(x, y, test_size=0.3, random_state=10)
ss = StandardScaler()
x_train = ss.fit_transform(x_train)
x_test = ss.transform(x_test)
return x_train, x_test, y_train, y_test
def train(x_train, x_test, y_train, y_test):
model = KNeighborsClassifier(5, p=1)
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
print(classification_report(y_test, y_pred))
print("Accuracy: ", model.score(x_test, y_test))
if __name__ == '__main__':
x_train, x_test, y_train, y_test = load_data()
train(x_train, x_test, y_train, y_test)运行结果#
precision recall f1-score support
0 1.00 1.00 1.00 51
1 0.92 1.00 0.96 57
2 0.96 0.96 0.96 55
3 0.93 0.98 0.96 56
4 1.00 0.94 0.97 51
5 0.96 0.96 0.96 51
6 1.00 1.00 1.00 55
7 0.97 1.00 0.98 60
8 0.91 0.86 0.89 50
9 0.98 0.91 0.94 54
accuracy 0.96 540
macro avg 0.96 0.96 0.96 540
weighted avg 0.96 0.96 0.96 540
Accuracy: 0.9629629629629629
阅读
--