我需要将我的二进制编码特征矩阵转换成由特征交互的所有可能组合组成的矩阵。我所说的实际上是所有组合(每组2个,每组3个,每组4个,每组所有,等等)。
任何人都知道sklearn.preprocessing是否有办法吗?还是其他图书馆?
将此数组输入某些函数或方法:
array([[0, 1, 1],
[1, 0, 0],
[1, 1, 1]])
并将其作为输出
array([[0, 0, 1, 0],
[0, 0, 0, 0],
[1, 1, 1, 1]])
新矩阵中的每一行代表[x1 * x2,x1 * x3,x2 * x3,x1 * x2 * x3]
您想要的就是powerset。因此,您想查找特征的幂集,然后乘以相应的二进制值,该值基本上是np.bitwise_and
。因此,这是您可以执行的操作:
np.bitwise_and
len(features)
减少np.logical_and.reduce
的列表sets
哪个会给你:
a = np.array([[0, 1, 1],
[1, 0, 0],
[1, 1, 1]])
from itertools import chain, combinations
features = a.T.tolist()
power_set = []
for comb in chain.from_iterable(combinations(features, r)
for r in range(2,len(features)+1)):
power_set.append(np.logical_and.reduce(comb).view('i1').tolist())