2017-09-05 50 views
3

例如,此threshold函數已根據doc棄用。如何在scipy中找到替代棄用函數?

但是,該文檔沒有提及任何替換。它只是在未來,或已經有一個替代品?如果是這樣,如何找到替換函數?

+1

由於文檔中提到剛剛使用'numpy.clip()' - 這裏的談話上的[' scipy'列表](https://mail.scipy.org/pipermail/scipy-dev/2015-July/020844.html) – AChampion

+0

@AChampion謝謝!但在我看來,numpy.clip是不同的。例如,如何使小數字零? numpy.clip只能剪輯到間隔邊緣 – user15964

+1

如果你有一個數組'a',那麼'a [a

回答

3

花了一點挖,但這裏是爲threshold代碼(scipy/stats/mstats_basic.py):

def threshold(a, threshmin=None, threshmax=None, newval=0): 
    a = ma.array(a, copy=True) 
    mask = np.zeros(a.shape, dtype=bool) 
    if threshmin is not None: 
     mask |= (a < threshmin).filled(False) 

    if threshmax is not None: 
     mask |= (a > threshmax).filled(False) 

    a[mask] = newval 
    return a 

但在此之前,我發現,我反向從文檔設計的它:

例陣列從文檔:

In [152]: a = np.array([9, 9, 6, 3, 1, 6, 1, 0, 0, 8]) 
In [153]: stats.threshold(a, threshmin=2, threshmax=8, newval=-1) 
/usr/local/bin/ipython3:1: DeprecationWarning: `threshold` is deprecated! 
stats.threshold is deprecated in scipy 0.17.0 
    #!/usr/bin/python3 
Out[153]: array([-1, -1, 6, 3, -1, 6, -1, -1, -1, 8]) 

建議的更換

In [154]: np.clip(a,2,8) 
Out[154]: array([8, 8, 6, 3, 2, 6, 2, 2, 2, 8]) 
.... 

剪切到最大或最小是有道理的;另一方面,閾值將所有超出邊界的值轉換爲其他值,例如0或-1。聽起來不太有用。但是,這並不難實現:

In [156]: mask = (a<2)|(a>8) 
In [157]: mask 
Out[157]: array([ True, True, False, False, True, False, True, True, True, False], dtype=bool) 
In [158]: a1 = a.copy() 
In [159]: a1[mask] = -1 
In [160]: a1 
Out[160]: array([-1, -1, 6, 3, -1, 6, -1, -1, -1, 8]) 

這是基本相同,我引用的代碼,只有在它如何處理None情況下的最小或最大不同。

+0

謝謝。所以看起來np.clip並不是'threshold'的直接替代 – user15964

0

對於它的價值,np.clip是直接替換的門檻,如果使用得當:

np.clip(array-threshold,0,1) 
相關問題