Henryk Borzymowski
Henryk Borzymowski

Reputation: 1078

reticulate does not work with R-Data frame and fit() function from Python (TypeError: 'float' object cannot be interpreted as an integer)

I try to take a R Data Frame and use it with the package 'reticulate'. I couldn't find an answer in the Internet. sorry if this is a basic question.

# Sample Data
n <- 5000
n_outlier <- .05 * n

set.seed(11212)
inlier <- mvtnorm::rmvnorm(n, mean = c(0,0))
outlier <- mvtnorm::rmvnorm(n_outlier, mean = c(20, 20))
testdata <- rbind(inlier, outlier)
smp_size <- floor(0.5 * nrow(testdata))
train_ind <- sample(seq_len(nrow(testdata)), size = smp_size)
train_lof <-as.data.frame(testdata[train_ind, ])
test_lof <- as.data.frame(testdata[-train_ind, ])

sklearn.neighbors <- import("sklearn.neighbors")

lof1 = sklearn.neighbors$LocalOutlierFactor(n_neighbors=15)
lof1$fit(train_lof)

Gives the following error:

Error in py_call_impl(callable, dots$args, dots$keywords) : TypeError: 'float' object cannot be interpreted as an integer

Upvotes: 2

Views: 1713

Answers (1)

Mikael Poul Johannesson
Mikael Poul Johannesson

Reputation: 1349

You have to be explicit with your types (e.g., integers vs floats -- or lists vs vectors) when working with reticulate. The function expects an integer so you have to use as.integer():

lof1 = sklearn.neighbors$LocalOutlierFactor(n_neighbors=as.integer(15))

Upvotes: 6

Related Questions