The function detect_seq.py
detects initial and final indices of sequential data identical to parameter value
in x
.
Use parameter min_seq
to set the minimum number of sequential values to detect.
The signature of detect_seq.py
is:
idx = detect_seq(x, value=np.nan, index=False, min_seq=1, max_alert=0, show=False, ax=None)
from detecta import detect_seq
import numpy as np
%matplotlib inline
help(detect_seq)
Help on function detect_seq in module detecta.detect_seq: detect_seq(x, value=nan, index=False, min_seq=1, max_alert=0, show=False, ax=None) Detect indices in x of sequential data identical to value. Parameters ---------- x : 1D numpy array_like data value : number, optional. Default = np.nan Value to be found in data index : bool, optional. Default = False Set to True to return a 2D array of initial and final indices where data is equal to value or set to False to return an 1D array of Boolean values with same length as x with True where x is equal to value and False where x is not equal to value. min_seq : integer, optional. Default = 1 Minimum number of sequential values to detect max_alert : number, optional. Default = 0 Minimal number of sequential data for a message to be printed with information about these indices. Set to 0 to not print any message. show : bool, optional. Default = False Show plot (True) of not (False). ax : matplotlib object, optional. Default = None Matplotlib axis object where to plot. Returns ------- idx : 1D or 2D numpy array_like 2D numpy array [indi, indf] of initial and final indices (if index is equal to True) or 1D array of Boolean values with same length as x (if index is equal to False). References ---------- .. [1] https://github.com/demotu/detecta/blob/master/docs/detect_seq.ipynb Examples -------- >>> x = [1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0] >>> detect_seq(x, 0) >>> detect_seq(x, 0, index=True) >>> detect_seq(x, 0, index=True, min_seq=2) >>> detect_seq(x, 10) >>> detect_seq(x, 10, index=True) >>> detect_seq(x, 0, index=True, min_seq=2, show=True) >>> detect_seq(x, 0, index=True, max_alert=2) Version history --------------- '1.0.1': Part of the detecta module - https://pypi.org/project/detecta/
x = [1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0]
detect_seq(x, 0)
array([False, True, True, True, False, False, False, True, False, True, True, True])
detect_seq(x, 0, index=True)
array([[ 1, 3], [ 7, 7], [ 9, 11]])
detect_seq(x, 0, index=True, min_seq=2)
array([[ 1, 3], [ 9, 11]])
detect_seq(x, 10)
array([False, False, False, False, False, False, False, False, False, False, False, False])
detect_seq(x, 10, index=True)
array([], shape=(0, 2), dtype=int64)
detect_seq(x, 1, index=True, min_seq=2, show=True)
array([[4, 6]])
detect_seq(x, 1, index=False, min_seq=1, show=True)
array([ True, False, False, False, True, True, True, False, True, False, False, False])
detect_seq(x, 0, index=True, min_seq=2, show=True)
array([[ 1, 3], [ 9, 11]])
detect_seq(x, 0, index=True, max_alert=2)
Sequential data equal or longer than 2: ([2], [1 3]) Sequential data equal or longer than 2: ([2], [ 9 11])
array([[ 1, 3], [ 7, 7], [ 9, 11]])
x = [1, 2, np.nan, np.nan, 5, 4, 5, np.nan, 2, 1, 2]
detect_seq(x, np.nan, index=True, max_alert=2, show=True)
array([[2, 3], [7, 7]])