mercredi 24 mai 2017

KNN Error for Flights Dataset

I am trying to learn how to do KNN in R, and am practicing on the flights dataset from the package nycflights13. I get an error running the below code saying

'train' and 'class' have different lengths

My code:

library(nycflights13)
library(class)


deparr <- na.omit(flights[c(4, 7, 16)])

classframe <- deparr[3]

flights %>% ggvis(~dep_time, ~arr_time, fill = ~distance) %>% layer_points()

set.seed(1234)

ind <- sample(2, nrow(deparr), replace=TRUE, prob=c(0.67, 0.33))

flights.training <- deparr[ind==1, 1:2]
flights.test <- deparr[ind==2, 1:2]
flights.trainlabels <- deparr[ind==1, 3]
flights.testlabels <- deparr[ind==2, 3]

predictions <- knn(train = flights.training, test = flights.test, cl = flights.trainlabels[,1], k = 3)




Aucun commentaire:

Enregistrer un commentaire