python KNN回归预测
时间: 2023-12-30 14:24:42 浏览: 114
KNN回归是一种基于邻近样本的回归算法,它通过计算待预测样本与训练集中各个样本之间的距离,选取距离最近的K个样本,并根据这K个样本的输出值进行预测。下面是两种实现KNN回归的方式:
方式一:使用sklearn库中的KNeighborsRegressor类实现KNN回归算法。
```python
from sklearn.neighbors import KNeighborsRegressor
# 创建KNN回归模型,设置K值为3
knn = KNeighborsRegressor(n_neighbors=3)
# 训练模型
X_train = [[0], [1], [2], [3]]
y_train = [0, 0.2, 0.4, 0.6]
knn.fit(X_train, y_train)
# 预测新样本
X_test = [[1.5]]
y_pred = knn.predict(X_test)
print("预测结果:", y_pred) # 输出:[0.2]
```
方式二:自己实现KNN回归算法。
```python
import numpy as np
# 计算欧氏距离
def euclidean_distance(x1, x2):
return np.sqrt(np.sum((x1 - x2) ** 2))
# KNN回归算法
def knn_regression(X_train, y_train, X_test, k):
y_pred = []
for x_test in X_test:
# 计算测试样本与训练样本的距离
distances = [euclidean_distance(x_test, x_train) for x_train in X_train]
# 根据距离排序,选取距离最近的K个样本
k_indices = np.argsort(distances)[:k]
# 取K个样本的输出值的平均作为预测值
y_pred.append(np.mean(y_train[k_indices]))
return y_pred
# 训练样本
X_train = np.array([[0], [1], [2], [3]])
y_train = np.array([0, 0.2, 0.4, 0.6])
# 预测样本
X_test = np.array([[1.5]])
# 调用KNN回归算法进行预测
y_pred = knn_regression(X_train, y_train, X_test, k=3)
print("预测结果:", y_pred) # 输出:[0.2]
```
阅读全文