<p>我现在面临着一个类似的问题,我需要一种灵活的方法来创建特定的交互,并查看StackOverflow。我按照上面@user333700注释中的提示操作,并感谢他找到了<em>patsy</em>(<a href="http://patsy.readthedocs.io/en/latest/overview.html" rel="noreferrer">http://patsy.readthedocs.io/en/latest/overview.html</a>),在谷歌搜索之后,这个scikit学习集成<em>patsy learn</em>(<a href="https://github.com/amueller/patsylearn" rel="noreferrer">https://github.com/amueller/patsylearn</a>)。</p>
<p>所以通过@motam79的例子,这是可能的:</p>
<pre><code>import numpy as np
import pandas as pd
from patsylearn import PatsyModel, PatsyTransformer
x = np.array([[ 3, 20, 11],
[ 6, 2, 7],
[18, 2, 17],
[11, 12, 19],
[ 7, 20, 6]])
df = pd.DataFrame(x, columns=["a", "b", "c"])
x_t = PatsyTransformer("a:b + a:c + b:c", return_type="dataframe").fit_transform(df)
</code></pre>
<p>这将返回以下信息:</p>
<pre><code> a:b a:c b:c
0 60.0 33.0 220.0
1 12.0 42.0 14.0
2 36.0 306.0 34.0
3 132.0 209.0 228.0
4 140.0 42.0 120.0
</code></pre>
<p>我在这里回答了一个类似的问题,在这里我提供了另一个带有分类变量的示例:
<a href="https://stackoverflow.com/questions/23497512/how-can-an-interaction-design-matrix-be-created-from-categorical-variables">How can an interaction design matrix be created from categorical variables?</a></p>