Compulsory exercise 2_澳门葡京手机版app下载

geirs
2022-04-19T13:16:23.987Z
type:structured-message
www.uio.no

Some of you have got some problems with question e.

First: The knn_mi function only performs the calculation for ONE prediction point at a time. Therefore, there is a need for a for loop in this case.

Second: Predicting the i-th test observation can be performed by the command

pred.knn[i] = knn_mi(Xptr[Xtr$X==1,1:2],Xptr[Xtr$X==0,1:2],nn=3,as.matrix(Xpte[i,1:2]))>0.5

Note in particular the use of as.matrix. Without this, the call can crash.

Third: The for loop can be somewhat time-consuming. This will in particular be problematic when performing predictions based on Xnew. A possibility here is to use only the 5000, say, rows of Xnew.

Geir