xtensor
Loading...
Searching...
No Matches
Sorting functions.

Enumerations

enum class  xt::quantile_method {
  xt::quantile_method::interpolated_inverted_cdf = 4 , xt::quantile_method::hazen , xt::quantile_method::weibull , xt::quantile_method::linear ,
  xt::quantile_method::median_unbiased , xt::quantile_method::normal_unbiased
}
 Quantile interpolation method. More...
 

Functions

template<class E >
auto xt::sort (const xexpression< E > &e, std::ptrdiff_t axis=-1)
 Sort xexpression (optionally along axis) The sort is performed using the std::sort functions.
 
template<class E >
auto xt::argsort (const xexpression< E > &e, std::ptrdiff_t axis=-1, sorting_method method=sorting_method::quick)
 Argsort xexpression (optionally along axis) Performs an indirect sort along the given axis.
 
template<class E , class C , class R = detail::flatten_sort_result_type_t<E>, class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
R xt::partition (const xexpression< E > &e, C kth_container, placeholders::xtuph)
 Partially sort xexpression.
 
template<class E , class C , class R = typename detail::linear_argsort_result_type<typename detail::sort_eval_type<E>::type>::type, class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
R xt::argpartition (const xexpression< E > &e, C kth_container, placeholders::xtuph)
 Partially sort arguments.
 
template<class T = double, class E , class P >
auto xt::quantile (E &&e, const P &probas, std::ptrdiff_t axis, T alpha, T beta)
 Compute quantiles over the given axis.
 
template<class T = double, class E , class P >
auto xt::quantile (E &&e, const P &probas, T alpha, T beta)
 Compute quantiles of the whole expression.
 
template<class T = double, class E , class P >
auto xt::quantile (E &&e, const P &probas, std::ptrdiff_t axis, quantile_method method=quantile_method::linear)
 Compute quantiles over the given axis.
 
template<class T = double, class E , class P >
auto xt::quantile (E &&e, const P &probas, quantile_method method=quantile_method::linear)
 Compute quantiles of the whole expression.
 
template<class E >
auto xt::median (E &&e, std::ptrdiff_t axis)
 Find the median along the specified axis.
 
template<layout_type L = ::xt::layout_type::row_major, class E >
auto xt::argmax (const xexpression< E > &e, std::ptrdiff_t axis)
 Find position of maximal value in xexpression By default, the returned index is into the flattened array.
 
template<class E >
auto xt::unique (const xexpression< E > &e)
 Find unique elements of a xexpression.
 
template<class E1 , class E2 >
auto xt::setdiff1d (const xexpression< E1 > &ar1, const xexpression< E2 > &ar2)
 Find the set difference of two xexpressions.
 

Detailed Description

Because sorting functions need to access the tensor data repeatedly, they evaluate their input and may allocate temporaries.

Enumeration Type Documentation

◆ quantile_method

Quantile interpolation method.

Predefined methods for interpolating quantiles, as defined in (Hyndman and Fan, 1996).

See also
(Hyndman and Fan, 1996) R. J. Hyndman and Y. Fan, "Sample quantiles in statistical packages", The American Statistician, 50(4), pp. 361-365, 1996
xt::quantile(E&& e, P const& probas, std::ptrdiff_t axis, xt::quantile_method method)
Enumerator
interpolated_inverted_cdf 

Method 4 of (Hyndman and Fan, 1996) with alpha=0 and beta=1.

hazen 

Method 5 of (Hyndman and Fan, 1996) with alpha=1/2 and beta=1/2.

weibull 

Method 6 of (Hyndman and Fan, 1996) with alpha=0 and beta=0.

linear 

Method 7 of (Hyndman and Fan, 1996) with alpha=1 and beta=1.

median_unbiased 

Method 8 of (Hyndman and Fan, 1996) with alpha=1/3 and beta=1/3.

normal_unbiased 

Method 9 of (Hyndman and Fan, 1996) with alpha=3/8 and beta=3/8.

Definition at line 983 of file xsort.hpp.

Function Documentation

◆ argmax()

template<layout_type L = ::xt::layout_type::row_major, class E >
auto xt::argmax ( const xexpression< E > & e,
std::ptrdiff_t axis )
inline

