无需SKLearn即可快速创建交互术语

2024-06-08 07:56:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以下代码在数据中创建交互术语:

def Interaction(x):
  for k in range(0,x.shape[1]-1):
    for j in range(k+1,x.shape[1]-1):
      new = x[:,k] * x[:,j]
      x = np.hstack((x,new[:,None]))
  return x

我的问题是,与SKLearn的多项式特征相比,它的速度非常慢。我怎样才能加快速度?我不能使用SKLearn,因为我想进行一些定制。例如,我想做一个交互变量X1*X2,但也要做X1*(1-X2),等等


Tags: 数据代码innewfordefnprange
1条回答
网友
1楼 · 发布于 2024-06-08 07:56:47

我们应该成对地将每行的每个元素相乘,我们可以这样做np.einsum('ij,ik->ijk, x, x)。这是冗余的2倍,但仍然比PolynomialFeatures快2倍

import numpy as np

def interaction(x):
    """
    >>> a = np.arange(9).reshape(3, 3)
    >>> b = np.arange(6).reshape(3, 2)
    >>> a
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    >>> interaction(a)
    array([[ 0,  1,  2,  0,  0,  2],
           [ 3,  4,  5, 12, 15, 20],
           [ 6,  7,  8, 42, 48, 56]])
    >>> b
    array([[0, 1],
           [2, 3],
           [4, 5]])
    >>> interaction(b)
    array([[ 0,  1,  0],
           [ 2,  3,  6],
           [ 4,  5, 20]])
    """
    b = np.einsum('ij,ik->ijk', x, x)
    m, n = x.shape
    axis1, axis2 = np.triu_indices(n, 1)
    axis1 = np.tile(axis1, m)
    axis2 = np.tile(axis2, m)
    axis0 = np.arange(m).repeat(n * (n - 1) // 2)
    return np.c_[x, b[axis0, axis1, axis2].reshape(m, -1)]

性能比较:

c = np.arange(30).reshape(6, 5)
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(2, interaction_only=True)
skl = poly.fit_transform
print(np.allclose(interaction(c), skl(c)[:, 1:]))
# True


In [1]: %timeit interaction(c)
118 µs ± 172 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [2]: %timeit skl(c)
243 µs ± 4.69 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

相关问题 更多 >