2016-12-07 42 views
4

我最近從numpy 1.11升級到numpy 1.13,希望擺脫這個掩碼數組警告,但它仍然存在:numpy 1.13 MaskedArrayFutureWarning:在具有共享掩碼的掩碼陣列上設置項目不會複製掩碼

MaskedArrayFutureWarning: setting an item on a masked array which has a shared mask will not copy the mask and also change the original mask array in the future. Check the NumPy 1.11 release notes for more information.*

基本上我的代碼只是改變了一個掩碼數組中的一些值,我不確定這個錯誤甚至意味着什麼。

我希望升級到numpy 1.13可以解決這個問題,但我認爲這個錯誤在我的最後。

爲清楚起見,我不顧警告參考1.11運行numpy的1.13:

的Python 2.7.12(默認情況下,二零一六年十一月十九日,6時48分10秒)

[GCC 5.4。 0 20160609] on linux2

請輸入「help」,「copyright」,「credits」或「license」以獲取更多信息。

作爲np進口numpy

np。 版本

'1.13.0.dev0 +未知'

感謝您的幫助。 Cat

+0

您是否研究過此部分:https://docs.scipy.org/doc/numpy/release.html#assigning-to-slices-views-of-maskedarray – hpaulj

+0

有關此警告的擴展討論:https:// github .COM/numpy的/ numpy的/問題/ 7164。我沒有看到它在新版本中已被更改的證據。 – hpaulj

回答

7

這個共享的面具業務有點混亂。

當前的行爲:

In [150]: x=np.ma.masked_greater(np.arange(8),5) 
In [151]: x 
Out[151]: 
masked_array(data = [0 1 2 3 4 5 -- --], 
      mask = [False False False False False False True True], 
     fill_value = 999999) 
In [152]: y=x[3:6]   # a view 
In [153]: y[0]=30   # modify the view 
/usr/local/bin/ipython3:1: MaskedArrayFutureWarning: setting an item on a masked array which has a shared mask will not copy the mask and also change the original mask array in the future. 
Check the NumPy 1.11 release notes for more information. 
    #!/usr/bin/python3 

data值變化與源

In [154]: y 
Out[154]: 
masked_array(data = [30 4 5], 
      mask = [False False False], 
     fill_value = 999999) 
In [155]: x 
Out[155]: 
masked_array(data = [0 1 2 30 4 5 -- --], 
      mask = [False False False False False False True True], 
     fill_value = 999999) 

但掩碼值變化共享不是:

In [156]: y.mask[0]=True 
In [157]: y 
Out[157]: 
masked_array(data = [-- 4 5], 
      mask = [ True False False], 
     fill_value = 999999) 
In [158]: x 
Out[158]: 
masked_array(data = [0 1 2 30 4 5 -- --], 
      mask = [False False False False False False True True], 
     fill_value = 999999) 

建立一個新的視圖,並撥打unshare方法:

In [159]: y=x[3:6] 
In [160]: y.unshare_mask() 
Out[160]: 
masked_array(data = [30 4 5], 
      mask = [False False False], 
     fill_value = 999999) 
In [161]: y[0]=31 
In [162]: y 
Out[162]: 
masked_array(data = [31 4 5], 
      mask = [False False False], 
     fill_value = 999999) 
In [163]: x 
Out[163]: 
masked_array(data = [0 1 2 31 4 5 -- --], 
      mask = [False False False False False False True True], 
     fill_value = 999999) 

這會更改data,而不會發出警告。

In [172]: x=np.ma.masked_greater(np.arange(8),5) 
In [174]: y=x[3:6] 
In [175]: y._sharedmask=False 
In [176]: y[0]=30 
In [177]: y.mask[0]=True 
In [178]: y 
Out[178]: 
masked_array(data = [-- 4 5], 
      mask = [ True False False], 
     fill_value = 999999) 
In [179]: x 
Out[179]: 
masked_array(data = [0 1 2 -- 4 5 -- --], 
      mask = [False False False True False False True True], 
     fill_value = 999999) 

新值和掩碼同時出現在yx

未來的行爲,沒有警告,可以與生產。

底線是 - 當您更改y(數據或掩碼)中的值時,x中的掩碼應該發生什麼?是否改變?

=================

或者哪裏在視圖中設置的數據值也改變了面具可能是一個更清晰的情況:

In [199]: x=np.ma.masked_greater(np.arange(8),5) 
In [200]: y=x[4:] 
In [201]: y 
Out[201]: 
masked_array(data = [4 5 -- --], 
      mask = [False False True True], 
     fill_value = 999999) 
In [202]: y[-1]=0 
/usr/local/bin/ipython3:1: MaskedArrayFutureWarning: setting an item on a masked array which has a shared mask will not copy the mask and also change the original mask array in the future. 
Check the NumPy 1.11 release notes for more information. 
    #!/usr/bin/python3 
In [203]: y 
Out[203]: 
masked_array(data = [4 5 -- 0], 
      mask = [False False True False], 
     fill_value = 999999) 
In [204]: x 
Out[204]: 
masked_array(data = [0 1 2 3 4 5 -- --], 
      mask = [False False False False False False True True], 
     fill_value = 999999) 

最後的y值是揭密,但相應的x不是(我應該顯示更改x.data)。這是您目前被警告的行爲。

但隨着future行爲:

In [205]: y=x[4:] 
In [206]: y._sharedmask=False 
In [207]: y[-1]=0 
In [208]: y 
Out[208]: 
masked_array(data = [4 5 -- 0], 
      mask = [False False True False], 
     fill_value = 999999) 
In [209]: x 
Out[209]: 
masked_array(data = [0 1 2 3 4 5 -- 0], 
      mask = [False False False False False False True False], 
     fill_value = 999999) 

x數據和口罩,用y而變化。

+0

非常有幫助!真的希望這個答案有更多的知名度... UPVOTE! – DilithiumMatrix

+0

很好的解釋。直到現在,我還無法接受這個警告 – ascripter