Using pandas, fit a K-nearest neighbors model with a value of `k=3` to this data and predict the outcome on the same data. Then, write a function to calculate accuracy using the actual and predicted labels and use the function, to calculate the accuracy of this K-nearest neighbors model on the data. Afterward, fit the K-nearest neighbors model again with `n_neighbors=3`, but apply a strategy through which the distance of neighbors will be counted or valued. So, this time use distance for the weights. Calculate the accuracy using the function created above. (use a value of 1.0 for weighted distances). Then, fit another K-nearest neighbors model. This time use uniform weights but set the power parameter for the Minkowski distance metric to be 1 (`p=1`) i.e. Manhattan Distance. Finally fit a K-nearest neighbors model using values of `k` (`n_neighbors`) ranging from 1 to 20. Use uniform weights (the default). The coefficient for the Minkowski distance (`p`) can be set to either 1 or 2--just be consistent. Store the accuracy and the value of `k` used from each of these fits in a list or dictionary. Plot (or view the table of) the `accuracy` vs `k`. What do you notice happens when `k=1`? Why do you think this is?
Using pandas, fit a K-nearest neighbors model with a value of `k=3` to this data and predict the outcome on the same data. Then, write a function to calculate accuracy using the actual and predicted labels and use the function, to calculate the accuracy of this K-nearest neighbors model on the data.
Afterward, fit the K-nearest neighbors model again with `n_neighbors=3`, but apply a strategy through which the distance of neighbors will be counted or valued. So, this time use distance for the weights. Calculate the accuracy using the function created above. (use a value of 1.0 for weighted distances). Then, fit another K-nearest neighbors model. This time use uniform weights but set the power parameter for the Minkowski distance metric to be 1 (`p=1`) i.e. Manhattan Distance. Finally fit a K-nearest neighbors model using values of `k` (`n_neighbors`) ranging from 1 to 20. Use uniform weights (the default). The coefficient for the Minkowski distance (`p`) can be set to either 1 or 2--just be consistent. Store the accuracy and the value of `k` used from each of these fits in a list or dictionary. Plot (or view the table of) the `accuracy` vs `k`. What do you notice happens when `k=1`? Why do you think this is?
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images