UCI-HAR (Activity Recognition) Dataset
load_uci_har
Section titled “load_uci_har”Defined in: griffith_logic.datasets.har.load_uci_har (function)
def load_uci_har( root: str | None = None, download: bool = True, return_subjects: bool = False,)Reads the raw Inertial Signals of UCI-HAR (9 channels x 128 steps).
The default location is data-uci-har/ at the repo root (same resolution rule as resolve_data_root).
If har_raw.npz exists, only that file is read. Otherwise the text files under
UCI HAR Dataset/ are read and har_raw.npz is created from them; if that directory is
also missing, the zip (about 60 MB) is downloaded and extracted.
Arguments
root— data directory. If None,data-uci-har/is resolved automaticallydownload— whether downloading is allowed when the data is not on disk. If False and the data is not found,FileNotFoundErroris raisedreturn_subjects— if True, also returns the subject-ID arrays (1..30)
Returns
(x_train, y_train, x_test, y_test). x_* is a float32 (N, 9, 128) array
(channel order is UCI_HAR_CHANNELS) and y_* is an int64 array of 0..5.
With return_subjects=True, a 6-tuple is returned with subject_train, subject_test
(int64, the ID of the subject who produced each window) appended.
Raises
FileNotFoundError: the data is missing and download=False
Examples using this API
- Activity Recognition (UCI-HAR, Phone Accelerometer) —
examples/27_har.py
fit_thermometer_thresholds
Section titled “fit_thermometer_thresholds”Defined in: griffith_logic.datasets.har.fit_thermometer_thresholds (function)
def fit_thermometer_thresholds(x_train: np.ndarray, n_thresholds: int = 4) -> np.ndarrayChooses per-channel thermometer thresholds from quantiles of the training data.
All samples and time steps of a channel are treated as a single distribution, and the
i / (n_thresholds + 1) quantiles (i = 1..n_thresholds) become the thresholds.
With 4 thresholds these are the 20 / 40 / 60 / 80th percentiles. Because the cuts are
quantiles, every bit is on for a roughly balanced share of the training data
(which makes good use of the information).
Arguments
x_train— real-valued signal of shape(N, C, L). Pass the training split only (mixing the test distribution into the thresholds makes the evaluation optimistic)n_thresholds— number of thresholds (bits) per channel
Returns
float32 array of shape (C, n_thresholds). Each row is ascending.
Examples using this API
- Activity Recognition (UCI-HAR, Phone Accelerometer) —
examples/27_har.py
thermometer_encode
Section titled “thermometer_encode”Defined in: griffith_logic.datasets.har.thermometer_encode (function)
def thermometer_encode( x: np.ndarray, n_thresholds: int = 4, thresholds: np.ndarray | None = None,) -> np.ndarrayConverts a real-valued signal into a thermometer code (0/1 bits that stack up monotonically).
For a value v in channel c, it emits bit_t = (v > θ[c, t]) for each threshold
θ[c, 0] <= θ[c, 1] <= .... Because the thresholds are ascending, bit_t = 1 implies that
every bit of a lower threshold is also 1 (the bits fill from the bottom, like a thermometer).
There are two ways to choose the thresholds. If you pass thresholds, they are used as is
(normally thresholds built by fit_thermometer_thresholds(x_train, n) from quantiles of the
training data, applied identically to the training and test splits). With None, the
quantiles of the input x itself are used (same formula as fit_thermometer_thresholds).
Doing that on the test split leaks its distribution, so always pass training-derived
thresholds for evaluation.
Arguments
x— real-valued signal of shape(N, C, L)(float array)n_thresholds— number of thresholds whenthresholdsis None. Ignored whenthresholdsis giventhresholds— ascending thresholds of shape(C, T)or(T,).(T,)is shared by all channels
Returns
float32 array of shape (N, C, L, T) (values 0.0 / 1.0).
reshape(N, -1) gives one input line of C × L × T bits
(ordered channel → time step → threshold).
Examples using this API
- Activity Recognition (UCI-HAR, Phone Accelerometer) —
examples/27_har.py