Find position of maximal value in xexpression By default, the returned index is into the flattened array.

If axis is specified, the indices are along the specified axis.

Parameters
einput xexpression
axisselect axis (optional)
Returns
returns xarray with positions of maximal value

Definition at line 1295 of file xsort.hpp.

◆ argpartition()

template<class E , class C , class R = typename detail::linear_argsort_result_type<typename detail::sort_eval_type<E>::type>::type, class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
R xt::argpartition ( const xexpression< E > & e,
C kth_container,
placeholders::xtuph  )
inline

Partially sort arguments.

Argpartition shuffles the indices to a xexpression in a way so that the index for the kth element in the returned xexpression is in the place it would appear in a sorted array and all elements smaller than this entry are placed (unsorted) before.

The optional third parameter can either be an axis or xnone() in which case the xexpression will be flattened.

This function uses std::nth_element internally.

xt::xarray<float> a = {1, 10, -10, 123};
std::cout << xt::argpartition(a, 0) << std::endl; // {2, 0, 3, 1} the correct entry at index 0
std::cout << xt::argpartition(a, 3) << std::endl; // {0, 1, 2, 3} the correct entry at index 3
std::cout << xt::argpartition(a, {0, 3}) << std::endl; // {2, 0, 1, 3} the correct entries at index 0
and 3
R argpartition(const xexpression< E > &e, C kth_container, placeholders::xtuph)
Partially sort arguments.
Definition xsort.hpp:664
Parameters
einput xexpression
kth_containera container of indices that should contain the correctly sorted value
axiseither integer (default = -1) to sort along last axis or xnone() to flatten before sorting
Returns
xcontainer with indices of partial sort of input

Definition at line 664 of file xsort.hpp.

◆ argsort()

template<class E >
auto xt::argsort ( const xexpression< E > & e,
std::ptrdiff_t axis = -1,
sorting_method method = sorting_method::quick )
inline

Argsort xexpression (optionally along axis) Performs an indirect sort along the given axis.

Returns an xarray of indices of the same shape as e that index data along the given axis in sorted order.

Parameters
exexpression to argsort
axisaxis along which argsort is performed
methodsorting algorithm to use
Returns
argsorted index array
See also
xt::sorting_method

Definition at line 452 of file xsort.hpp.

◆ median()

template<class E >
auto xt::median ( E && e,
std::ptrdiff_t axis )
inline

Find the median along the specified axis.

Given a vector V of length N, the median of V is the middle value of a sorted copy of V, V_sorted - i e., V_sorted[(N-1)/2], when N is odd, and the average of the two middle values of V_sorted when N is even.

Parameters
axisaxis along which the medians are computed. If not set, computes the median along a flattened version of the input.
einput xexpression
Returns
median value

Definition at line 1126 of file xsort.hpp.

◆ partition()

template<class E , class C , class R = detail::flatten_sort_result_type_t<E>, class = std::enable_if_t<!xtl::is_integral<C>::value, int>>
R xt::partition ( const xexpression< E > & e,
C kth_container,
placeholders::xtuph  )
inline

Partially sort xexpression.

Partition shuffles the xexpression in a way so that the kth element in the returned xexpression is in the place it would appear in a sorted array and all elements smaller than this entry are placed (unsorted) before.

The optional third parameter can either be an axis or xnone() in which case the xexpression will be flattened.

This function uses std::nth_element internally.

xt::xarray<float> a = {1, 10, -10, 123};
std::cout << xt::partition(a, 0) << std::endl; // {-10, 1, 123, 10} the correct entry at index 0
std::cout << xt::partition(a, 3) << std::endl; // {1, 10, -10, 123} the correct entry at index 3
std::cout << xt::partition(a, {0, 3}) << std::endl; // {-10, 1, 10, 123} the correct entries at index 0
and 3
R partition(const xexpression< E > &e, C kth_container, placeholders::xtuph)
Partially sort xexpression.
Definition xsort.hpp:569
Parameters
einput xexpression
kth_containera container of indices that should contain the correctly sorted value
axiseither integer (default = -1) to sort along last axis or xnone() to flatten before sorting
Returns
partially sorted xcontainer

