InferPy
InferPy copied to clipboard
Pandas Data linkage
Given a data structure with labeled columns (i.e., a pandas dataframe). Study how to associate each column to an observed variable. A straightforward way would be creating a variable with each variable, and the association is done in the fit invokation:
@inf.probmodel
def model():
...
with inf.datamodel():
x1 = inf.Normal(.... name="x1")
x2 = inf.Normal(.... name="x2")
df = .... # pandas dataframe with columns "x1" and "x2"
m = model()
m.fit(data = df.to_dict())
The previous conversion could be done implicitly if the input is a pandas data frame
m.fit(data = df)
In some cases, we might be interested in associating multiples columns to a multidimensional RV. For example, in a linear regression, where all the predictive attributes will be associated to a single variable in the model:
@inf.probmodel
def model():
...
with inf.datamodel():
x = inf.Normal(.... name="x")
y = inf.Normal(.... name="y")
df = .... # pandas dataframe with columns "x1", "x2", "y"
m = model()
y_train = df["y"].values
x_train = df[["x1,"x2"]].values
m.fit(data ={"x":x_train, "y":y_train} )