我有一个函数可以根据给定的 x 构建多项式:[1, x^2,x^3,x^4,...,x^ Degree]
def build_poly(x, degree):
"""polynomial basis functions for input data x, for j=0 up to j=degree."""
D = len(x)
polyome = np.ones((D, 1))
for i in range(1, degree+1):
polyome = np.c_[polyome, x**i]
return polyome
现在,我想计算给定 x 的多项式,但忽略 sume 值。
因此,这就是我所做的:
创建X:
x=np.array([[1,2,3],[4,5,6]])])
我用我想省略的值掩盖了该值:
masked_x= np.ma.masked_equal(x, 5)
print(masked_x)
但是当我进行计算时:
print(build_poly(masked_x,2))
遮蔽已经消失。 为什么? 我想让程序省略屏蔽元素
最佳答案
显然,在使用掩码数组时,必须始终使用例程的 numpy.ma
版本。任何偏离这一点的行为,numpy 都会“忘记”屏蔽元素的存在。
def build_poly(x, degree):
"""polynomial basis functions for input data x, for j=0 up to j=degree."""
D = len(x)
polyome = np.ones((D, 1))
for i in range(1, degree+1):
polyome = np.ma.concatenate([polyome, np.ma.power(x,i)], axis=1)
return polyome
关于python - Numpy:计算中的屏蔽元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46876004/