2017-09-05 49 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) 
相关问题