Numpy:计算中的蒙面元素

时间:2017-10-22 15:45:00

标签: python numpy computation

我有一个从给定的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]])])

enter image description here

我想要忽略这个值:

masked_x= np.ma.masked_equal(x, 5)
print(masked_x)

enter image description here

但是当我做计算时:

print(build_poly(masked_x,2))

enter image description here

掩蔽消失了。 为什么? 我想让程序省略蒙面元素

1 个答案:

答案 0 :(得分: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
相关问题