How to find a replacement for an obsolete function in scipy?

For example, this function is thresholddeprecated according to doc .

However, the dock did not say any replacement. Is it just a thing of the future, or is there already a replacement? If so, how do I find the replacement function?

+4
source share
2 answers

It took a bit of digging, but here is the code for 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

But before that, I found that I redid it from the documentation:

An example of an array of documents:

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])

Suggested replacement

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

Circumcision to the maximum or min makes sense; a threshold, on the other hand, turns all values ​​outside the limits into another value, such as 0 or -1. That doesn't sound so helpful. But this is not easy to achieve:

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 min max.

+3

, np.clip , :

np.clip(array-threshold,0,1)
0

Source: https://habr.com/ru/post/1685062/


All Articles