neurostatx.statistics.utils
Functions
| Name | Description |
|---|---|
KNNimputation | Impute missing values in df from a complete reference table. |
apply_various_models | Apply a fitted factor or sklearn transformer to df. |
KNNimputationfunction#
def KNNimputation(ref_df, df, n_neighbors=5, weights='distance', metric='nan_euclidean', keep_all_features=True)Impute missing values in df from a complete reference table.
Fits sklearn KNNImputer on ref_df and applies it to df. Both
tables must share the same columns, and ref_df should contain no
missing values.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
ref_df | pd.DataFrame | required | Complete reference dataset used to learn neighbor relationships. |
df | pd.DataFrame | required | Dataset to impute. |
n_neighbors | int | 5 | Number of neighbors. Defaults to 5. |
weights | str | 'distance' | Neighbor weighting, "uniform" or "distance". Defaults to
"distance". |
metric | str | 'nan_euclidean' | Distance metric. Defaults to "nan_euclidean". |
keep_all_features | bool | True | If True, columns that are entirely missing are still imputed. Defaults to True. |
Returns
outpd.DataFrame- Imputed dataset.
Examples
>>> import pandas as pd>>> import numpy as np>>> from neurostatx.statistics.utils import KNNimputation>>> ref = pd.DataFrame({"a": [1.0, 2.0, 3.0], "b": [2.0, 4.0, 6.0]})>>> df = pd.DataFrame({"a": [1.5, np.nan], "b": [3.0, 5.0]})>>> KNNimputation(ref, df, n_neighbors=2).isna().sum().sum()0apply_various_modelsfunction#
def apply_various_models(df, mod)Apply a fitted factor or sklearn transformer to df.
Supports semopy models, sklearn transformers, and factor_analyzer models.
Parameters
| Name | Type | Description |
|---|---|---|
df | pd.DataFrame | Data to transform. |
mod | object | Fitted model exposing predict_factors or transform. |
Returns
ypd.DataFrame- Transformed factor or component scores.
Examples
>>> import pandas as pd>>> from sklearn.decomposition import PCA>>> from neurostatx.statistics.utils import apply_various_models>>> df = pd.DataFrame({"a": [1.0, 2.0, 3.0], "b": [2.0, 4.0, 6.0]})>>> pca = PCA(n_components=1).fit(df)>>> apply_various_models(df, pca).shape(3, 1)