Python numpy.percentile() Examples

The following are 30 code examples of numpy.percentile(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: toolbox.py    From xalpha with MIT License 6 votes vote down vote up
def analyse_percentile(cpdf, col):
        percentile = [1, 5, 25, 50, 75, 95, 99]
        r = [round(d, 3) for d in np.percentile(list(cpdf[col]), percentile)]
        print(
            "\n预测偏差分位:",
            "\n1% 分位: ",
            r[0],
            "\n5% 分位: ",
            r[1],
            "\n25% 分位: ",
            r[2],
            "\n50% 分位: ",
            r[3],
            "\n75% 分位: ",
            r[4],
            "\n95% 分位: ",
            r[5],
            "\n99% 分位: ",
            r[6],
        ) 
Example #2
Source File: test_nanfunctions.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_multiple_percentiles(self):
        perc = [50, 100]
        mat = np.ones((4, 3))
        nan_mat = np.nan * mat
        # For checking consistency in higher dimensional case
        large_mat = np.ones((3, 4, 5))
        large_mat[:, 0:2:4, :] = 0
        large_mat[:, :, 3:] *= 2
        for axis in [None, 0, 1]:
            for keepdim in [False, True]:
                with suppress_warnings() as sup:
                    sup.filter(RuntimeWarning, "All-NaN slice encountered")
                    val = np.percentile(mat, perc, axis=axis, keepdims=keepdim)
                    nan_val = np.nanpercentile(nan_mat, perc, axis=axis,
                                               keepdims=keepdim)
                    assert_equal(nan_val.shape, val.shape)

                    val = np.percentile(large_mat, perc, axis=axis,
                                        keepdims=keepdim)
                    nan_val = np.nanpercentile(large_mat, perc, axis=axis,
                                               keepdims=keepdim)
                    assert_equal(nan_val, val)

        megamat = np.ones((3, 4, 5, 6))
        assert_equal(np.nanpercentile(megamat, perc, axis=(1, 2)).shape, (2, 3, 6)) 
Example #3
Source File: adagan.py    From adagan with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _compute_data_weights_topk(self, opts, density_ratios):
        """Put a uniform distribution on K points with largest prob real data.

        This is a naiive heuristic which makes next GAN concentrate on those
        points of the training set, which were classified correctly with
        largest margins. I.e., out current mixture model is not capable of
        generating points looking similar to these ones.
        """
        threshold = np.percentile(density_ratios,
                                  opts["topk_constant"]*100.0)
        # Note that largest prob_real_data corresponds to smallest density
        # ratios.
        mask = density_ratios <= threshold
        data_weights = np.zeros(self._data_num)
        data_weights[mask] = 1.0 / np.sum(mask)
        return data_weights 
Example #4
Source File: test_quantile.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_quantile_multi(self):

        qs = [.1, .9]
        result = self.ts.quantile(qs)
        expected = pd.Series([np.percentile(self.ts.dropna(), 10),
                              np.percentile(self.ts.dropna(), 90)],
                             index=qs, name=self.ts.name)
        tm.assert_series_equal(result, expected)

        dts = self.ts.index.to_series()
        dts.name = 'xxx'
        result = dts.quantile((.2, .2))
        expected = Series([Timestamp('2000-01-10 19:12:00'),
                           Timestamp('2000-01-10 19:12:00')],
                          index=[.2, .2], name='xxx')
        tm.assert_series_equal(result, expected)

        result = self.ts.quantile([])
        expected = pd.Series([], name=self.ts.name, index=Index(
            [], dtype=float))
        tm.assert_series_equal(result, expected) 
Example #5
Source File: longtermmean.py    From yatsm with MIT License 6 votes vote down vote up
def scale_EVI(evi, periods, qmin=10, qmax=90):
    """ Returns EVI scaled to upper and lower quantiles

    Quantiles are calculated based on EVI within some year-to-year interval.
    As part of finding the quantiles, EVI values not within the (0, 1) range
    will be removed.

    Args:
        evi (np.ndarray): EVI values
        periods (np.ndarray): intervals of years to group and scale together
        qmin (float, optional): lower quantile for scaling (default: 10)
        qmax (float, optional): upper quantile for scaling (default: 90)

    Returns:
        np.ndarray: scaled EVI array

    """
    _evi = evi.copy()
    for u in np.unique(periods):
        index = np.where(periods == u)
        evi_min = np.percentile(evi[index], qmin)
        evi_max = np.percentile(evi[index], qmax)
        _evi[index] = (evi[index] - evi_min) / (evi_max - evi_min)

    return _evi 
Example #6
Source File: bench_grad_alpha.py    From entmax with MIT License 6 votes vote down vote up
def bench(f_):
    timings_fwd = []
    timings_bck = []
    for _ in range(100):

        with f_ as f:
            tic = time.perf_counter()
            f.forward()
            torch.cuda.synchronize()
            toc = time.perf_counter()
            timings_fwd.append(toc - tic)

            tic = time.perf_counter()
            f.backward()
            torch.cuda.synchronize()
            toc = time.perf_counter()
            timings_bck.append(toc - tic)

    return (np.percentile(timings_fwd, [25, 50, 75]),
            np.percentile(timings_bck, [25, 50, 75])) 
Example #7
Source File: online.py    From contextualbandits with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _add_bootstrapped_inputs(self, base_algorithm, batch_sample_method,
                                 nsamples, njobs_samples, percentile,
                                 ts_byrow = False, ts_weighted = False):
        assert (batch_sample_method == 'gamma') or (batch_sample_method == 'poisson')
        assert isinstance(nsamples, int)
        assert nsamples >= 1
        self.batch_sample_method = batch_sample_method
        self.nsamples = nsamples
        self.njobs_samples = _check_njobs(njobs_samples)
        if not isinstance(base_algorithm, list):
            self.base_algorithm = self._make_bootstrapped(base_algorithm, percentile,
                                                          ts_byrow, ts_weighted)
        else:
            self.base_algorithm = [ \
                self._make_bootstrapped(alg, percentile, ts_byrow, ts_weighted) \
                for alg in base_algorithm] 
Example #8
Source File: test_nanfunctions.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_out(self):
        mat = np.random.rand(3, 3)
        nan_mat = np.insert(mat, [0, 2], np.nan, axis=1)
        resout = np.zeros(3)
        tgt = np.percentile(mat, 42, axis=1)
        res = np.nanpercentile(nan_mat, 42, axis=1, out=resout)
        assert_almost_equal(res, resout)
        assert_almost_equal(res, tgt)
        # 0-d output:
        resout = np.zeros(())
        tgt = np.percentile(mat, 42, axis=None)
        res = np.nanpercentile(nan_mat, 42, axis=None, out=resout)
        assert_almost_equal(res, resout)
        assert_almost_equal(res, tgt)
        res = np.nanpercentile(nan_mat, 42, axis=(0, 1), out=resout)
        assert_almost_equal(res, resout)
        assert_almost_equal(res, tgt) 
Example #9
Source File: online.py    From contextualbandits with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def reset_percentile(self, percentile=80):
        """
        Set the upper confidence bound percentile to a custom number

        Parameters
        ----------
        percentile : int [0,100]
            Percentile of the confidence interval to take.

        Returns
        -------
        self : obj
            This object
        """
        assert (percentile > 0) and (percentile < 100)
        if self.is_fitted:
            self._oracles.reset_attribute("percentile", percentile)
        self.base_algorithm.percentile = percentile
        return self 
Example #10
Source File: online.py    From contextualbandits with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def reset_percentile(self, percentile=30):
        """
        Set the moving percentile to a custom number

        Parameters
        ----------
        percentile : int between 0 and 100
            The new percentile to set. Note that it will still apply
            decay to it after being set through this method.

        Returns
        -------
        self : obj
            This object
        """
        if self.decay_type == 'threshold':
            raise ValueError("Method is not available when not using percentile decay.")
        assert percentile >= 0
        assert percentile <= 100
        self.percentile = percentile
        return self 
Example #11
Source File: test_function_base.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_keepdims(self):
        d = np.ones((3, 5, 7, 11))
        assert_equal(np.percentile(d, 7, axis=None, keepdims=True).shape,
                     (1, 1, 1, 1))
        assert_equal(np.percentile(d, 7, axis=(0, 1), keepdims=True).shape,
                     (1, 1, 7, 11))
        assert_equal(np.percentile(d, 7, axis=(0, 3), keepdims=True).shape,
                     (1, 5, 7, 1))
        assert_equal(np.percentile(d, 7, axis=(1,), keepdims=True).shape,
                     (3, 1, 7, 11))
        assert_equal(np.percentile(d, 7, (0, 1, 2, 3), keepdims=True).shape,
                     (1, 1, 1, 1))
        assert_equal(np.percentile(d, 7, axis=(0, 1, 3), keepdims=True).shape,
                     (1, 1, 7, 1))

        assert_equal(np.percentile(d, [1, 7], axis=(0, 1, 3),
                                   keepdims=True).shape, (2, 1, 1, 7, 1))
        assert_equal(np.percentile(d, [1, 7], axis=(0, 3),
                                   keepdims=True).shape, (2, 1, 5, 7, 1)) 
Example #12
Source File: online.py    From contextualbandits with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def reset_percentile(self, percentile=80):
        """
        Set the upper confidence bound percentile to a custom number

        Parameters
        ----------
        percentile : int [0,100]
            Percentile of the confidence interval to take.

        Returns
        -------
        self : obj
            This object
        """
        assert (percentile > 0) and (percentile < 100)
        if self.is_fitted:
            self._oracles.reset_attribute("alpha", percentile)
        self.base_algorithm.alpha = percentile
        return self 
Example #13
Source File: display.py    From scarlet with MIT License 6 votes vote down vote up
def __init__(self, img, percentiles=[1, 99]):
        """Create norm that is linear between lower and upper percentile of img
        Parameters
        ----------
        img: array_like
            Image to normalize
        percentile: array_like, default=[1,99]
            Lower and upper percentile to consider. Pixel values below will be
            set to zero, above to saturated.
        """
        assert len(percentiles) == 2
        vmin, vmax = np.percentile(img, percentiles)
        # solution for beta assumes flat spectrum at vmax
        stretch = vmax - vmin
        beta = stretch / np.sinh(1)
        super().__init__(minimum=vmin, stretch=stretch, Q=beta) 
Example #14
Source File: test_aggregator.py    From pywr with GNU General Public License v3.0 6 votes vote down vote up
def agg_func(request):
    agg_func_name = request.param

    if agg_func_name == "custom":
        # When using custom you assign the function rather than a string.
        agg_func_name = npy_func = custom_test_func
    elif agg_func_name == "percentile":
        agg_func_name = {
            "func": "percentile",
            "args": [95],
            "kwargs": {}
        }
        npy_func = partial(np.percentile, q=95)
    elif agg_func_name == "percentileofscore":
        agg_func_name = {
            "func": "percentileofscore",
            "kwargs": {
                "score": 0.5,
                "kind": "rank"
            }
        }
        npy_func = partial(percentileofscore_with_axis, score=0.5, kind="rank")
    else:
        npy_func = npy_funcs[agg_func_name]
    return agg_func_name, npy_func 
Example #15
Source File: test_recorders.py    From pywr with GNU General Public License v3.0 6 votes vote down vote up
def test_seasonal_fdc_recorder(self):
        """
        Test the FlowDurationCurveRecorder
        """
        model = load_model("timeseries4.json")

        df = pandas.read_csv(os.path.join(os.path.dirname(__file__), 'models', 'timeseries3.csv'),
                             parse_dates=True, dayfirst=True, index_col=0)

        percentiles = np.linspace(20., 100., 5)

        summer_flows = df.loc[pandas.Timestamp("2014-06-01"):pandas.Timestamp("2014-08-31"), :]
        summer_fdc = np.percentile(summer_flows, percentiles, axis=0)

        model.run()

        rec = model.recorders["seasonal_fdc"]
        assert_allclose(rec.fdc, summer_fdc) 
Example #16
Source File: online.py    From contextualbandits with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self, nchoices, percentile=80, fit_intercept=True,
                 lambda_=1.0, ucb_from_empty=False,
                 beta_prior='auto', smoothing=None, noise_to_smooth=True,
                 assume_unique_reward=False,
                 random_state=None, njobs=-1):
        assert (percentile > 0) and (percentile < 100)
        assert lambda_ > 0.
        base = _LogisticUCB_n_TS_single(lambda_=float(lambda_),
                                        fit_intercept=fit_intercept,
                                        alpha=float(percentile),
                                        ts=False)
        self._add_common_params(base, beta_prior, smoothing, noise_to_smooth, njobs, nchoices,
                                False, None, False, assume_unique_reward,
                                random_state, assign_algo=True, prior_def_ucb=True,
                                force_unfit_predict = ucb_from_empty)
        self.percentile = percentile 
