Skip to content

Median Function

The median function computes the median of a window. When combined with the SlidingWindow abstraction, the median function can be used to compute the median feature of a time series. The median is defined as: (write the formula as two cases for even and odd length vectors and index with i for each case)

\[ \text{median}(x) = \begin{cases} 0.5 \cdot (x_{\lfloor n/2 \rfloor} + x_{\lceil n/2 \rceil}) & \text{if $n$ is even} \\ x_{\lfloor n/2 \rfloor} & \text{if $n$ is odd} \end{cases} \]

where \(x\) is a vector of length \(n\).

Compute the median of the values in x.

Parameters:

Name Type Description Default
x ndarray

The array to compute the median of.

required
method str

The method to use when computing the quantile. Default is 'linear'. See numpy.quantile for more information.

'linear'
where Callable[[Union[int, float, int_, float_]], Union[bool, bool_]]

A function that takes a value and returns True or False. Default is lambda x: not np.isnan(x) i.e. a measurement is valid if it is not a NaN value.

lambda : not numpy.isnan(x)

Returns:

Type Description
Union[float, float_]

The median of the values in x.

Examples

import numpy as np
import autonfeat as aft
import autonfeat.functional as F

# Random data
n_samples = 100
x = np.random.rand(n_samples)

# Create sliding window
ws = 10
ss = 10
window = aft.SlidingWindow(window_size=ws, step_size=ss)

# Get featurizer
featurizer = window.use(F.median_tf)

# Get features
features = featurizer(x)

# Print features
print(features)

If you enjoy using AutonFeat, please consider starring the repository ⭐️.