assignments/assignment1/knn.py
def compute_distances_two_loops(self, X):
        '''
        Computes L1 distance from every sample of X to every training sample
        Uses simplest implementation with 2 Python loops

        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_test", num_test)
        # print("num_train", num_train)
        dists = np.zeros((num_test, num_train), np.float32)
        for i_test in range(num_test):
            for i_train in range(num_train):
                # TODO: Fill dists[i_test][i_train]
                dists[i_test,i_train]=np.sum(np.abs([ X[i_test]- self.train_X[i_train]]))


        # print("dists", dists)


        return dists

        Computes L1 distance from every sample of X to every training sample
        Uses simplest implementation with 2 Python loops

        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
Uses simplest implementation with 2 Python loops

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 Uses simplest implementation with 2 Python loops 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_two_loops(self, X):
        num_train = self.train_X.shape[0]
        num_test = X.shape[0]
        dists = np.zeros((num_test, num_train), np.float32)
        for i_test in range(num_test):
            for i_train in range(num_train):
                dists[i_test,i_train]=np.sum(np.abs([ X[i_test]- self.train_X[i_train]]))
        return dists
def compute_distances_two_loops ( self , X ) : num_train = self . train_X . shape [ 0 ] num_test = X . shape [ 0 ] dists = np . zeros ( ( num_test , num_train ) , np . float32 ) for i_test in range ( num_test ) : for i_train in range ( num_train ) : dists [ i_test , i_train ] = np . sum ( np . abs ( [ X [ i_test ] - self . train_X [ i_train ] ] ) ) return dists
Computes L1 distance from every sample of X to every training sample
Uses simplest implementation with 2 Python loops
Computes L1 distance from every sample of X to every training sample Uses simplest implementation with 2 Python loops
'''
        Computes L1 distance from every sample of X to every training sample
        Uses simplest implementation with 2 Python loops

        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_test", num_test) # print("num_train", num_train) # TODO: Fill dists[i_test][i_train] # print("dists", dists)
self
X