Example #17
Source File: online.py    From contextualbandits with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def reset_threshold(self, threshold="auto"):
        """
        Set the adaptive threshold to a custom number

        Parameters
        ----------
        threshold : float or "auto"
            New threshold to use. If passing "auto", will set it
            to 1.5/nchoices. Note that this threshold will still be
            decayed if the object was initialized with ``decay_type="threshold"``,
            and will still be updated if initialized with ``percentile != None``.

        Returns
        -------
        self : obj
            This object
        """
        if isinstance(threshold, int):
            threshold = float(threshold)
        elif threshold == "auto":
            threshold = 1.5 / self.nchoices
        assert isinstance(threshold, float)
        self.thr = threshold
        return self 
Example #18
Source File: histograms.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _hist_bin_fd(x, range):
    """
    The Freedman-Diaconis histogram bin estimator.

    The Freedman-Diaconis rule uses interquartile range (IQR) to
    estimate binwidth. It is considered a variation of the Scott rule
    with more robustness as the IQR is less affected by outliers than
    the standard deviation. However, the IQR depends on fewer points
    than the standard deviation, so it is less accurate, especially for
    long tailed distributions.

    If the IQR is 0, this function returns 1 for the number of bins.
    Binwidth is inversely proportional to the cube root of data size
    (asymptotically optimal).

    Parameters
    ----------
    x : array_like
        Input data that is to be histogrammed, trimmed to range. May not
        be empty.

    Returns
    -------
    h : An estimate of the optimal bin width for the given data.
    """
    del range  # unused
    iqr = np.subtract(*np.percentile(x, [75, 25]))
    return 2.0 * iqr * x.size ** (-1.0 / 3.0) 
