numpy:计算中的蒙版元素 [英] Numpy: Masked elements in computation

查看:161
本文介绍了numpy:计算中的蒙版元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有根据给定的x构造多项式的功能:[1,x ^ 2,x ^ 3,x ^ 4,...,x ^ degree]

I have a function to built a polynomial from a given 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计算多项式,但是省略求和值.

Now, I would like to calculate polynom for a given x, but omiting sume values.

因此,这就是我所做的:

Hence, what this is what I did:

创建的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))

遮罩消失了. 为什么? 我想让程序省略被遮罩的元素

The masking has disappeeared. Why ? I want to have the program omit the masked elements

推荐答案

显然,在使用掩码数组时,必须始终使用例程的numpy.ma版本.与此不符之处在于,存在蒙版元素的numpy'forgets'.

Apparently when working with masked arrays one must consistently use the numpy.ma versions of the routines. Any departure from this, and numpy 'forgets' that masked elements are present.

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

这篇关于numpy:计算中的蒙版元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