Answered step by step
Verified Expert Solution
Question
1 Approved Answer
For this question, only python and numpy package are allowed to use Here is a sample output: def find_closest_example (data, test_example): (5 pts) Using the
For this question, only python and numpy package are allowed to use
Here is a sample output:
def find_closest_example (data, test_example): (5 pts) Using the euclidean distance, this function finds the position of the closest example in the "data" parameter to "test_example". So, if the closest example is the third one inside data, then it returns 2. If it is the fifth example, it returns the number 4...etc Parameters: data: 2-D numpy array continuous data in N x M dimensions test_example: 1-D array with with M values for a test example Returns: The "index" of the closest example (one integer). return None In [1]: import numpy as np In [2]: import homework2solution as hw In [3]: #Testing the find_closest_example function In [4]: #We'll place a datapoint in each quadrant (in 2D plane example) In [5]: tdata = np.array([ [-1,1], [1,1], [1, -1], [-1, -1] 1) In [6]: hw.find_closest_example(tdata, np.array([-6,9]) ) Out[6]: 0 In [7]: hw.find_closest_example(tdata, np.array( [4,9]) ) Out[7]: 1 In [8]: hw.find_closest_example(tdata, np.array( [-3,-4]) ) Out[8]: 3 In [9]: hw.find_closest_example(tdata, np.array([3,-4]) ) Out[9]: 2 In [10]: #Note that O means the first example, or the example in the first row in the training data In [11]: #Now testing three dimensional problems In [12]: tdata = np.array([ [1, -5, 1], [3, 5, 0], [-5, -6, -7] 1) In [13]: hw.find_closest_example(tdata, np.array([-2, -4,-8]) ) Out[13]: 2
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