Example #19
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_midpoint(self):
        assert_equal(np.percentile(range(10), 51,
                                   interpolation='midpoint'), 4.5)
        assert_equal(np.percentile(range(11), 51,
                                   interpolation='midpoint'), 5.5)
        assert_equal(np.percentile(range(11), 50,
                                   interpolation='midpoint'), 5) 
Example #20
Source File: nanfunctions.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _nanquantile_ureduce_func(a, q, axis=None, out=None, overwrite_input=False,
                              interpolation='linear'):
    """
    Private function that doesn't support extended axis or keepdims.
    These methods are extended to this function using _ureduce
    See nanpercentile for parameter usage
    """
    if axis is None or a.ndim == 1:
        part = a.ravel()
        result = _nanquantile_1d(part, q, overwrite_input, interpolation)
    else:
        result = np.apply_along_axis(_nanquantile_1d, axis, a, q,
                                     overwrite_input, interpolation)
        # apply_along_axis fills in collapsed axis with results.
        # Move that axis to the beginning to match percentile's
        # convention.
        if q.ndim != 0:
            result = np.moveaxis(result, axis, 0)

    if out is not None:
        out[...] = result
    return result 
Example #21
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_api(self):
        d = np.ones(5)
        np.percentile(d, 5, None, None, False)
        np.percentile(d, 5, None, None, False, 'linear')
        o = np.ones((1,))
        np.percentile(d, 5, None, o, False, 'linear') 
