Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of working R package PORT of Python package #1756

Open
thierrymoudiki opened this issue Mar 2, 2025 · 0 comments
Open

Example of working R package PORT of Python package #1756

thierrymoudiki opened this issue Mar 2, 2025 · 0 comments

Comments

@thierrymoudiki
Copy link

Been trying for years to port Python packages to R using reticulate (even using the new https://rstudio.github.io/reticulate/articles/package.html). More precisely, install like: install.packages. The success is quite random for packaging (only works in scripts and notebooks). Sometimes works on certain platforms and others won't. Looking for existing (small) R packages achieving this (or maybe I'm dumb) prowess. I guess it's 100% about environment management.

remotes::install_github("Techtonique/nnetsauce_r")
library(datasets)
# install.packages("reticulate") # Uncomment if reticulate is not installed.
library(reticulate)

# Use reticulate to access Python modules
sklearn <- import("sklearn")

X <- as.matrix(iris[, 1:4])
y <- as.integer(iris[, 5]) - 1L

n <- dim(X)[1]
p <- dim(X)[2]

set.seed(213)
train_index <- sample(x = 1:n, size = floor(0.8*n), replace = TRUE)
test_index <- -train_index

X_train <- as.matrix(iris[train_index, 1:4])
y_train <- as.integer(iris[train_index, 5]) - 1L
X_test <- as.matrix(iris[test_index, 1:4])
y_test <- as.integer(iris[test_index, 5]) - 1L

sklearn <- import("sklearn")

# ValueError: Sample weights must be 1D array or scalar
obj <- sklearn$tree$DecisionTreeClassifier()
obj2 <- nnetsauce::CustomClassifier(obj)
obj2$fit(X_train, y_train)
print(obj2$score(X_test, y_test))
print(obj2$predict_proba(X_test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant