Answered step by step
Verified Expert Solution
Question
1 Approved Answer
solve using python, please dont post existing chegg solutions, all are wrong, i have reported them. They've identified 15 image patches already with this pathology
solve using python, please dont post existing chegg solutions, all are wrong, i have reported them.
They've identified 15 image patches already with this pathology and are trying to identify more. To this end, they have a process that reads image patches, converts each one to an np.array representation, and presents them in batches to a special K-Nearest Neighbor (KNN) class. This class then computes the distances between the query image representations and the 15 reference Image representations, and maintains a record of the K most similar (smallest distance) query Image representations seen so far. Your task will be to implement the KNN class. To this end, all of the image patches will have already been converted into an np.ndarray representation of size [1, D) and batched into matrices of size (B, D). Keep in mind though that since the incoming data is streaming the size of B is not fixed! Task specifications: 1. You'll be given an np.ndarray reference matrix of size [15, D] whose rows represent the 15 patches which have already been identified as containing the rare pathology. 2. Your KNN class should be constructed using the reference matrix, the number of similar image patches we want to keep K, and a distance function that takes a query matrix (shape=[B, D]) and reference matrix (shape=[15, D) and returns a matrix representing the distances between all pairs of shape (B, 15). 3. You'll need to implement the KNN class with an observe function that takes a single argument, an np.ndarray of size [B, D], who's rows represent individual image patch representations. Remember that B might vary each time observe is called. 4. The observe function should compute the distance between each query matrix row (each image patch) to each of the reference matrix rows using the distance function described in Step 2. The minimum distance between a query row and any reference row is the distance we care about. 5. Your KNN class should maintain a collection of the K query arrays with the smallest distances to any reference point. This collection must be returned as a RxD numpy array from the nearest_k property method. The collection must be sorted from smallest distance to largest distance (to any reference point). 6. The test cases will provide the name of the distance function to use, either 'L2" (https://en.wikipedia.org/wiki/Euclidean_distance) or 'cityblock' (https://en.wikipedia.org/wiki/Taxicab geometry) and you should construct your KNN class with the appropriate distance function. our code here. Read input from STDIN. Print output to STDOUT from typing import Iterable, Callable import numpy as np import sys # Computes the distances between an NxD matrix and an MxD matrix and returns an NxM matrix of distances. distanceFn = Callable((np.ndarray, np.ndarray], np.ndarray] class KNN: # Fill in your KNN implementation here. @property def nearest_k(self) -> np.ndarray: """Returns a numpy array of size (K x D) where K is the number of nearest neighbors we want to keep and D is the dimensionality. The queries must be sorted from smallest distance to largest distance (to any reference point). pass def get_nearest_neighbors references: np.ndarray, 7 Shape=[N, D] distance_function_name: str, k: int, queries: Iterable[np.ndar ray] > -> np.ndarray: assert distance_function_name in ('L2', 'cityblock') # Create the distance function which takes the query matrix (B, D) and the reference # matrix (N, D) and produces a matrix of distances between all pairs of size (B, N). # #my_distance fn = ... assert distance_function_name in ('L2', 'cityblock') # Create the distance function which takes the query matrix (B, D) and the reference # matrix (N, D) and produces a matrix of distances between all pairs of size (B, N). # #my_distance_fn = ... # # Your solution should look something like this: # knn = KNN (references, distanceFn=my_distance_fn, K) # for query in queries: # # The shape of query = [B, D). knn.observe (query) # return knn. nearest_k # Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started