python - Numpy:计算中的屏蔽元素

标签 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

遮蔽已经消失。 为什么? 我想让程序省略屏蔽元素

最佳答案

显然,在使用掩码数组时,必须始终使用例程的 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/

相关文章:

python - 如何在 Django 表单中创建用户无法编辑的只读字段?

python - 如何转换日期时间列标题(例如 2007-03-01 00 :00:00) into Date-Month-Year format i. e. 2007-03-01

python - 使用 f2py 从 Fortran 修改 Numpy 字符串数组

Matlab:eps的逆向?正重量的准确性?

python - 如何打印 Gridsearch 中每个组合的准确度分数?

python - 每当我开始时,Pyspark都会给这些

python - 如何切片 2D Python 数组? : "TypeError: list indices must be integers, not tuple" 失败

python - 在 python 中使用 3 位小数

node.js - 如何执行密集的 Node.js 计算

python - 如何提高 python 循环速度?