knn算法python代码
时间: 2023-10-12 14:14:45 浏览: 71
以下是一个简单的knn算法的Python代码:
```python
from math import sqrt
# 计算两个向量之间的欧几里德距离
def euclideanDistance(instance1, instance2, length):
distance = 0
for i in range(length):
distance += pow((instance1[i] - instance2[i]), 2)
return sqrt(distance)
# 返回训练数据集中与测试实例距离最近的k个实例
def getNeighbors(trainingSet, testInstance, k):
distances = []
length = len(testInstance)-1
for x in range(len(trainingSet)):
dist = euclideanDistance(testInstance, trainingSet[x], length)
distances.append((trainingSet[x], dist))
distances.sort(key=lambda x: x[1])
neighbors = []
for x in range(k):
neighbors.append(distances[x][0])
return neighbors
# 针对给定测试集knn算法的主要功能
def getResponse(neighbors):
classVotes = {}
for x in range(len(neighbors)):
response = neighbors[x][-1]
if response in classVotes:
classVotes[response] += 1
else:
classVotes[response] = 1
sortedVotes = sorted(classVotes.items(), key=lambda x: x[1], reverse=True)
return sortedVotes[0][0]
# 通过计算准确性来评估模型
def getAccuracy(testSet, predictions):
correct = 0
for x in range(len(testSet)):
if testSet[x][-1] == predictions[x]:
correct += 1
return (correct / float(len(testSet))) * 100.0
# 测试knn算法
def main():
# 模拟训练集
trainSet = [[2, 2, 2, 'a'], [4, 4, 4, 'b'], [5, 5, 5, 'b'], [3, 3, 3, 'a'], [6, 6, 6, 'b'], [8, 8, 8, 'b'], [7, 7, 7, 'a']]
# 模拟测试集
testSet = [[5, 5, 5, 'a'], [2, 2, 2, 'b'], [3, 3, 3, 'a'], [8, 8, 8, 'b']]
# 设置k值
k = 3
predictions = []
for x in range(len(testSet)):
neighbors = getNeighbors(trainSet, testSet[x], k)
result = getResponse(neighbors)
predictions.append(result)
print(f"预测值为{result}")
accuracy = getAccuracy(testSet, predictions)
print('准确率: ' + repr(accuracy) + '%')
if __name__ == '__main__':
main()
```
这是一个基本的knn算法代码,适用于任何数据集。当然,您可以根据需要对其进行修改和扩展。
阅读全文