Example #22
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_percentile_no_overwrite(self):
        a = np.array([2, 3, 4, 1])
        np.percentile(a, [50], overwrite_input=False)
        assert_equal(a, np.array([2, 3, 4, 1]))

        a = np.array([2, 3, 4, 1])
        np.percentile(a, [50])
        assert_equal(a, np.array([2, 3, 4, 1])) 
Example #23
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_2D(self):
        x = np.array([[1, 1, 1],
                      [1, 1, 1],
                      [4, 4, 3],
                      [1, 1, 1],
                      [1, 1, 1]])
        assert_array_equal(np.percentile(x, 50, axis=0), [1, 1, 1]) 
Example #24
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_lower_higher(self):

        # interpolation_method 'lower'/'higher'
        assert_equal(np.percentile(range(10), 50,
                                   interpolation='lower'), 4)
        assert_equal(np.percentile(range(10), 50,
                                   interpolation='higher'), 5) 
Example #25
Source File: bench_loss.py    From entmax with MIT License 5 votes vote down vote up
def _bench(f):
    timings = []
    for _ in range(10):
        tic = time.perf_counter()
        f()
        torch.cuda.synchronize()
        toc = time.perf_counter()
        timings.append(toc - tic)
    return np.percentile(timings, [25, 50, 75]) 
