assignment1_colab/assignment1/cs231n/classifiers/k_nearest_neighbor.py
def predict_labels(self, dists, k=1):
        """
        Given a matrix of distances between test points and training points,
        predict a label for each test point.

        Inputs:
        - dists: A numpy array of shape (num_test, num_train) where dists[i, j]
          gives the distance betwen the ith test point and the jth training point.

        Returns:
        - y: A numpy array of shape (num_test,) containing predicted labels for the
          test data, where y[i] is the predicted label for the test point X[i].
        """
        num_test = dists.shape[0]
        y_pred = np.zeros(num_test)
        for i in range(num_test):
            # A list of length k storing the labels of the k nearest neighbors to
            # the ith test point.
            closest_y = []
            # closest y is probably going to renew everytime we go to a new example
            # because we should be saving the labels in y_pred[i] and returning that
            
            #########################################################################
            # TODO:                                                                 #
            # Use the distance matrix to find the k nearest neighbors of the ith    #
            # testing point, and use self.y_train to find the labels of these       #
            # neighbors. Store these labels in closest_y.                           #
            # Hint: Look up the function numpy.argsort.                             #
            #########################################################################
            # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
            sort_labels = np.argsort(dists)
            # take the first label and match it with the self.y_train
            closest_y.append(self.y_train[sort_labels[0]])
            pass

            # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
            #########################################################################
            # TODO:                                                                 #
            # Now that you have found the labels of the k nearest neighbors, you    #
            # need to find the most common label in the list closest_y of labels.   #
            # Store this label in y_pred[i]. Break ties by choosing the smaller     #
            # label.                                                                #
            #########################################################################
            # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

            pass

            # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****

        return y_pred

        Given a matrix of distances between test points and training points,
        predict a label for each test point.

        Inputs:
        - dists: A numpy array of shape (num_test, num_train) where dists[i, j]
          gives the distance betwen the ith test point and the jth training point.

        Returns:
        - y: A numpy array of shape (num_test,) containing predicted labels for the
          test data, where y[i] is the predicted label for the test point X[i].
        
Given a matrix of distances between test points and training points,
predict a label for each test point.

A numpy array of shape (num_test, num_train) where dists[i, j]
gives the distance betwen the ith test point and the jth training point.
Given a matrix of distances between test points and training points predict a label for each test point . A numpy array of shape ( num_test num_train ) where dists [ i j ] gives the distance betwen the ith test point and the jth training point .
def predict_labels(self, dists, k=1):
        num_test = dists.shape[0]
        y_pred = np.zeros(num_test)
        for i in range(num_test):
            closest_y = []
            sort_labels = np.argsort(dists)
            closest_y.append(self.y_train[sort_labels[0]])
            pass
            pass
        return y_pred
def predict_labels ( self , dists , k = 1 ) : num_test = dists . shape [ 0 ] y_pred = np . zeros ( num_test ) for i in range ( num_test ) : closest_y = [ ] sort_labels = np . argsort ( dists ) closest_y . append ( self . y_train [ sort_labels [ 0 ] ] ) pass pass return y_pred
Given a matrix of distances between test points and training points,
predict a label for each test point.
Given a matrix of distances between test points and training points predict a label for each test point .
"""
        Given a matrix of distances between test points and training points,
        predict a label for each test point.

        Inputs:
        - dists: A numpy array of shape (num_test, num_train) where dists[i, j]
          gives the distance betwen the ith test point and the jth training point.

        Returns:
        - y: A numpy array of shape (num_test,) containing predicted labels for the
          test data, where y[i] is the predicted label for the test point X[i].
        """ # A list of length k storing the labels of the k nearest neighbors to # the ith test point. # closest y is probably going to renew everytime we go to a new example # because we should be saving the labels in y_pred[i] and returning that ######################################################################### # TODO:                                                                 # # Use the distance matrix to find the k nearest neighbors of the ith    # # testing point, and use self.y_train to find the labels of these       # # neighbors. Store these labels in closest_y.                           # # Hint: Look up the function numpy.argsort.                             # ######################################################################### # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** # take the first label and match it with the self.y_train # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** ######################################################################### # TODO:                                                                 # # Now that you have found the labels of the k nearest neighbors, you    # # need to find the most common label in the list closest_y of labels.   # # Store this label in y_pred[i]. Break ties by choosing the smaller     # # label.                                                                # ######################################################################### # *****START OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)***** # *****END OF YOUR CODE (DO NOT DELETE/MODIFY THIS LINE)*****
None
self
dists
k