hw0_corrected.pdf 1. Write a function called neighborClassify that takes in a 1D numpy array of numbers (the heights of unknown animals) and a 2D numpy array containing the heights of known animals....

1 answer below »
cannot use sklearn or any other machine learning libraries. Just need to write functions. Its very basic


hw0_corrected.pdf 1. Write a function called neighborClassify that takes in a 1D numpy array of numbers (the heights of unknown animals) and a 2D numpy array containing the heights of known animals. The function will return a list of 0s and 1s a 0 for each non-giraffe input and a 1 for each giraffe input using nearest neighbors classification (see below). Specifically, the function call must look like this: neighborClassify(featureArray, trainArray) featureArray will be a numpy array of shape (n) (where there are arbitrary number n animals to classify) and trainArray is a numpy array of shape (n,2 ) where each row contains first the height of a training animal and then its corresponding class (0 for non-giraffe, 1 for giraffe). Specifically, if featureArray=np.array([6,3,9]) and trainArray=np.array([[0.5,0], [1.5,0], [2.5,0], [4.5,1], [5,1], [7.5, 0], [8,1], [9.2,1]]), the function will return the list [1, 0, 1] . Classification is done by the nearest neighbors approach. Each test input is given the label of the nearest input in the training set. Below is a graphical example of this approach. 2. Write a function called findAccuracy that takes in a list of approximated class labels output by the classifier (neighborClassify) and a list of true labels provided in the training set, and calculates the overall accuracy and the false positive rate of the classifier as a list of length 2 [accuracy,FalsePosRate], with each value between 0 and 1. Specifically, the function call must look like this: findAccuracy(classifierOutput, trueLabels) If classifierOutput=[1,1,1,0,1,0,1,1] and trueLabels=[1,1,0,0,0,0,1,1], the function will return the number list [0.75,0.5] (2 out of 8 values (4 actually-negative values) were incorrect). 3. Write a function called fillBlanks that takes in a featureArray (n) numpy array and returns a (n) numpy array where all missing values are filled in with the number 1 . Specifically, the function call must look like this: featArrayCorrected=fillBlanks(featArray) If featArray=np.array([-2,5,-6,3,1,8,10,-1]) the function will return np.array([1,5,1,3,1,8,10,1]) into featArrayCorrected
Answered Same DaySep 13, 2021

Answer To: hw0_corrected.pdf 1. Write a function called neighborClassify that takes in a 1D numpy array of...

Aditya answered on Sep 14 2021
145 Votes
import numpy as np
def fillBlanks(featArray):

for i in range(len(featArray)):
if
featArray[i]<0:
featArray[i]=1
return featArray
a=np.array([-2,5,-6,3,1,8,10,-1])

a=fillBlanks(a)
print(a
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here