# Python实现kNN回归算法

import numpy as np

#用于显示进度

from tqdm import tqdm

#将数据x和y进行划分（划分成train和test）

#如需对数据进行标准化，可使用下述函数

def normData(dataSet):

maxVals = dataSet.max(axis=0)

minVals = dataSet.min(axis=0)

ranges = maxVals - minVals

retData = (dataSet - minVals) / ranges

return retData

#单个样本进行预测

def kNN(dataSet, blowers, testData, k):

distSquareMat = (dataSet - testData) ** 2

distSquareSums = distSquareMat.sum(axis=1)

distances = distSquareSums ** 0.5

sortedIndices = distances.argsort()

indices = sortedIndices[:k]

blowerList = []

for i in indices:

blower = blowers[i]

blowerList.append(blower)

s = 0

for j in blowerList:

s += j

result = s/len(blowerList)

return result

#多个样本进行预测（testData为多元数组，blowers为一元数组，dataSet为多元数组）

def predict(dataSet, blowers, testData, k):

predict_result = []

for i in range(len(testData)):

result = kNN(dataSet, blowers, testData[i], k)

predict_result.append(result)

predict_result = np.array(predict_result)

return predict_result

r2_list = []

for i in tqdm(range(15)):#此处的15可自由设定

y_pred = predict(x_train_norm, y_train, x_test_norm, k)

r2 = r2_score(y_test, y_pred)

r2_list.append(r2)

KNeighborsRegressor.fit(X=train_x,y=train_y)

fit(self, X, y) Fit the model using X as training data and y as target values Parameters ---------- X : Training data. If array or matrix, shape [n_samples, n_features], or [n_samples, n_samples] if metric='precomputed'. y : Target values, array of float values, shape = [n_samples] or [n_samples, n_outputs]

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180823G1JUUU00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2021-11-30

2021-11-30

2021-11-30

2018-05-31

2018-05-30

2018-05-29

2018-05-16

2018-05-14

2018-04-13