from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd
def knn_test():
data_url = "/data/workspace/myshixun/home/iris_train.csv"
df = pd.read_csv(data_url)
X = df.iloc[:,1:4]
y=df.iloc[:,4]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# 利用KNeighborsClassifier函数制作knn分类器
# 选取最近的点的个数n_neighbors=3
clf = KNeighborsClassifier(n_neighbors=3)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
acc = np.sum(y_test == y_pred) / X_test.shape[0]
return acc
# 测试函数
test_acc = knn_test()
print("Test Acc:", test_acc)
第1关:KNN分类器
最新推荐文章于 2024-11-13 17:26:08 发布