Example #26
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_sequence(self):
        x = np.arange(8) * 0.5
        assert_equal(np.percentile(x, [0, 100, 50]), [0, 3.5, 1.75]) 
Example #27
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_axis(self):
        x = np.arange(12).reshape(3, 4)

        assert_equal(np.percentile(x, (25, 50, 100)), [2.75, 5.5, 11.0])

        r0 = [[2, 3, 4, 5], [4, 5, 6, 7], [8, 9, 10, 11]]
        assert_equal(np.percentile(x, (25, 50, 100), axis=0), r0)

        r1 = [[0.75, 1.5, 3], [4.75, 5.5, 7], [8.75, 9.5, 11]]
        assert_equal(np.percentile(x, (25, 50, 100), axis=1), np.array(r1).T)

        # ensure qth axis is always first as with np.array(old_percentile(..))
        x = np.arange(3 * 4 * 5 * 6).reshape(3, 4, 5, 6)
        assert_equal(np.percentile(x, (25, 50)).shape, (2,))
        assert_equal(np.percentile(x, (25, 50, 75)).shape, (3,))
        assert_equal(np.percentile(x, (25, 50), axis=0).shape, (2, 4, 5, 6))
        assert_equal(np.percentile(x, (25, 50), axis=1).shape, (2, 3, 5, 6))
        assert_equal(np.percentile(x, (25, 50), axis=2).shape, (2, 3, 4, 6))
        assert_equal(np.percentile(x, (25, 50), axis=3).shape, (2, 3, 4, 5))
        assert_equal(
            np.percentile(x, (25, 50, 75), axis=1).shape, (3, 3, 5, 6))
        assert_equal(np.percentile(x, (25, 50),
                                   interpolation="higher").shape, (2,))
        assert_equal(np.percentile(x, (25, 50, 75),
                                   interpolation="higher").shape, (3,))
        assert_equal(np.percentile(x, (25, 50), axis=0,
                                   interpolation="higher").shape, (2, 4, 5, 6))
        assert_equal(np.percentile(x, (25, 50), axis=1,
                                   interpolation="higher").shape, (2, 3, 5, 6))
        assert_equal(np.percentile(x, (25, 50), axis=2,
                                   interpolation="higher").shape, (2, 3, 4, 6))
        assert_equal(np.percentile(x, (25, 50), axis=3,
                                   interpolation="higher").shape, (2, 3, 4, 5))
        assert_equal(np.percentile(x, (25, 50, 75), axis=1,
                                   interpolation="higher").shape, (3, 3, 5, 6)) 
Example #28
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_exception(self):
        assert_raises(ValueError, np.percentile, [1, 2], 56,
                      interpolation='foobar')
        assert_raises(ValueError, np.percentile, [1], 101)
        assert_raises(ValueError, np.percentile, [1], -1)
        assert_raises(ValueError, np.percentile, [1], list(range(50)) + [101])
        assert_raises(ValueError, np.percentile, [1], list(range(50)) + [-0.1]) 
Example #29
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_basic(self):
        x = np.arange(8) * 0.5
        assert_equal(np.percentile(x, 0), 0.)
        assert_equal(np.percentile(x, 100), 3.5)
        assert_equal(np.percentile(x, 50), 1.75)
        x[1] = np.nan
        with warnings.catch_warnings(record=True) as w:
            warnings.filterwarnings('always', '', RuntimeWarning)
            assert_equal(np.percentile(x, 0), np.nan)
            assert_equal(np.percentile(x, 0, interpolation='nearest'), np.nan)
            assert_(w[0].category is RuntimeWarning) 
Example #30
Source File: test_function_base.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_nearest(self):
        assert_equal(np.percentile(range(10), 51,
                                   interpolation='nearest'), 5)
        assert_equal(np.percentile(range(10), 49,
                                   interpolation='nearest'), 4)