In [11]:
from patsy import dmatrix, dmatrices
In [12]:
np.random.seed(0)
x1 = np.random.rand(5) + 10
x2 = np.random.rand(5) * 10
x1, x2
Out[12]:
In [13]:
dmatrix("x1")
Out[13]:
In [14]:
dmatrix("x1 - 1")
Out[14]:
In [15]:
dmatrix("x1 + 0")
Out[15]:
In [16]:
dmatrix("x1 + x2")
Out[16]:
In [17]:
dmatrix("x1 + x2 - 1")
Out[17]:
In [18]:
df = pd.DataFrame(np.array([x1, x2]).T, columns=["x1", "x2"])
df
Out[18]:
In [19]:
dmatrix("x1 + x2 - 1", data=df)
Out[19]:
In [20]:
dmatrix("x1 + np.log(np.abs(x2))", data=df)
Out[20]:
In [21]:
def doubleit(x):
return 2 * x
dmatrix("doubleit(x1)", data=df)
Out[21]:
In [22]:
dmatrix("center(x1) + standardize(x2)", data=df)
Out[22]:
In [23]:
dmatrix("x1 + x2", data=df)
Out[23]:
In [24]:
dmatrix("I(x1 + x2)", data=df)
Out[24]:
In [25]:
dmatrix("x1 + I(x1**2) + I(x1**3) + I(x1**4)", data=df)
Out[25]:
In [26]:
df["a1"] = pd.Series(["a1", "a1", "a2", "a2", "a3", "a5"])
df["a2"] = pd.Series([1, 4, 5, 6, 8, 9])
df
Out[26]:
In [27]:
dmatrix("a1", data=df)
Out[27]:
In [28]:
dmatrix("a2", data=df)
Out[28]:
In [29]:
dmatrix("C(a2)", data=df)
Out[29]: