我正在尝试编写一个将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 …Run Code Online (Sandbox Code Playgroud)