Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 . R programming: The first task is to write the code to implement the K - Nearest Neighbors, or KNN , model from scratch.
R programming: The first task is to write the code to implement the KNearest Neighbors, or KNN model from
scratch. We will do this in steps:
Write a function called euclideandistance that calculates the Euclidean distance between two vectors.
There are two input arguments for this function: vector vec and vector vec The output for
this function is a numeric, the Euclidean distance euclDist
Write a function called manhattandistance that calculates the Manhattan distance between two
vectors. There are two input arguments for this function: vector vec and vector vec The
output for this function is a numeric, the Manhattan distance manhDist
Write a function called euclideandistanceall that calculates the Euclidean distance between a
vector and all the row vectors in an input data matrix. There are two input arguments for this
function: a vector vec and an input data matrix matX The output for this function is a vector
outputeuclDistVec which is of the same length as the number of rows in matX This function
must use the function euclideandistance you previously wrote.
Write a function called manhattandistanceall that calculates the Manhattan distance between
a vector and all the row vectors in an input data matrix. There are two input arguments for this
function: a vector vec and an input data matrix matX The output for this function is a vector
outputmanhattanDistVec which is of the same length as the number of rows in matX This function
must use the function manhattandistance you previously wrote.
Write a function called myKNN that compares a vector to a matrix and finds its Knearest neighbors.
There are five input arguments for this function: vector vec the input data matrix matX the
class labels corresponding to each row of the matrix matY the number of nearest neighbors you are
interested in finding K and a Boolean argument specifying if we are using the Euclidean distance
euclDistUsed The argument K should be a positive integer. If the argument euclDistUsed TRUE,
then use the Euclidean distance. Otherwise, use the Manhattan distance. The output of this function
is a list of length outputknnMajorityVote The first element in the output list should be a vector
of length K containing the class labels of the closest neighbors. The second element in the output list
should be the majority vote of the K class labels in the first element of the list. The function must use
the functions euclideandistance and manhattandistance you previously wrote.
Apply this function to predict the label of the rd observation using the first observations as your input
training data matrix. Use K What is the predicted label when you use Euclidean distance? What is
the predicted label when you use Manhattan distance? Are these predictions correct
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started