question.
7. Complete the function knn-predict that takes in three parameters: data, x-new, and k. • data is a tuple of two items. The first item is a list of x coordinates which are numbers, and the second item is a list of y coordinates, which are also numbers. Assume that data [0] is sorted in ascending order with no duplicated values. • x-new is a number between min (data[0]) and max (data[0]) inclusive. .k is a positive integer. • The function returns the following predicted value y-new at the point x-new: Σ yi, iEN (new) whereyi = data[1]. The set Nx (Inew) is the set of indices of the k items in the x coordi- nates data [0] that are closest to x-new. Thus, y-new is the average of the y values of the k nearest neighbors to x_new. The acronym knn stands for k-nearest neighbors. For example, suppose data = ([0, 5, 10, 15], [1, 7, -5, 11]). Let's see what Nx (Inew) and Ynew are for a few different choices for x-new and k. Ynew = • If xnew = 2 and k = 2 then 1+7 N2(2) = [0, 1] and Ynew = = 4. N . If x.new = 2 and k = 3 then 1+ 7 - 5 N3(2) = [0, 1, 2] and Ynew = 3 1. • If x-new = 8 and k = 2 then = N2(8) = [1,2] and Ynew = = 7 - 5 2 = 1. • If x-new = 8 and k = 3 then N3(8) = 1,2,3] and Ynew = 7- 5 + 11 3 4.33333
Using Python to solve the Using Python to solve the question.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am