Knowee
Questions
Features
Study Tools

<p>(b) Implement the function <b><code>findknn</code></b>, which should find the $k$ nearest neighbors of a set of vectors within a given training data set. Break ties arbitrarily. The call of <pre> [I,D]=findknn(xTr,xTe,k); </pre> should result in two matrices $I$ and $D$, both of dimensions $k\times m$, where $m$ is the number of input vectors in <code>xTe</code>. The matrix $I(i,j)$ is the index of the $i^{th}$ nearest neighbor of the vector $xTe(j,:)$. So, for example, if we set <code>i=I(1,3)</code>, then <code>xTr(i,:)</code> is the first nearest neighbor of vector <code>xTe(3,:)</code>. The second matrix $D$ returns the corresponding distances. So $D(i,j)$ is the distance of $xTe(j,:)$ to its $i^{th}$ nearest neighbor. </p>

Question

<p>(b) Implement the function <b><code>findknn</code></b>, which should find the kk nearest neighbors of a set of vectors within a given training data set. Break ties arbitrarily. The call of <pre> [I,D]=findknn(xTr,xTe,k); </pre> should result in two matrices II and DD, both of dimensions k×mk\times m, where mm is the number of input vectors in <code>xTe</code>. The matrix I(i,j)I(i,j) is the index of the ithi^{th} nearest neighbor of the vector xTe(j,:)xTe(j,:). So, for example, if we set <code>i=I(1,3)</code>, then <code>xTr(i,:)</code> is the first nearest neighbor of vector <code>xTe(3,:)</code>. The second matrix DD returns the corresponding distances. So D(i,j)D(i,j) is the distance of xTe(j,:)xTe(j,:) to its ithi^{th} nearest neighbor. </p>

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To implement the function findknn, we will use the following steps:

  1. First, we need to calculate the Euclidean distance between each vector in xTe and all vectors in xTr. We can use the l2distance function for this, which calculates the square root of the sum of the squared differences between two sets of vectors.

  2. Once we have the distance matrix, we need to sort it in ascending order along each column. This will give us the indices of the nearest neighbors in xTr for each vector in xTe. We can use the sort function in MATLAB for this, which returns a sorted matrix and an index matrix that can be used to reconstruct the original matrix.

  3. Finally, we need to return the first k rows of the sorted index matrix and the corresponding distances. These are the k nearest neighbors and their distances for each vector in

This problem has been solved

Similar Questions

#<GRADED> def findknn(xTr,xTe,k): """ function [indices,dists]=findknn(xTr,xTe,k); Finds the k nearest neighbors of xTe in xTr. Input: xTr = nxd input matrix with n row-vectors of dimensionality d xTe = mxd input matrix with m row-vectors of dimensionality d k = number of nearest neighbors to be found Output: indices = kxm matrix, where indices(i,j) is the i^th nearest neighbor of xTe(j,:) dists = Euclidean distances to the respective nearest neighbors """ # Enter your code here n,d = xTr.shape m,d = xTe.shape tempDists = l2distance(xTr,xTe); indices=np.argsort(tempDists,axis=0)[:k]; dists=tempDists[indices,np.arange(tempDists.shape[1])[None, :]]; raise NotImplementedError('Your code goes here!') return indices, dists # until here #</GRADED> using python and without using for loop

Identify the difficulties with the k-nearest neighbor algorithm.(1 Point)Curse of dimensionalityCalculate the distance of the test case from all training casesBoth A and BNone of the above

#<GRADED> def knnclassifier(xTr,yTr,xTe,k): """ function preds=knnclassifier(xTr,yTr,xTe,k); k-nn classifier Input: xTr = nxd input matrix with n row-vectors of dimensionality d xTe = mxd input matrix with m row-vectors of dimensionality d k = number of nearest neighbors to be found Output: preds = 1xd vector of predicted labels, ie preds(i) is the predicted label of xTe(i,:) """ # fix array shapes yTr = yTr.flatten() # Your code goes here raise NotImplementedError('Your code goes here!') return preds #</GRADED> python code with no for loop and return preds

Question 4(True/False) K Nearest Neighbors with large k tend to be the best classifiers.1 pointTrueFalse

Identify the difficulties with the k-nearest neighbor algorithm.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.