2

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

Henryk Borzymowski
  • 988
  • 1
  • 10
  • 22

1 Answers1

6

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))
  • Do you maybe know how to call a private function then? I mean from the lof1 -> lof1$_decision_function(test_lof) ? – Henryk Borzymowski May 10 '18 at 16:24
  • 2
    I've answered this question in https://stackoverflow.com/questions/50289232/cant-find-private-function-for-sklearn-localoutlierfactor-in-reticulate/ – Yuan Tang May 12 '18 at 14:15