簡體   English   中英

numpy:計算中的蒙版元素

[英]Numpy: Masked elements in 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計算多項式,但忽略求和值。

因此,這就是我所做的:

創建了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'forgets'。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM