Question

function predictKNN which computes the KNN prediction for given input xi, K and train data. A...

function predictKNN which computes the KNN prediction for given input xi, K and train data.

A correct implementation will behave similar to below example:

Input: xi: list, trainx: list of lists, trainy: list of list, k: integer

call: predictKNN(trainx[0],trainx,trainy)

output: 2: integer

def predictKNN(xi,trainx,trainy,k=3):
###
### YOUR CODE HERE
###

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Assume that you are given listx , listy, listtarget where listx has x values,  listy has corresponding y values and listtarget has the corresponding target values where the target is 0 or 1.

Assuming this, here is the python code:

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

def KNN(listx,listy,listtarget,testOrdPair,k):
df = pd.DataFrame(list(zip(listx,listy,listtarget)),columns=['x','y','target'])
scaler = StandardScaler()
scaler.fit(df.drop('target',axis=1))
scaled_features=scaler.transform(df.drop('target',axis=1))
x=df_feat=pd.DataFrame(scaled_features,columns=df.columns[:-1])
y=pd.DataFrame(df['target'])
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(x,y)
pred=knn.predict(testOrdPair)
return pred

Here. testOrdPair refers to a list of (x,y) for which you want a prediction, a list of targets corresponding to each ordered pairs is returned.

Hope this helps!

Add a comment
Know the answer?
Add Answer to:
function predictKNN which computes the KNN prediction for given input xi, K and train data. A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • python Write a function that takes as input a single integer parametern and computes the nth...

    python Write a function that takes as input a single integer parametern and computes the nth Fibonacci Sequence number The Fibonacci sequence first two terms are 1 and 1(so, if n - 2 your function should return 1). From there, the previous two values are summed to come up with the next result in the sequence 1.1,2,3,5,8,13, 21, 34, 55, etc.) For example, if the input is 7 then your function should return 13 26561.1615880 1 def Fibonacci(n): # your...

  • In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int...

    In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int -> Int Write a recursive function myProduct that multiplies all the numbers in a list. myProduct :: [Integer] -> Integer Using the technique of List Comprehension write a function that would remove even numbers from a list of lists. For Example: given input: [[1,2,3,4], [6,3,45,8], [4,9,23,8]] expected output: [[1,3], [3,45],[9,23]]. Show how the library function replicate :: Int -> a-> [a] that produces a...

  • Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all...

    Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all the prime numbers up to that value. Example: If input parameter is 10, output is "2, 3, 5, 7" If input parameter is 100, output is "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97" Write a "tester function" that: Given a specific subset of inputs, tests the...

  • Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all...

    Using PHP: Write a function that: Given a numerical parameter in input, computes and prints all the prime numbers up to that value. Example: If input parameter is 10, output is "2, 3, 5, 7" If input parameter is 100, output is "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97" Write a "tester function" that: Given a specific subset of inputs, tests the...

  • Question:Write a C program to input an integer k. Compute 10^k and store the result in...

    Question:Write a C program to input an integer k. Compute 10^k and store the result in a double variable. Display the result on the standard output using printf. Implementation ° The program is named lab4c . c. Use the given template lab4c .c (BELOW ) and fill in your code. ° Assume that the input integer k is small enough so that 10^k can be stored in a double variable named my_double. ° Display on the standard output the result...

  • Classification in Python: Classification In this assignment, you will practice using the kNN (k-Nearest Neighbors) algorithm...

    Classification in Python: Classification In this assignment, you will practice using the kNN (k-Nearest Neighbors) algorithm to solve a classification problem. The kNN is a simple and robust classifier, which is used in different applications. The goal is to train kNN algorithm to distinguish the species from one another. The dataset can be downloaded from UCI Machine Learning Repository: https://archive.ics.uci.edu/ml/machine-learning-databases/iris/ (Links to an external site.)Links to an external site.. Download `iris.data` file from the Data Folder. The Data Set description...

  • Write a function that accepts as input a scalar x and an integer n and computes f(x), where f(x) = 1 + x + ... + x^n. For example, if x = 2 and n = 3, the function should return 1 + 2 + 4 + 8 = 15. Hi...

    Write a function that accepts as input a scalar x and an integer n and computes f(x), where f(x) = 1 + x + ... + x^n. For example, if x = 2 and n = 3, the function should return 1 + 2 + 4 + 8 = 15. Hint: Use point-wise operations and the MATLAB sum function. Your Function 1 1 function yf(x) 3 end Code to call your function Your Function 1 1 function yf(x) 3 end...

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • The function Log-Floor(x) computes the fractional part of the base-two logarithm of x (or, in other...

    The function Log-Floor(x) computes the fractional part of the base-two logarithm of x (or, in other words, the largest k such that 2^k<=x). What is the asymptotic running time of the iterative implementation given below? Describe a recursive implementation of Log-Floor that has the same running time as the iterative version. Justify your solution. Provide pseudocode. Log-Floor(x): k = 0 while x > 1 x = x / 2 // Integer division k = k + 1 return k

  • Given a linked list of integers, write a method that computes and prints the pairwise sums...

    Given a linked list of integers, write a method that computes and prints the pairwise sums which is illustrated below with an example. If the linked list has values 5,3,2,9,3,15,22 from head to tail, the pairwise sums would be 8,5,11,12,18,37 which is the sum of two consecutive values from left to right. Notice the output will be of size one less than the original. You will output the pairwise sums exactly as above on a single line, comma separated. If...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT