k近邻算法python代码
时间: 2023-08-18 12:07:00 浏览: 75
下面是一个简单的k近邻算法的Python代码示例:
```python
from math import sqrt
# 计算两个点之间的欧几里得距离
def euclidean_distance(point1, point2):
distance = 0.0
for i in range(len(point1)):
distance += (point1[i] - point2[i]) ** 2
return sqrt(distance)
# 找到k个最近邻居
def get_neighbors(train_set, test_instance, k):
distances = []
for train_instance in train_set:
dist = euclidean_distance(test_instance, train_instance)
distances.append((train_instance, dist))
distances.sort(key=lambda x: x[1])
neighbors = []
for i in range(k):
neighbors.append(distances[i][0])
return neighbors
# 预测测试实例的类别
def predict_classification(train_set, test_instance, k):
neighbors = get_neighbors(train_set, test_instance, k)
output_values = [instance[-1] for instance in neighbors]
prediction = max(set(output_values), key=output_values.count)
return prediction
```
这个代码可以用于处理带有数值属性的数据集。你需要将数据集分成训练集和测试集,并将每个实例表示为一个包含数值属性和类别的列表。例如,对于一个含有3个属性和1个类别标签的数据集,可以表示为一个列表的列表:
```python
dataset = [[2.7810836,2.550537003,0],
[1.465489372,2.362125076,0],
[3.396561688,4.400293529,0],
[1.38807019,1.850220317,0],
[3.06407232,3.005305973,0],
[7.627531214,2.759262235,1],
[5.332441248,2.088626775,1],
[6.922596716,1.77106367,1],
[8.675418651,-0.242068655,1],
[7.673756466,3.508563011,1]]
```
对于每个测试实例,可以使用`predict_classification`函数预测它的类别。例如,对于测试实例`[5.0, 2.0]`,可以这样预测:
```python
k = 3
prediction = predict_classification(dataset, [5.0, 2.0], k)
print('Prediction:', prediction)
```
输出结果应该为`Prediction: 1`。
阅读全文