assignments/assignment1/knn.py
def predict_labels_multiclass(self, dists):
        '''
        Returns model predictions for multi-class classification case
        
        Arguments:
        dists, np array (num_test_samples, num_train_samples) - array
           with distances between each test and each train sample

        Returns:
        pred, np array of int (num_test_samples) - predicted class index 
           for every test sample
        '''
        num_test = dists.shape[0]
        num_test = dists.shape[0]
        pred = np.zeros(num_test, np.int)
        for i in range(num_test):
            # TODO: Implement choosing best class based on k

            indexes_of_k_smallest_values = np.argpartition(dists[i], self.k)[:self.k]

            filter_indices = indexes_of_k_smallest_values
            axis = 0


            labels_of_k_smallest_values = np.take(self.train_y, filter_indices, axis)
            (values, counts) = np.unique(labels_of_k_smallest_values, return_counts=True)
            # print("values:", values)
            # print("counts:", counts)
            pred[i] = values[np.argmax(counts)]

        return pred

        Returns model predictions for multi-class classification case
        
        Arguments:
        dists, np array (num_test_samples, num_train_samples) - array
           with distances between each test and each train sample

        Returns:
        pred, np array of int (num_test_samples) - predicted class index 
           for every test sample
        
Returns model predictions for multi-class classification case
Arguments:
dists, np array (num_test_samples, num_train_samples) - array
with distances between each test and each train sample

pred, np array of int (num_test_samples) - predicted class index
for every test sample
Returns model predictions for multi - class classification case Arguments : dists np array ( num_test_samples num_train_samples ) - array with distances between each test and each train sample pred np array of int ( num_test_samples ) - predicted class index for every test sample
def predict_labels_multiclass(self, dists):
        num_test = dists.shape[0]
        num_test = dists.shape[0]
        pred = np.zeros(num_test, np.int)
        for i in range(num_test):
            indexes_of_k_smallest_values = np.argpartition(dists[i], self.k)[:self.k]
            filter_indices = indexes_of_k_smallest_values
            axis = 0
            labels_of_k_smallest_values = np.take(self.train_y, filter_indices, axis)
            (values, counts) = np.unique(labels_of_k_smallest_values, return_counts=True)
            pred[i] = values[np.argmax(counts)]
        return pred
def predict_labels_multiclass ( self , dists ) : num_test = dists . shape [ 0 ] num_test = dists . shape [ 0 ] pred = np . zeros ( num_test , np . int ) for i in range ( num_test ) : indexes_of_k_smallest_values = np . argpartition ( dists [ i ] , self . k ) [ : self . k ] filter_indices = indexes_of_k_smallest_values axis = 0 labels_of_k_smallest_values = np . take ( self . train_y , filter_indices , axis ) ( values , counts ) = np . unique ( labels_of_k_smallest_values , return_counts = True ) pred [ i ] = values [ np . argmax ( counts ) ] return pred
Returns model predictions for multi-class classification case
Arguments:
dists, np array (num_test_samples, num_train_samples) - array
with distances between each test and each train sample
Returns model predictions for multi - class classification case Arguments : dists np array ( num_test_samples num_train_samples ) - array with distances between each test and each train sample
'''
        Returns model predictions for multi-class classification case
        
        Arguments:
        dists, np array (num_test_samples, num_train_samples) - array
           with distances between each test and each train sample

        Returns:
        pred, np array of int (num_test_samples) - predicted class index 
           for every test sample
        ''' # TODO: Implement choosing best class based on k # print("values:", values) # print("counts:", counts)
self
dists