In [1]:
import pandas as pd
import numpy as np
from patsy import dmatrix, dmatrices
In [2]:
df = pd.DataFrame({'x': np.arange(1, 5), 'y': [2*i for i in np.arange(1, 5)]})
In [3]:
df
Out[3]:
In [4]:
df['z'] = df.x + df.y
In [5]:
df
Out[5]:
In [16]:
# Basic design matrix
# Pandas compatibility
# Interaction term is included using x*y
dmat = dmatrix('x + y + x*y', df)
print(dmat)
In [17]:
dmat_df = pd.DataFrame(dmat)
In [18]:
dmat_df.columns = dmat.design_info.term_names
In [19]:
dmat_df
Out[19]:
In [20]:
# Design matrices
y, x = dmatrices('z ~ x + x:y', df)
In [23]:
y
Out[23]:
In [22]:
# Easy to convert to pandas DataFrame
pd.DataFrame(y)
Out[22]:
In [24]:
x
Out[24]:
In [28]:
# Easy to convert to pandas DataFrame
pd.DataFrame(x, columns=x.design_info.term_names)
Out[28]:
In [ ]: