assignments/assignment1/knn.py
def predict_labels_binary(self, dists):
        '''
        Returns model predictions for binary 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 bool (num_test_samples) - binary predictions 
           for every test sample
        '''
        # print("dists:",dists)
        num_test = dists.shape[0]
        pred = np.zeros(num_test, np.bool)



        for i in range(num_test):
            # print("dists[i]:",dists[i])
            # TODO: Implement choosing best class based on k
            # nearest training samples

            # self.train_X has the images  (16, 3072)
            # self.train_y has the the labels (121,)
            # dists array has array list of the l1 distances of shape: (16, 121)
            # we choose dist[0] which is first test images distanceses for all other train data points
            # now we will check the smallest n numbers
            # put them in a list.
            # find labels through these indexes in train labels
            # and see if total number of true is bigger than total number of false



            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.zeros(self.k)

            labels_of_k_smallest_values = np.take(self.train_y, filter_indices, axis)
            # indexes_of_k_smallest_values = np.argpartition(dists[i], self.k)
            # print("indexes_of_k_smallest_values for test image[", i, "]:", indexes_of_k_smallest_values)
            
            

            # print("labels_of_k_smallest_values:"  ,labels_of_k_smallest_values)
            pred[i] = np.bincount(labels_of_k_smallest_values).argmax()



            # print("KNN prediction for test image[",i,"] is:",pred[i])
        self.prediction=  pred

        return pred

        Returns model predictions for binary 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 bool (num_test_samples) - binary predictions 
           for every test sample
        
Returns model predictions for binary 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 bool (num_test_samples) - binary predictions
for every test sample
Returns model predictions for binary 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 bool ( num_test_samples ) - binary predictions for every test sample
def predict_labels_binary(self, dists):
        num_test = dists.shape[0]
        pred = np.zeros(num_test, np.bool)
        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.zeros(self.k)
            labels_of_k_smallest_values = np.take(self.train_y, filter_indices, axis)
            pred[i] = np.bincount(labels_of_k_smallest_values).argmax()
        self.prediction=  pred
        return pred
def predict_labels_binary ( self , dists ) : num_test = dists . shape [ 0 ] pred = np . zeros ( num_test , np . bool ) 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 . zeros ( self . k ) labels_of_k_smallest_values = np . take ( self . train_y , filter_indices , axis ) pred [ i ] = np . bincount ( labels_of_k_smallest_values ) . argmax ( ) self . prediction = pred return pred
Returns model predictions for binary 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 binary 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 binary 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 bool (num_test_samples) - binary predictions 
           for every test sample
        ''' # print("dists:",dists) # print("dists[i]:",dists[i]) # TODO: Implement choosing best class based on k # nearest training samples # self.train_X has the images  (16, 3072) # self.train_y has the the labels (121,) # dists array has array list of the l1 distances of shape: (16, 121) # we choose dist[0] which is first test images distanceses for all other train data points # now we will check the smallest n numbers # put them in a list. # find labels through these indexes in train labels # and see if total number of true is bigger than total number of false # indexes_of_k_smallest_values = np.argpartition(dists[i], self.k) # print("indexes_of_k_smallest_values for test image[", i, "]:", indexes_of_k_smallest_values) # print("labels_of_k_smallest_values:"  ,labels_of_k_smallest_values) # print("KNN prediction for test image[",i,"] is:",pred[i])
self
dists