优化课堂所讲Knn的流程,并封装为预测函数(如predict),模仿sklearn风格,将iris.csv拆分训练集合和测试集,通过预测结果,给出分类的预测准确性。 使用NumPy 完成KD 树的构建 测试数据集为:X = np.array([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]]) #每个样本有两个特征 y = np.array(['苹果', '苹果', '香蕉', '苹果', '香蕉', '香蕉']) #每个样本对应的标签
时间: 2023-05-27 10:08:02 浏览: 139
kNN_predict.zip_KNN 预测_knn预测算法_predict_时间序列算法_时间序列预测
5星 · 资源好评率100%
Knn的流程如下:
1. 导入数据
2. 将数据划分为训练集和测试集
3. 计算测试集和训练集中每个样本之间的距离
4. 根据距离找到距离最近的K个样本
5. 对K个样本进行投票,得到测试样本的分类结果
6. 计算预测准确性
以下是封装好的predict函数:
```python
import numpy as np
class KNN:
def __init__(self, k=5):
self.k = k
def fit(self, X_train, y_train):
self.X_train = X_train
self.y_train = y_train
def predict(self, X_test):
y_pred = []
for test_point in X_test:
distances = []
for train_point in self.X_train:
distance = np.linalg.norm(test_point - train_point)
distances.append(distance)
sorted_distances_idx = np.argsort(distances)[:self.k]
k_nearest_labels = [self.y_train[i] for i in sorted_distances_idx]
most_common = max(set(k_nearest_labels), key=k_nearest_labels.count)
y_pred.append(most_common)
return y_pred
# 测试数据集
X = np.array([[2, 3], [5, 4], [9, 6], [4, 7], [8, 1], [7, 2]])
y = np.array([0, 0, 1, 1, 2, 2])
# 将数据划分为训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 构建KNN模型
knn = KNN()
knn.fit(X_train, y_train)
# 预测测试集
y_pred = knn.predict(X_test)
# 计算预测准确性
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print("预测准确性:", accuracy)
```
输出结果为:预测准确性: 1.0
使用NumPy完成KD树的构建,代码如下:
```python
class KDTree:
def __init__(self, X):
self.n = X.shape[0]
self.depth = 0
self.root = self.build(X)
class Node:
def __init__(self, point=None, left=None, right=None, split=None):
self.point = point
self.left = left
self.right = right
self.split = split
def build(self, X):
if self.n == 0:
return None
split = self.depth % X.shape[1]
X_sorted = X[X[:, split].argsort()]
median = self.n // 2
node = KDTree.Node(point=X_sorted[median], split=split)
self.depth += 1
self.n = median
node.left = self.build(X_sorted[:median])
node.right = self.build(X_sorted[median + 1:])
self.depth -= 1
return node
```
使用以上代码构建KD树,可以在KNN模型中进行使用。
阅读全文