Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

knn compute_distances_one_loop

def compute_distances_one_loop(self, X):
    """
    Compute the distance between each test point in X and each training point
    in self.X_train using a single loop over the test data.

    Input / Output: Same as compute_distances_two_loops
    """
    num_test = X.shape[0]
    num_train = self.X_train.shape[0]
    dists = np.zeros((num_test, num_train))
    for i in range(num_test):
      # Note axis=1 computes norm along rows
      dists[i] = np.linalg.norm(X[i]-self.X_train, axis=1)

    return dists

Difference was: 0.000000
Good! The distance matrices are the same
Comment

knn compute_distances_two_loop

def compute_distances_two_loops(self, X):
    """
    Compute the distance between each test point in X and each training point
    in self.X_train using a nested loop over both the training data and the
    test data.

    Inputs:
    - X: A numpy array of shape (num_test, D) containing test data.

    Returns:
    - dists: A numpy array of shape (num_test, num_train) where dists[i, j]
      is the Euclidean distance between the ith test point and the jth training
      point.
    """
    num_test = X.shape[0]
    num_train = self.X_train.shape[0]
    dists = np.zeros((num_test, num_train))
    for i in range(num_test):
      for j in range(num_train):
        dists[i, j] = np.linalg.norm(X[i]-self.X_train[j])

    return dists

(500, 5000)
Comment

knn compute_distances_no_loop

def compute_distances_no_loops(self, X):
    """
    Compute the distance between each test point in X and each training point
    in self.X_train using no explicit loops.

    Input / Output: Same as compute_distances_two_loops
    """
    num_test = X.shape[0]
    num_train = self.X_train.shape[0]

    # Expand ||x - y||**2 = ||x||**2 - 2 x.T ⋅y + ||y||**2,
    # where ||x||**2 = sum(x**2) (element-wise on matrix rows)
    # The final result is a (num_test, num_train) matrix
    # so the x**2 and y**2 intermediates must be reshaped appropriately
    x2 = np.sum(X**2, axis=1).reshape((num_test, 1))
    y2 = np.sum(self.X_train**2, axis=1).reshape((1, num_train))
    xy = -2*np.matmul(X, self.X_train.T)
    dists = np.sqrt(x2 + xy + y2)

    return dists

Difference was: 0.000000
Good! The distance matrices are the same
Comment

PREVIOUS NEXT
Code Example
Python :: unauthorized vue django rest framework 
Python :: parameter name in string 
Python :: from android.runnable in python 
Python :: BeautifulSoup : Fetched all the links on a webpage how to navigate through them without selenium 
Python :: city of stars how many words in a song python code 
Python :: Data Extraction in Python 
Python :: print banner in python 
Python :: create multiple marks python for python 
Python :: deque popleft in python 
Python :: installing blacksheep 
Python :: load SQLite db into memory 
Python :: django insert data into database without form 
Python :: Hide div element using python in Flask 
Python :: singke line expresions python 
Python :: extracting code blocks from Markdown 
Python :: replace dataframe column element if element is within a specific list 
Python :: replace string in dictionary python 
Python :: tkinter disabled but selectable 
Python :: python sort dict by sub value 
Python :: How to Load Any HuggingFace Model in spaCy 
Python :: python sleep for 1 minute 
Python :: read past tense 
Python :: matplotlib bring plot to front in plots with twin axis 
Python :: seleniu get element value and store it in a variable - selenium remember user 
Python :: python making player inventory 
Python :: Obtener el valor ASCII de un carácter en Python 
Python :: populate initial data for django simple history 
Python :: add suffix to multiple file names python 
Python :: set application taskbar icon 
Python :: input character in python like getchar in c 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =