Compulsory exercise 2
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
Publisert 19. apr. 2022 15:16
- Sist endret 19. apr. 2022 15:16