Created
June 27, 2023 17:23
-
-
Save nanxstats/06232707afb6f2d8e73ce3c3e4212dae to your computer and use it in GitHub Desktop.
Calling SDV in an R6 class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(reticulate) | |
# Ignore this ---- | |
use_python("/opt/homebrew/bin/python3.10") | |
# reticulate works ---- | |
sdv <- import("sdv") | |
# Generate some example data | |
get_available_demos <- sdv$datasets$demo$get_available_demos | |
x <- get_available_demos(modality = "single_table") | |
# Detect metadata | |
SingleTableMetadata <- sdv$metadata$SingleTableMetadata | |
metadata <- SingleTableMetadata() | |
metadata$detect_from_dataframe(data = x) | |
# Create synthesizer instance | |
GaussianCopulaSynthesizer <- sdv$single_table$copulas$GaussianCopulaSynthesizer | |
synthesizer <- GaussianCopulaSynthesizer(metadata) | |
# Fit model and inference | |
synthesizer$fit(x) | |
synthesizer$sample(num_rows = 10L) | |
# R6 works ---- | |
library(R6) | |
GaussianCopulaSynthesizer <- R6Class( | |
"GaussianCopulaSynthesizer", | |
list( | |
metadata = NULL, | |
synthesizer = NULL, | |
initialize = function(metadata) { | |
self$metadata <- metadata | |
self$synthesizer <- sdv$single_table$copulas$GaussianCopulaSynthesizer(metadata) | |
}, | |
fit = function(data) { | |
self$synthesizer$fit(data) | |
}, | |
sample = function(num_rows) { | |
self$synthesizer$sample(num_rows = as.integer(num_rows)) | |
} | |
) | |
) | |
gcs <- GaussianCopulaSynthesizer$new(metadata) | |
gcs$fit(x) | |
gcs$sample(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment