def n_peaks_valleys(x):
    """Find number of peaks and valleys in an array of values.

    Parameters
    ----------
    x : array-like, shape = (n_samples)
        Data samples, where n_samples is the number of samples.

    Returns
    -------
    n_changes : number of changes (peaks and valleys).
    """
    diff_sign = np.sign(x[1:] - x[:-1])
    return np.count_nonzero(diff_sign[1:] != diff_sign[:-1])
Find number of peaks and valleys in an array of values.

    Parameters
    ----------
    x : array-like, shape = (n_samples)
        Data samples, where n_samples is the number of samples.

    Returns
    -------
    n_changes : number of changes (peaks and valleys).
    
optbinning/binning/auto_monotonic.py