Import Data and handle it
m.data() loads tabular data into the BF object, either a data frame that already exists in your working session or a CSV file. The table is stored in m.df, where it can be manipulated before the columns used by the model are sent to it. Data that cannot be stored in a table (matrices, networks, arrays of different lengths) are passed as a dictionary.
Pass a data frame from your session
Load, clean, filter or merge your data with the tools of your own language, then hand the data frame to BF; nothing needs to be written to disk. BF works on a copy, so later steps (m.scale(), m.OHE(), β¦) never modify your original table.
import pandas as pd
from BayesForge import bf
m = bf(platform="cpu")
# Any pandas data frame from your session
df = pd.read_csv(m.load.howell1(only_path=True), sep=";")
df = df[df.age > 18] # e.g. keep adults only
m.data(df) # a pandas DataFrame
m.df.head()library(BayesForge)
m <- importBF(platform = "cpu")
# Any R data.frame from your session (base R, dplyr, data.table, ...)
df <- read.csv(m$load$howell1(only_path = TRUE), sep = ";")
df <- subset(df, age > 18) # e.g. keep adults only
m$data(df) # an R data.frame
head(m$df)using PyBayesForge, PythonCall, DataFrames, CSV
m = importBF(platform = "cpu")
# Any Julia DataFrame (or other Tables.jl table) from your session
path = pyconvert(String, m.load.howell1(only_path = true))
df = CSV.read(path, DataFrame; delim = ';')
df = filter(:age => >(18), df) # e.g. keep adults only
m.data(df) # a Julia DataFrame
m.df.head()Once the data are in BF, select the columns the model uses with m.data_to_model(["weight", "height"]) (list("weight", "height") in R): they become the arrays in m.data_on_model, passed to the model by argument name. See Working with R and Julia tools for getting the posterior draws back as native R or Julia objects.
Import tabular data from a CSV file
from BayesForge import bf
m = bf(platform="cpu")
data_path = m.load.howell1(only_path=True) # path to a bundled example csv
m.data(data_path, sep=';') # extra arguments go to pandas.read_csv
m.df.head()library(BayesForge)
m <- importBF(platform = "cpu")
data_path <- m$load$howell1(only_path = TRUE) # path to a bundled example csv
m$data(data_path, sep = ';')
head(m$df)using PyBayesForge
m = importBF(platform = "cpu")
data_path = m.load.howell1(only_path = true) # path to a bundled example csv
m.data(data_path, sep = ';')
m.df.head()Import non tabular data
Data that do not fit in a table are passed directly as a dictionary of arrays, whose names match the arguments of the model function.
import jax.numpy as jnp
m.data_on_model = dict(
y = jnp.array([1.2, 0.7, 2.3]), # a vector
A = jnp.eye(3), # a matrix (e.g. a network)
)m$data_on_model <- list(
y = jnp$array(c(1.2, 0.7, 2.3)), # a vector
A = jnp$eye(3L) # a matrix (e.g. a network)
)m.data_on_model = pydict(
y = jnp.array([1.2, 0.7, 2.3]), # a vector
A = jnp.eye(3), # a matrix (e.g. a network)
)Handle data
To encode categories, standardize, cast types and send columns to the model (index, OHE, scale, z_score, to_float, to_int, data_to_model), see Handle data.