`def knn_train_test(df, train_col, target_col):`

` #shuffling the data`

`np.random.seed(1)`

`shuffled_index = np.random.permutation(df.index)`

`shuffled_df = df.reindex(shuffled_index)`

`#shuffled_df = shuffled_df.drop([0], axis=0)`

```
`#spliting the dataset into train and test set`
`split_value = int(len(shuffled_df)/2)`
`train_set = shuffled_df.iloc[0:split_value]`
`test_set = shuffled_df.iloc[split_value:]`
`k_values = [1, 3, 5, 7, 9]`
`k_rmses = {}`
`#training and predicting`
`for k in k_values:`
` knn = KNeighborsRegressor(n_neighbors=k)`
` knn.fit(train_set[[train_col]], train_set[target_col])`
` predictions = knn.predict(test_set[[train_col]])`
`mse = mean_squared_error(test_set[target_col], predictions)`
`rmse = np.sqrt(mse)`
`k_rmses[k] = rmse`
`return k_rmses`
```

`# training all the numeric columns in normalized_cars using knn_train_test function`

`target_cols = normalized_cars.columns.drop('price')`

`k_rmses_target = {}`

`for cols in target_cols:`

`rmse_vals = knn_train_test(normalized_cars, cols, 'price')`

`k_rmses_target[cols]=rmse_vals`

`import matplotlib.pyplot as plt`

`%matplotlib inline`

`for k,v in sorted(k_rmses_target.items()):`

`x = list(v.keys())`

`y = list(v.values())`

```
`plt.plot(x,y, "", markersize=20)`
`plt.xlabel('k value')`
`plt.ylabel('RMSE')`
```