我试图写使用numpy的Python中的功能,将作为输入的向量x = array([x_1, x_2, ..., x_n])
和的正整数m
,并返回尺寸的阵列n
由m
形式(比如说的n==3
,m==4
):
array([[ x_1, x_1^2, x_1^3, x_1^4],
[ x_2, x_2^2, x_2^3, x_2^4],
[ x_3, x_3^2, x_3^3, x_3^4]])
到目前为止,我有
import numpy as np
def f(x, m):
return x ** np.arange(1, m+1)
它适用于n==1
,但引发了一个例外n > 1
。我试图避免使用循环。在Numpy中有做这个的好方法吗?
谢谢!
您可以
np.vander
x = np.array([2.3,1.3,4.4])
m = 4
np.vander(x,m+1,True)[:,1:]
# array([[ 2.3 , 5.29 , 12.167 , 27.9841],
# [ 1.3 , 1.69 , 2.197 , 2.8561],
# [ 4.4 , 19.36 , 85.184 , 374.8096]])
要么
np.power.outer
np.power.outer(x,np.arange(1,m+1))
# same output
要么
np.logspace
np.logspace(1,m,m,True,x[:,None])
# same output
要么
np.multply.accumulate
或cumprod
np.multiply.accumulate(np.broadcast_to(x,(m,np.size(x))),axis=0).T
# same output
np.broadcast_to(x,(m,np.size(x))).cumprod(axis=0).T
# same output
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句