# 送你一份使用k近邻算法实现回归的实用指南（附代码、链接）

#### 目录

1. 一个简单的例子来理解KNN背后的直观解释

2. KNN算法是如何工作的?

3. 点之间距离的计算方法

4. 如何选择k因子?

5. 应用在一个数据集上

6. 额外的资源

#### 4. 如何选择k因子?

ID11的预测体重是：

```ID11 = (77+72+60)/3
ID11 = 69.66 kg```

ID11的预测体重是：

```ID 11 =  (77+59+72+60+58)/5
ID 11 = 65.2 kg```

K值很低时（假设k = 1）,该模型过拟合训练数据,从而导致验证集的错误率很高。另一方面，k取较大值时,模型在训练集和验证集上表现都很差。如果你仔细观察，验证误差曲线的值在k = 9时达到最小值，此时k值是模型的最优值（根据不同的数据集会有所不同）。这条曲线被称为“手肘曲线”（因为它的形状很像手肘），通常用于确定k值。

#### 5. 应用在一个数据集上

```import pandas as pd

df.isnull().sum()

#missing values in Item_weight and Outlet_size needs to be imputed

mean = df[‘Item_Weight’].mean() #imputing item_weight with mean

df[‘Item_Weight’].fillna(mean, inplace =True)

mode = df[‘Outlet_Size’].mode() #imputing outlet size with mode

df[‘Outlet_Size’].fillna(mode[0], inplace =True)

```df.drop(['Item_Identifier', 'Outlet_Identifier'], axis=1, inplace=True)
df = pd.get_dummies(df)```

```from sklearn.model_selection import train_test_split
train , test = train_test_split(df, test_size = 0.3)

x_train = train.drop('Item_Outlet_Sales', axis=1)
y_train = train['Item_Outlet_Sales']

x_test = test.drop('Item_Outlet_Sales', axis = 1)
y_test = test['Item_Outlet_Sales']```

```from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))

x_train_scaled = scaler.fit_transform(x_train)
x_train = pd.DataFrame(x_train_scaled)

x_test_scaled = scaler.fit_transform(x_test)
x_test = pd.DataFrame(x_test_scaled)```

```#import required packages
from sklearn import neighbors
from sklearn.metrics import mean_squared_error
from math import sqrt
import matplotlib.pyplot as plt
%matplotlib inline
rmse_val = [] #to store rmse values for different k
for K in range(20):
K = K+1
model = neighbors.KNeighborsRegressor(n_neighbors = K)

model.fit(x_train, y_train)  #fit the model
pred=model.predict(x_test) #make prediction on test set```

```    error = sqrt(mean_squared_error(y_test,pred)) #calculate rmse
rmse_val.append(error) #store rmse values
print('RMSE value for k= ' , K , 'is:', error)```

```RMSE value for k = 1 is: 1579.8352322344945
RMSE value for k = 2 is: 1362.7748806138618
RMSE value for k = 3 is: 1278.868577489459
RMSE value for k = 4 is: 1249.338516122638
RMSE value for k = 5 is: 1235.4514224035129
RMSE value for k = 6 is: 1233.2711649472913
RMSE value for k = 7 is: 1219.0633086651026
RMSE value for k = 8 is: 1222.244674933665
RMSE value for k = 9 is: 1219.5895059285074
RMSE value for k = 10 is: 1225.106137547365
RMSE value for k = 11 is: 1229.540283771085
RMSE value for k = 12 is: 1239.1504407152086
RMSE value for k = 13 is: 1242.3726040709887
RMSE value for k = 14 is: 1251.505810196545```

```RMSE value for k = 15 is: 1253.190119191363
RMSE value for k = 16 is: 1258.802262564038
RMSE value for k = 17 is: 1260.884931441893
RMSE value for k = 18 is: 1265.5133661294733
RMSE value for k = 19 is: 1269.619416217394
RMSE value for k = 20 is: 1272.10881411344

#plotting the rmse values against k values
curve = pd.DataFrame(rmse_val) #elbow curve
curve.plot()```

submission[‘Item_Identifier’] = test[‘Item_Identifier’]

submission[‘Outlet_Identifier’] = test[‘Outlet_Identifier’]

#preprocessing test dataset

test.drop([‘Item_Identifier’, ‘Outlet_Identifier’], axis=1, inplace=True)

test[‘Item_Weight’].fillna(mean, inplace =True)

test = pd.get_dummies(test)

test_scaled = scaler.fit_transform(test)

test = pd.DataFrame(test_scaled)

```#predicting on the test set and creating submission file
predict = model.predict(test)
submission['Item_Outlet_Sales'] = predict
submission.to_csv('submit_file.csv',index=False)```

```from sklearn.model_selection import GridSearchCV
params = {'n_neighbors':[2,3,4,5,6,7,8,9]}
knn = neighbors.KNeighborsRegressor(
model = GridSearchCV(knn, params, cv=5)
model.fit(x_train,y_train)
model.best_params```

`{'n_neighbors': 7}`

#### 译者附：注册下载数据集流程

1、注册一个账号，然后注册这个比赛

2、点击data

A Practical Introduction to K-Nearest Neighbors Algorithm for Regression （with Python code）