assignments/assignment1/knn.py
def compute_distances_no_loops(self, X):
        '''
        Computes L1 distance from every sample of X to every training sample
        Fully vectorizes the calculations using numpy

        Arguments:
        X, np array (num_test_samples, num_features) - samples to run
        
        Returns:
        dists, np array (num_test_samples, num_train_samples) - array
           with distances between each test and each train sample
        '''
        num_train = self.train_X.shape[0]
        num_test = X.shape[0]


        # print("num_train:",num_train)
        # print("num_test:",num_test)
        # print("X.shape:",X.shape)
        #
        # print("train_X.shape:",self.train_X.shape)

        '''
        num_train: 121
        num_test: 16
        X.shape: (16, 3072)
        train_X.shape: (121, 3072)
        '''

        # Using float32 to to save memory - the default is float64
        dists = np.zeros((num_test, num_train), np.float32)
        # TODO: Implement computing all distances with no loops!

        m1 = np.reshape(X, (X.shape[0], 1, X.shape[1]))

        dists = np.sum(np.abs(m1 - self.train_X), axis=2)
        return dists

        Computes L1 distance from every sample of X to every training sample
        Fully vectorizes the calculations using numpy

        Arguments:
        X, np array (num_test_samples, num_features) - samples to run
        
        Returns:
        dists, np array (num_test_samples, num_train_samples) - array
           with distances between each test and each train sample
        
Computes L1 distance from every sample of X to every training sample
Fully vectorizes the calculations using numpy

X, np array (num_test_samples, num_features) - samples to run

dists, np array (num_test_samples, num_train_samples) - array
with distances between each test and each train sample
Computes L1 distance from every sample of X to every training sample Fully vectorizes the calculations using numpy X np array ( num_test_samples num_features ) - samples to run dists np array ( num_test_samples num_train_samples ) - array with distances between each test and each train sample
def compute_distances_no_loops(self, X):
        num_train = self.train_X.shape[0]
        num_test = X.shape[0]
         print("train_X.shape:",self.train_X.shape)
         Using float32 to to save memory - the default is float64
        dists = np.zeros((num_test, num_train), np.float32)
         TODO: Implement computing all distances with no loops!
        m1 = np.reshape(X, (X.shape[0], 1, X.shape[1]))
        dists = np.sum(np.abs(m1 - self.train_X), axis=2)
        return dists
def compute_distances_no_loops ( self , X ) : num_train = self . train_X . shape [ 0 ] num_test = X . shape [ 0 ] '''
        num_train: 121
        num_test: 16
        X.shape: (16, 3072)
        train_X.shape: (121, 3072)
        ''' dists = np . zeros ( ( num_test , num_train ) , np . float32 ) m1 = np . reshape ( X , ( X . shape [ 0 ] , 1 , X . shape [ 1 ] ) ) dists = np . sum ( np . abs ( m1 - self . train_X ) , axis = 2 ) return dists
Computes L1 distance from every sample of X to every training sample
Fully vectorizes the calculations using numpy
Computes L1 distance from every sample of X to every training sample Fully vectorizes the calculations using numpy
'''
        Computes L1 distance from every sample of X to every training sample
        Fully vectorizes the calculations using numpy

        Arguments:
        X, np array (num_test_samples, num_features) - samples to run
        
        Returns:
        dists, np array (num_test_samples, num_train_samples) - array
           with distances between each test and each train sample
        ''' # print("num_train:",num_train) # print("num_test:",num_test) # print("X.shape:",X.shape) # # print("train_X.shape:",self.train_X.shape) '''
        num_train: 121
        num_test: 16
        X.shape: (16, 3072)
        train_X.shape: (121, 3072)
        ''' # Using float32 to to save memory - the default is float64 # TODO: Implement computing all distances with no loops!
self
X