Descriptor Preprocessor
- class DescriptorPreprocessor(nan_strategy='median', max_nan_fraction=0.2, variance_threshold=0.0, with_mean=True, with_std=True)[source]
Clean and standardize a molecular descriptor matrix.
Performs the following steps, in order, on a numeric descriptor matrix:
Drop high-NaN columns — any column whose fraction of NaN values exceeds
max_nan_fractionis removed.Drop constant columns — any column whose variance (computed ignoring NaNs, only over columns that survived step 1) is less than or equal to
variance_thresholdis removed. This catches descriptors that are identical for every molecule and would carry no information.Impute remaining NaNs — leftover NaNs in the kept columns are filled with the per-column
medianormeanlearned at fit time.Standardize — the surviving columns are mean-centered and scaled to unit variance with
StandardScaler(each step optional viawith_mean/with_std).
The class follows scikit-learn conventions: hyperparameters are stored verbatim in
__init__, fitted state lives in trailing-underscore attributes, and the class inheritsfit_transformfromTransformerMixin. It can therefore be dropped into aPipelineorColumnTransformer.- Parameters:
nan_strategy ({"median", "mean"}, default="median") – Statistic used to impute remaining NaNs in columns that survive the
max_nan_fractionfilter. Median is more robust to outlier descriptor values.max_nan_fraction (float, default=0.2) – Columns whose fraction of NaN values strictly exceeds this threshold are dropped. Use 1.0 to keep every column regardless of NaN count, and 0.0 to drop any column containing a single NaN.
variance_threshold (float, default=0.0) – Columns with variance
<= variance_threshold(computed ignoring NaNs) are dropped. The default removes truly constant descriptors; raise it to also drop near-constant ones.with_mean (bool, default=True) – Mean-center each surviving column. Passed through to the underlying
StandardScaler.with_std (bool, default=True) – Scale each surviving column to unit variance. Passed through to the underlying
StandardScaler.
- kept_columns_
Boolean mask of columns retained after the NaN and variance filters.
Truemeans the column was kept.- Type:
ndarray of bool, shape (n_features,)
- kept_indices_
Integer indices of the retained columns, equivalent to
np.where(kept_columns_)[0].- Type:
ndarray of int
- impute_values_
Per-column imputation value (median or mean) for the retained columns, computed on the training data ignoring NaNs.
- Type:
ndarray of shape (n_kept_features,)
- scaler_
Fitted scaler applied to the imputed, retained columns.
- Type:
sklearn.preprocessing.StandardScaler
Examples
>>> import numpy as np >>> X = np.array([[1.0, 2.0, 5.0, np.nan], ... [2.0, 2.0, 6.0, 1.0], ... [3.0, 2.0, 7.0, 2.0]]) >>> pre = DescriptorPreprocessor() >>> X_clean = pre.fit_transform(X) >>> X_clean.shape # NaN and constant columns dropped (1/3 > 0.2), rest scaled (3, 2)
- fit(X, y=None)[source]
Learn the column mask, imputation values, and scaler.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Raw descriptor matrix. May contain NaNs (but not infs).
y (Ignored) – Present for scikit-learn API compatibility.
- Returns:
self – The fitted transformer.
- Return type:
- Raises:
ValueError – If hyperparameters are invalid, or if every column gets dropped by the NaN and variance filters.
- transform(X)[source]
Apply the learned column mask, imputation, and scaling.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Raw descriptor matrix with the same number of columns as the matrix seen during
fit. May contain NaNs (but not infs).- Returns:
X_out – Cleaned, imputed, and (optionally) standardized matrix.
- Return type:
ndarray of shape
(n_samples, n_features_out_)- Raises:
sklearn.exceptions.NotFittedError – If
fithas not been called.ValueError – If the number of features in
Xdoes not match the training matrix.