多项式展开的2d numpy.power

时间:2012-07-30 14:41:35

标签: python numpy

我正在尝试编写一个将2d-ndarray映射到2d-ndarray的函数。输入数组的行可以独立处理,输入行和输出行之间应该有一对一的对应关系。对于输入的每一行,应计算行的给定顺序的多项式展开(有关示例,请参见docstring)。目前的实施工作;但是它需要在“powerMatrix”中对行和行的重复进行显式循环。通过一次调用numpy.power可以获得相同的结果吗?顺便说一句:结果行中条目的顺序对我来说无关紧要。

import numpy
def polynomialFeatures(x, order):
    """ Generate polynomial features of given order for data x.

    For each row of ndarray x, the polynomial expansions are computed, i.e
    for row [x1, x2] and order 2, the following row of the result matrix is
    computed: [1, x1, x1**2, x2, x1*x2, x1**2*x2, x2**2, x1*x2**2, x1**2*x2**2]

    Parameters
    ----------
    x : array-like
        2-D array; for each of its rows, the polynomial features are created

    order : int
        The order of the polynomial features

    Returns
    -------
    out : ndarray
        2-D array of shape (x.shape[0], (order+1)**x.shape[1]) containing the 
        polynomial features computed for the rows of the array x

    Examples
    --------
    >>> polynomialFeatures([[1, 2, 3], [-1, -2, -3]], 2)
    array([[  1   3   9   2   6  18   4  12  36   1   3   9   2   6  18   4  12  
             36   1   3   9   2   6  18   4  12  36]
           [  1  -3   9  -2   6 -18   4 -12  36  -1   3  -9   2  -6  18  -4  12 
            -36   1  -3   9  -2   6 -18   4 -12  36]])
    """
    x = numpy.asarray(x)
    # TODO: Avoid duplication of rows
    powerMatrix = numpy.array([range(order+1)] * x.shape[1]).T
    # TODO: Avoid explicit loop, and use numpy's broadcasting
    F = []
    for i in range(x.shape[0]):
        X = numpy.power(x[i], powerMatrix).T
        F.append(numpy.multiply.reduce(cartesian(X), axis=1))

    return numpy.array(F)

print numpy.all(polynomialFeatures([[1, 2, 3], [-1, -2, -3]], 2) ==
                numpy.array([[1,   3,   9,   2,   6,  18,   4,  12,  36,   1,
                              3,   9,   2,   6,  18,   4,  12,  36,   1,   3, 
                              9,   2,   6,  18,   4,  12,  36],
                             [1,  -3,   9,  -2,   6, -18,   4, -12,  36,  -1,
                              3,  -9,   2,  -6,  18,  -4,  12, -36,   1,  -3,
                              9,  -2,   6, -18,   4, -12,  36]]))

谢谢, 扬

编辑:缺少的函数cartesian在此处定义:Using numpy to build an array of all combinations of two arrays

1 个答案:

答案 0 :(得分:3)

基本思想是将尺寸(在您的情况下,尺寸0,行数)与“不在路上”的计算无关地移动到更高的维度,然后自动通过它进行广播。

我不确定您的cartesian方法正在做什么,但这是一个使用np.indices生成X矩阵上的索引元组的解决方案:

import numpy as np
def polynomial_features(x, order):
    x = np.asarray(x).T[np.newaxis]
    n = x.shape[1]
    power_matrix = np.tile(np.arange(order + 1), (n, 1)).T[..., np.newaxis]
    X = np.power(x, power_matrix)
    I = np.indices((order + 1, ) * n).reshape((n, (order + 1) ** n)).T
    F = np.product(np.diagonal(X[I], 0, 1, 2), axis=2)
    return F.T