Definition at line 569 of file xsort.hpp.

◆ quantile() [1/4]

template<class T = double, class E , class P >
auto xt::quantile ( E && e,
const P & probas,
quantile_method method = quantile_method::linear )
inline

Compute quantiles of the whole expression.

The quantiles are computed over the whole expression, as if flatten in a one-dimensional expression. The function takes the name of a predefined method to compute to interpolate between values.

See also
xt::quantile_method
xt::quantile(E&& e, P const& probas, std::ptrdiff_t axis, xt::quantile_method method)

Definition at line 1076 of file xsort.hpp.

◆ quantile() [2/4]

template<class T = double, class E , class P >
auto xt::quantile ( E && e,
const P & probas,
std::ptrdiff_t axis,
quantile_method method = quantile_method::linear )
inline

Compute quantiles over the given axis.

The function takes the name of a predefined method to compute to interpolate between values.

See also
xt::quantile_method
xt::quantile(E&& e, P const& probas, std::ptrdiff_t axis, T alpha, T beta)

Definition at line 1010 of file xsort.hpp.

◆ quantile() [3/4]

template<class T = double, class E , class P >
auto xt::quantile ( E && e,
const P & probas,
std::ptrdiff_t axis,
T alpha,
T beta )
inline

Compute quantiles over the given axis.

In a sorted array represneting a distribution of numbers, the quantile of a probability p is the the cut value q such that a fraction p of the distribution is lesser or equal to q. When the cutpoint falls between two elemnts of the sample distribution, a interpolation is computed using the alpha and beta coefficients, as descripted in (Hyndman and Fan, 1996).

The algorithm partially sorts entries in a copy along the axis axis.

Parameters
eExpression containing the distribution over which the quantiles are computed.
probasAn list of probability associated with each desired quantiles. All elements must be in the range [0, 1].
axisThe dimension in which to compute the quantiles, i.e the axis representing the distribution.
alphaInterpolation parameter. Must be in the range [0, 1]].
betaInterpolation parameter. Must be in the range [0, 1]].
Template Parameters
TThe type in which the quantile are computed.
Returns
An expression with as many dimensions as the input e. The first axis correspond to the quantiles. The other axes are the axes that remain after the reduction of e.
See also
(Hyndman and Fan, 1996) R. J. Hyndman and Y. Fan, "Sample quantiles in statistical packages", The American Statistician, 50(4), pp. 361-365, 1996
https://en.wikipedia.org/wiki/Quantile

Definition at line 906 of file xsort.hpp.

◆ quantile() [4/4]

template<class T = double, class E , class P >
auto xt::quantile ( E && e,
const P & probas,
T alpha,
T beta )
inline

Compute quantiles of the whole expression.

The quantiles are computed over the whole expression, as if flatten in a one-dimensional expression.

See also
xt::quantile(E&& e, P const& probas, std::ptrdiff_t axis, T alpha, T beta)

Definition at line 960 of file xsort.hpp.

◆ setdiff1d()

template<class E1 , class E2 >
auto xt::setdiff1d ( const xexpression< E1 > & ar1,
const xexpression< E2 > & ar2 )
inline

Find the set difference of two xexpressions.

This returns a flattened xtensor with the sorted, unique values in ar1 that are not in ar2.

Parameters
ar1input xexpression (will be flattened)
ar2input xexpression

Definition at line 1332 of file xsort.hpp.

◆ sort()

template<class E >
auto xt::sort ( const xexpression< E > & e,
std::ptrdiff_t axis = -1 )
inline

Sort xexpression (optionally along axis) The sort is performed using the std::sort functions.

A copy of the xexpression is created and returned.

Parameters
exexpression to sort
axisaxis along which sort is performed
Returns
sorted array (copy)

Definition at line 255 of file xsort.hpp.

◆ unique()

template<class E >
auto xt::unique ( const xexpression< E > & e)
inline

Find unique elements of a xexpression.

This returns a flattened xtensor with sorted, unique elements from the original expression.

Parameters
einput xexpression (will be flattened)

Definition at line 1311 of file xsort.hpp.