Lakshmi
Lakshmi

Reputation: 1

Error: NA/NaN/Inf in foreign function call (arg 6) in knn function written in R. Can somebody please correct me?

The knn function I used is showing the following error. Can somebody please help me out?

Error in knn(train = auction_data_train, test = auction_data_test, cl <- auction_data_train_labels,  : 

  NA/NaN/Inf in foreign function call (arg 6)

In addition: Warning messages:
1: In knn(train = auction_data_train, test = auction_data_test, cl <- auction_data_train_labels,  :
  NAs introduced by coercion
2: In knn(train = auction_data_train, test = auction_data_test, cl <- auction_data_train_labels,  :
  NAs introduced by coercion

I have shown the structure of my data and the code used below.

> str(auction_data)
'data.frame':   69 obs. of  11 variables:
 $ Qty              : int  66 106 35 9 20 17 6 3 4 2 ...
 $ Unit.Price       : Factor w/ 15 levels "0-500","1000-1500",..: 12 12 13 15 15 3 9 9 8 14 ...
 $ New.Invoice.Date : int  1 1 1 1 1 1 1 1 1 1 ...
 $ New.Seller       : int  1 1 1 1 1 1 1 1 1 1 ...
 $ New.Seller.Branch: int  1 1 1 1 1 1 1 1 1 1 ...
 $ New.Location     : int  1 1 1 1 1 1 1 1 1 1 ...
 $ New.Buyer        : int  2 2 2 2 2 2 2 2 2 2 ...
 $ New.Make         : int  4 4 4 4 4 4 4 4 4 4 ...
 $ New.Model        : int  26 25 27 62 28 29 31 30 33 32 ...
 $ New.Category     : int  1 1 1 1 1 1 1 1 1 1 ...
 $ New.Sub.Category : int  2 2 2 2 2 2 2 2 2 2 ...
> auction_data_train<-auction_data[1:59, ]
> auction_data_test<-auction_data[60:69, ]
> auction_data_train_labels<-auction_data[1:59,2]
> auction_data_test_labels<-auction_data[60:69,2]
> library(class)
> auction_data_test_pred<-knn(train=auction_data_train, test=auction_data_test,cl<-auction_data_train_labels, k=8)

Upvotes: 0

Views: 1842

Answers (1)

Neel Kamal
Neel Kamal

Reputation: 1076

Exclude labels from training data and testing data.

auction_data_train<-auction_data[1:59, -2]
auction_data_test<-auction_data[60:69, -2] 

Also, inside knn function call use cl = auction_data_train_labels.

Note: Using the <- operator inside a function call will create a new (or overwrite an existing) object.

Upvotes: 2

Related Questions