hw0_co
ected.pdf
1. Write a function called neighborClassify that takes in a 1D numpy a
ay of numbers
(the heights of unknown animals) and a 2D numpy a
ay 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(featureA
ay, trainA
ay)
featureA
ay will be a numpy a
ay of shape (n) (where there are a
itrary number n
animals to classify) and trainA
ay is a numpy a
ay of shape (n,2 ) where each row
contains first the height of a training animal and then its co
esponding class (0 for non-giraffe,
1 for giraffe). Specifically, if featureA
ay=np.a
ay([6,3,9]) and
trainA
ay=np.a
ay([[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 inco
ect).
3. Write a function called fillBlanks that takes in a featureA
ay (n) numpy a
ay and
eturns a (n) numpy a
ay where all missing values are filled in with the number 1
.
Specifically, the function call must look like this:
featA
ayCo
ected=fillBlanks(featA
ay)
If featA
ay=np.a
ay([-2,5,-6,3,1,8,10,-1]) the function will return
np.a
ay([1,5,1,3,1,8,10,1]) into featA
ayCo
ected