Skip to content

Data Density Function

The data density function computes the ratio of valid values in a sliding window to the total number of values in the window. See n-valid for more details on how valid values are computed. It can be coupled with the SlidingWindow abstraction to compute the data density feature of a time series. It can be defined as:

\[ \text{density} = \frac{N_{valid}}{N_{total}} \]

where \(N_{valid}\) is the number of valid values in a window \(W\) and \(N_{total}\) is the total number of values in \(W\).

Compute the data density of the array x.

Parameters:

Name Type Description Default
x ndarray

The array to compute the data density of.

required
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 data density of measurements in x.

Raises:

Type Description
DivideByZeroError

If x is empty.

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.data_density_tf)

# Get features
features = featurizer(x)

# Print features
print(features)

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