Question: Using an example of kNN ( k - Nearest Neighbors ) model used for predicting concentration of nitrogen oxides ( NOx ) in Boston air,

Using an example of kNN (k-Nearest Neighbors) model used for predicting concentration of nitrogen oxides (NOx) in Boston air, based on a single predictor - weighted average distance from 5 employment centers in Boston area. In this question you are going to tune the hyperparameter , i.e. find its optimal value, and then fit the model with that value of . But first, you will produce the following plot from class, which shows how performance of the fitted model varies with the change of the tuning parameter . We will use simple validation and split the data into train and test subsets. To simplify, we first extract the two variables nox and dist from Boston data frame from MASS library, and create a new data frame called df. To do that, run the following cell. Create data frames train and test from the data frame df. The data frame train should consist of observations (i.e. rows) of df whose indices are precisely those 400 random indices from vector tr. The data frame test should consist of the complement of tr (you can use df[-tr,] for taking the complementary indices, i.e. precisely those indices which are not in tr).
Hint: First two rows of train should be
xall yall
4151.65826.93
4632.73447.13
and of test:
xall yall
14.09005.38
66.06224.58

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!