训练数据集
根据给定的距离度量,在训练集T中找出与x最邻近的k个点,涵盖这k个点的x 的邻域记作
; 在
中根据分类决策规则(如多数表决)决定x的类别y: k近邻法的特殊情况是k=1的情形,称为最近邻算法。对于输入的实例点(特征向 量)x,最近邻法将训练数据集中与x最邻近点的类作为x的类。
# -*- coding:utf-8 -*-
# /usr/bin/python
'''
-------------------------------------------------------------------------
@File Name : knn.py
@Description :
@Run Script : python knn.py
@Envs : pip install
@Change Activity:
1. 2021/12/3 下午1:48 : build
-------------------------------------------------------------------------
@CodeStyle : standard, simple, readable, maintainable, and portable!
@Author : Yan Errol 13075851954
@Email : 260187357@qq.com
@Copyright : "Copyright 2021, Yan Errol"
-------------------------------------------------------------------------
'''
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from collections import Counter
# 加载读取数据
data = pd.read_csv("dataset.csv")
print(data.label.value_counts())
print("data\n",data)
# 数据可视化 验证线性可分性
plt.scatter(data[:50]['sepal length'], data[:50]['sepal width'], label='0')
plt.scatter(data[50:100]['sepal length'], data[50:100]['sepal width'], label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()
plt.show()
plt.savefig("show.png")
# 数据集拆分
data = np.array(data.iloc[:100,[0,1,-1]])
X,Y = data[:,:-1],data[:, -1]
XTrain,xtest,YTrain,ytest = train_test_split(X,Y,test_size=0.2)
class KNN():
def __init__(self,XTrain,YTrain,nNeighbors=5,p=2):
self.XTrain = XTrain
self.YTrain = YTrain
self.nNeighbors = nNeighbors
self.p = p
def predict(self,X):
'''预测'''
# 全局遍历
knnlist = []
for i in range(len(self.XTrain)):
dist = np.linalg.norm(X - self.XTrain[i], ord=self.p)
knnlist.append([round(dist,2), self.YTrain[i]])
knnlist = sorted(knnlist,key=lambda x:x[0])
minKnnlist = knnlist[0:self.nNeighbors]
# 统计
knn = [k[-1] for k in minKnnlist]
count_pairs = Counter(knn)
max_count = sorted(count_pairs, key=lambda x: x)[-1]
return max_count
def score(self,X_test, y_test):
right_count = 0
n = 10
for X, y in zip(X_test, y_test):
label = self.predict(X)
if label == y:
right_count += 1
return right_count / len(X_test)
clf = KNN(XTrain, YTrain)
resultScore = clf.score(xtest, ytest)
print(resultScore)