### Install HyperImpute from Source Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/README.md Use this command to install the library directly from its source code. ```bash $ pip install . ``` -------------------------------- ### Install Testing Dependencies Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/README.md Install the necessary dependencies for running tests. This command should be executed in your terminal. ```bash pip install .[testing] ``` -------------------------------- ### Install HyperImpute from PyPI Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/README.md Use this command to install the library from the Python Package Index. ```bash $ pip install hyperimpute ``` -------------------------------- ### Import Libraries and Setup HyperImpute Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_01_hyperimpute_with_hyperband.ipynb Imports necessary libraries and configures HyperImpute for reproducible results and logging. This setup is required before running any experiments. ```python import copy import os import time import random import sys import warnings import numpy as np import pandas as pd from typing import Any from hyperimpute.plugins.imputers import Imputers from hyperimpute.utils.distributions import enable_reproducible_results from hyperimpute.utils.benchmarks import compare_models import hyperimpute.logger as log from IPython.display import HTML, display import tabulate import json warnings.filterwarnings("ignore") enable_reproducible_results() imputers = Imputers() log.add(sink=sys.stderr, level="INFO") ``` -------------------------------- ### Setup Imputer and Evaluation Framework Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_02_model_performance.ipynb Initializes the Imputers class and defines functions to get a configured imputer and evaluate dataset performance. The `get_imputer` function sets up specific classifiers and regressors, while `evaluate_dataset_repeated` orchestrates the comparison of models across different scenarios and missing percentages. ```python from pathlib import Path from hyperimpute.plugins.imputers import Imputers from hyperimpute.utils.distributions import enable_reproducible_results import hyperimpute.logger as log from hyperimpute.utils.benchmarks import compare_models enable_reproducible_results() imputers = Imputers() def get_imputer(): return imputers.get( "hyperimpute", optimizer="simple", classifier_seed=["random_forest", "logistic_regression", "xgboost", "catboost"], regression_seed=[ "random_forest_regressor", "linear_regression", "xgboost_regressor", "catboost_regressor", ], ) def evaluate_dataset_repeated( name, X_raw, y, ref_methods=["sklearn_ice", "sklearn_missforest", "sinkhorn", "miwae", "gain"], scenarios=["MAR", "MCAR", "MNAR"], miss_pct=[0.3], n_iter=10, debug=False, ): return compare_models( name=name, evaluated_model=get_imputer(), X_raw=X_raw, ref_methods=ref_methods, scenarios=scenarios, miss_pct=miss_pct, n_iter=n_iter, ) ``` -------------------------------- ### Load Model Selection Trace and Setup Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_03_insights_hyperimpute_model_selection.ipynb Loads the model selection trace from a JSON file and sets up the output directory for diagrams. ```python import json import matplotlib.pyplot as plt from pathlib import Path with open("general_results/model_selection_trace.json") as f: data = json.load(f) output_dir = Path("diagrams/model_selection") ``` -------------------------------- ### Instantiate and Use KNeighborsRegressorPlugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_kneighbors_regressor.md Instantiate the KNeighborsRegressorPlugin and use it to fit and predict on data. This example demonstrates the basic usage flow. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("kneighbors") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) ``` -------------------------------- ### NeuralNetsPlugin Example Usage Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_neural_nets.md Demonstrates how to instantiate and use the NeuralNetsPlugin for fitting and predicting with a dataset. ```APIDOC ### Example ```pycon >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="classifiers").get("neural_nets") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` ``` -------------------------------- ### Experiment Setup and Helper Functions Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_01_hyperimpute_with_naive_search.ipynb Defines functions to get an imputer, save experiment results to a JSON file, and evaluate a dataset repeatedly with specified parameters. These functions are central to running the hyperimpute experiments. ```python from pathlib import Path experiment = "experiments_01_hyperimpute_with_naive_search" def get_imputer(): return imputers.get("hyperimpute", optimizer="simple") def save_results(fname, results): path = Path(experiment) path.mkdir(parents=True, exist_ok=True) out = path / fname with open(out, "w") as outfile: json.dump(results, outfile) def evaluate_dataset_repeated( name, X_raw, y, ref_methods=[ "mean", "sklearn_ice", "sklearn_missforest", "softimpute", "gain", "miwae", "sinkhorn", ], scenarios=["MAR", "MCAR", "MNAR"], miss_pct=[0.1, 0.3, 0.5, 0.7], n_iter=10, ): results = compare_models( name=name, evaluated_model=get_imputer(), X_raw=X_raw, ref_methods=ref_methods, scenarios=scenarios, miss_pct=miss_pct, n_iter=n_iter, ) save_results(name, results) ``` -------------------------------- ### Instantiate and Use ICE Imputer Plugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_ice.md Instantiate the ICE imputer plugin and apply it to a dataset with missing values. This example shows how to get the plugin and then fit and transform a NumPy array containing NaNs. ```python >>> import numpy as np >>> from hyperimpute.plugins.imputers import Imputers >>> plugin = Imputers().get("ice") >>> plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Instantiate and Use KNeighborsClassifierPlugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_kneighbors.md Instantiate the KNeighborsClassifierPlugin and use it to fit and predict probabilities on a dataset. This example demonstrates the basic usage flow. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="classifiers").get("kneighbors") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### Instantiate and Use Linear Regression Plugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_linear_regression.md Demonstrates how to get the linear regression plugin and use it to fit and predict on a dataset. This is useful for performing regression tasks. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("linear_regression") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### Setup for Imputation Benchmarking Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_04_gain_of_function.ipynb Imports necessary libraries and initializes the Imputers class for benchmarking imputation algorithms. Ensures reproducible results by enabling a fixed random seed. ```python import sys from benchmark_imputation import simulate_scenarios from hyperimpute.plugins.imputers import Imputers import warnings import pandas as pd import hyperimpute.logger as log from hyperimpute.utils.metrics import print_score, generate_score from hyperimpute.utils.distributions import enable_reproducible_results enable_reproducible_results() imputers = Imputers() ``` -------------------------------- ### Instantiate and Use MeanImputer Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_mean.md Instantiate the MeanImputer plugin and apply it to a dataset with missing values. This example demonstrates how to load the plugin and use its fit_transform method. ```python >>> import numpy as np >>> from hyperimpute.plugins.imputers import Imputers >>> plugin = Imputers().get("mean") >>> plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Import Libraries and Initialize Hyperimpute Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_01_hyperimpute_with_naive_search.ipynb Imports necessary libraries and initializes hyperimpute with a specific logger level. This setup is required before running any imputation tasks. ```python import copy import os import time import random import sys import warnings import numpy as np import pandas as pd from typing import Any from hyperimpute.plugins.imputers import Imputers from hyperimpute.utils.distributions import enable_reproducible_results from hyperimpute.utils.benchmarks import compare_models import hyperimpute.logger as log from IPython.display import HTML, display import tabulate import json warnings.filterwarnings("ignore") enable_reproducible_results() imputers = Imputers() log.add(sink=sys.stderr, level="INFO") ``` -------------------------------- ### Instantiate and Use RandomForestPlugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_random_forest.md Instantiate the RandomForestPlugin and use it to fit and predict on a dataset. This example demonstrates the basic usage flow for classification tasks. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="classifiers").get("random_forest") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) ``` -------------------------------- ### Instantiate and Use SoftImpute Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_softimpute.md Instantiate the SoftImpute imputer plugin and apply it to a dataset with missing values. This example demonstrates the basic usage for imputation. ```python import numpy as np from hyperimpute.plugins.imputers import Imputers plugin = Imputers().get("softimpute") plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Instantiate and Use CatBoost Classifier Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_catboost.md Demonstrates how to get the CatBoost classifier plugin and use it to fit and predict probabilities on a dataset. This is useful for classification tasks where CatBoost's handling of categorical features is beneficial. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="classifiers").get("catboost") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_04_gain_of_function.ipynb Imports libraries for metrics, plotting, data manipulation, and parallel processing. Ensure these are installed before running. ```python from hyperimpute.plugins.utils.metrics import RMSE from benchmark_imputation import ws_score import matplotlib.pyplot as plt import seaborn as sns from pathlib import Path from sklearn.preprocessing import LabelEncoder from pathlib import Path from joblib import Parallel, delayed ``` -------------------------------- ### Import necessary libraries and configure HyperImpute Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_01_hyperimpute_with_bayesian_optimization.ipynb Imports essential libraries and configures HyperImpute for reproducible results and logging. This setup is required before running any imputation tasks. ```python import copy import os import time import random import sys import warnings import numpy as np import pandas as pd from typing import Any from hyperimpute.plugins.imputers import Imputers from hyperimpute.utils.distributions import enable_reproducible_results from hyperimpute.utils.benchmarks import compare_models import hyperimpute.logger as log from IPython.display import HTML, display import tabulate import json warnings.filterwarnings("ignore") enable_reproducible_results() ``` ```python imputers = Imputers() log.add(sink=sys.stderr, level="INFO") ``` -------------------------------- ### Instantiate and Use MIWAE Imputer Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_miwae.md Demonstrates how to get the MIWAE imputer plugin and use it to fit and transform data with missing values. Requires numpy for NaN values. ```python import numpy as np from hyperimpute.plugins.imputers import Imputers plugin = Imputers().get("miwae") plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Instantiate and Use SVCPlugin for Regression Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_svc.md Demonstrates how to instantiate the SVCPlugin for regression tasks and use it to fit and predict on a dataset. This example requires importing the Predictions class and a dataset like Iris. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("svm") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### Instantiate and Use CatBoostRegressorPlugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_catboost_regressor.md Instantiate the CatBoostRegressorPlugin for regression tasks and use it to fit and predict on data. This example demonstrates loading the plugin and applying it to the Iris dataset. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("catboost_regressor") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### Instantiate and Use Median Imputer Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_median.md Demonstrates how to get the median imputer plugin and use it to fill missing values in a dataset. Ensure numpy is imported for NaN values. ```python import numpy as np from hyperimpute.plugins.imputers import Imputers plugin = Imputers().get("median") plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Instantiate and Use RandomForestRegressor Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_random_forest_regressor.md Instantiate the RandomForestRegressor plugin and use it to fit and predict on a dataset. This example demonstrates the basic usage flow for regression tasks. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("random_forest") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) ``` -------------------------------- ### Instantiate and Use HyperImpute Plugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_hyperimpute.md Instantiate the HyperImpute plugin and use it to fit and transform data with missing values. This example demonstrates a common use case for the plugin. ```python >>> import numpy as np >>> from hyperimpute.plugins.imputers import Imputers >>> plugin = Imputers().get("hyperimpute") >>> plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Instantiate and Use XGBoost Regressor Plugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_xgboost_regressor.md Instantiate the XGBoost Regressor plugin and use it to fit and predict on a dataset. This example demonstrates the basic workflow for using the plugin. ```python from hyperimpute.plugins.prediction import Predictions plugin = Predictions(category="regressors").get("xgboost") from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) plugin.fit_predict(X, y) ``` -------------------------------- ### NeuralNetsRegressionPlugin Initialization and Usage Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_neural_nets_regression.md This snippet demonstrates how to instantiate the NeuralNetsRegressionPlugin and use it for fitting and predicting on data. It includes an example using scikit-learn's Iris dataset. ```APIDOC ## NeuralNetsRegressionPlugin ### Description Regression plugin based on Neural networks. ### Parameters * **n_layers_hidden** (*int*) - Number of hypothesis layers (n_layers_hidden x n_units_hidden + 1 x Linear layer). * **n_units_hidden** (*int*) - Number of hidden units in each hypothesis layer. * **nonlin** (*str*, optional) - Nonlinearity to use in NN. Can be ‘elu’, ‘relu’, ‘selu’ or ‘leaky_relu’. Defaults to 'relu'. * **lr** (*float*, optional) - learning rate for optimizer. step_size equivalent in the JAX version. Defaults to 0.001. * **weight_decay** (*float*, optional) - l2 (ridge) penalty for the weights. Defaults to 0.001. * **n_iter** (*int*, optional) - Maximum number of iterations. Defaults to 1000. * **batch_size** (*int*, optional) - Batch size. Defaults to 512. * **n_iter_print** (*int*, optional) - Number of iterations after which to print updates and check the validation loss. Defaults to 10. * **patience** (*int*, optional) - Number of iterations to wait before early stopping after decrease in validation loss. Defaults to 10. * **n_iter_min** (*int*, optional) - Minimum number of iterations to go through before starting early stopping. Defaults to 100. * **dropout** (*float*, optional) - Dropout rate. Defaults to 0.1. * **clipping_value** (*int*, optional) - Gradients clipping value. Defaults to 1. * **batch_norm** (*bool*, optional) - Whether to use batch normalization. Defaults to True. * **early_stopping** (*bool*, optional) - Whether to use early stopping. Defaults to True. * **hyperparam_search_iterations** (*int | None*, optional) - Number of iterations for hyperparameter search. Defaults to None. * **random_state** (*int*, optional) - Random state for reproducibility. Defaults to 0. * **kwargs** - Additional keyword arguments. ### Example ```pycon >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("neural_nets_regression") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` ### Methods #### fit_predict(X: DataFrame, y: DataFrame) -> DataFrame Fits the model to the data and predicts the output. #### static hyperparameter_space() -> List[Params] Returns the hyperparameter space for the plugin. #### static name() -> str Returns the name of the plugin. ``` -------------------------------- ### Instantiate and Use Neural Nets Regression Plugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_neural_nets_regression.md Demonstrates how to get the neural nets regression plugin and use it to fit and predict on a dataset. This is useful for regression tasks where a neural network model is desired. ```python from hyperimpute.plugins.prediction import Predictions plugin = Predictions(category="regression").get("neural_nets_regression") from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### Instantiate and Use Neural Nets Classifier Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_neural_nets.md Demonstrates how to get the neural_nets classifier plugin and use its fit_predict method with the Iris dataset. This returns class probabilities. ```python from hyperimpute.plugins.prediction import Predictions plugin = Predictions(category="classifiers").get("neural_nets") from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### Instantiate and Use EM Imputer Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_EM.md Instantiate the EM imputer plugin and use it to fit and transform a dataset with missing values. This example demonstrates the basic usage for imputation. ```python import numpy as np from hyperimpute.plugins.imputers import Imputers plugin = Imputers().get("EM") plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Instantiate and Use MLP Regressor Plugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_mlp_regressor.md Instantiate the MLP Regressor plugin and use it to fit and predict on a dataset. This example demonstrates how to load data and apply the plugin for regression. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("mlp_regressor") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### Import Simulation and Imputation Tools Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_03_insights_hyperimpute_convergence_std.ipynb Imports necessary libraries for simulating imputation scenarios and utilizing HyperImpute's imputation plugins. This setup is required before performing imputation tasks. ```python from benchmark_imputation import simulate_scenarios from hyperimpute.plugins.imputers import Imputers import warnings import pandas as pd import hyperimpute.logger as log ``` -------------------------------- ### Instantiate and Use SKLearn ICE Imputer Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_sklearn_ice.md Instantiate the ICE imputer plugin and apply it to a dataset with missing values. This example demonstrates the basic usage for imputation. ```python import numpy as np from hyperimpute.plugins.imputers import Imputers plugin = Imputers().get("ice") plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Instantiate and Use MicePlugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_mice.md Instantiate the MicePlugin and use it to impute missing values in a dataset. This example demonstrates basic usage with a small numpy array containing NaNs. ```python import numpy as np from hyperimpute.plugins.imputers import Imputers plugin = Imputers().get("mice") plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Instantiate and Use Sinkhorn Imputer Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_sinkhorn.md Demonstrates how to get the Sinkhorn imputer plugin and apply it to data with missing values. This is useful for imputing quantitative data using the Sinkhorn strategy. ```python import numpy as np from hyperimpute.plugins.imputers import Imputers plugin = Imputers().get("sinkhorn") plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Creating a Custom Median Imputer Plugin Source: https://context7.com/vanderschaarlab/hyperimpute/llms.txt Shows how to create a custom imputation plugin by subclassing `ImputerPlugin`. This example implements a median imputer using scikit-learn's `SimpleImputer` and registers it with the `Imputers` registry. ```python import pandas as pd import numpy as np from typing import Any, List from sklearn.impute import SimpleImputer import hyperimpute.plugins.core.params as params from hyperimpute.plugins.imputers import ImputerPlugin, Imputers class MedianImputerCustom(ImputerPlugin): """Custom median imputer demonstrating the plugin interface.""" def __init__(self, random_state: int = 0) -> None: super().__init__(random_state=random_state) self._model = SimpleImputer(strategy="median") @staticmethod def name() -> str: return "custom_median" @staticmethod def hyperparameter_space(*args: Any, **kwargs: Any) -> List[params.Params]: return [] # No tunable hyperparameters def _fit(self, X: pd.DataFrame, *args: Any, **kwargs: Any) -> "MedianImputerCustom": self._model.fit(X) return self def _transform(self, X: pd.DataFrame) -> pd.DataFrame: return self._model.transform(X) Imputers().add("custom_median", MedianImputerCustom) plugin = Imputers().get("custom_median") X = pd.DataFrame([[1, np.nan], [np.nan, 2], [3, 4]]) print(plugin.fit_transform(X)) ``` -------------------------------- ### Configure and Run Model Benchmark Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/tutorials/tutorial_02_benchmark_models.ipynb This snippet shows how to initialize an imputer with specific optimization settings and then use it to benchmark against reference methods using simulated missing data. Ensure necessary libraries are imported. ```python from sklearn.datasets import load_diabetes from hyperimpute.plugins.imputers import Imputers from hyperimpute.utils.benchmarks import compare_models imputer = Imputers().get( "hyperimpute", # the name of the imputation method. # The rest of the kwargs are specific to the method # optimizer: str. The optimizer to use: simple, hyperband, bayesian optimizer="hyperband", # classifier_seed: list. Model search pool for categorical columns. classifier_seed=["logistic_regression", "catboost", "xgboost", "random_forest"], # regression_seed: list. Model search pool for continuous columns. regression_seed=[ "linear_regression", "catboost_regressor", "xgboost_regressor", "random_forest_regressor", ], # class_threshold: int. how many max unique items must be in the column to be is associated with categorical class_threshold=5, # imputation_order: int. 0 - ascending, 1 - descending, 2 - random imputation_order=2, # n_inner_iter: int. number of imputation iterations n_inner_iter=10, # select_model_by_column: bool. If true, select a different model for each column. Else, it reuses the model chosen for the first column. select_model_by_column=True, # select_model_by_iteration: bool. If true, selects new models for each iteration. Else, it reuses the models chosen in the first iteration. select_model_by_iteration=True, # select_lazy: bool. If false, starts the optimizer on every column unless other restrictions apply. Else, if for the current iteration there is a trend(at least to columns of the same type got the same model from the optimizer), it reuses the same model class for all the columns without starting the optimizer. select_lazy=True, # select_patience: int. How many iterations without objective function improvement to wait. select_patience=5, ) # Load baseline dataset X, _ = load_diabetes(as_frame=True, return_X_y=True) # Run benchmarks _ = compare_models( name="example", evaluated_model=imputer, X_raw=X, ref_methods=["sklearn_ice"], scenarios=["MAR"], miss_pct=[0.3, 0.5], n_iter=2, n_jobs=1, ) ``` -------------------------------- ### Initialize HyperImpute Components and Utilities Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_03_insights_hyperimpute_model_selection.ipynb Sets up the necessary components for HyperImpute experiments, including the imputer registry, simulation utilities, logging, and warning filters. Enables reproducible results. ```python from benchmark_imputation import simulate_scenarios from hyperimpute.plugins.imputers import Imputers import warnings import pandas as pd import hyperimpute.logger as log log.add(sink=sys.stderr, level="INFO") imputers = Imputers() warnings.filterwarnings("ignore") ``` -------------------------------- ### Import necessary libraries Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/tutorials/tutorial_00_imputer_plugins.ipynb Imports essential libraries for data manipulation, machine learning, and HyperImpute functionalities. Ensure these are installed in your environment. ```python import sys import warnings import time from tqdm import tqdm from math import sqrt import numpy as np import pandas as pd from sklearn.datasets import load_breast_cancer from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error from sklearn.experimental import enable_iterative_imputer from sklearn.impute import IterativeImputer from hyperimpute.plugins.utils.metrics import RMSE from hyperimpute.plugins.utils.simulate import simulate_nan import xgboost as xgb from IPython.display import HTML, display import tabulate if not sys.warnoptions: warnings.simplefilter("ignore") ``` -------------------------------- ### Initialize Imputers and set up subsample Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/tutorials/tutorial_01_bayesian_optimization_over_imputers.ipynb Loads available imputation plugins from Hyperimpute and defines a list of imputers to be used in the benchmark. A subsample size is also set. ```python from hyperimpute.plugins.imputers import Imputers, ImputerPlugin from hyperimpute.plugins.utils.metrics import RMSE from hyperimpute.plugins.utils.simulate import simulate_nan from hyperimpute.utils.optimizer import EarlyStoppingExceeded, create_study imputers = Imputers() imputers.list() imputers_seed = [ "mean", # "miracle", # "miwae", # "gain", # "softimpute", # "sinkhorn", # "sklearn_ice", # "most_frequent", # "median", # "EM", # "sklearn_missforest", ] subsample = 500 ``` -------------------------------- ### KNeighborsRegressorPlugin Initialization and Usage Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_kneighbors_regressor.md Demonstrates how to initialize the KNeighborsRegressorPlugin and use it for fitting and predicting with sample data. ```APIDOC ## KNeighborsRegressorPlugin ### Description Regression plugin based on the KNeighborsRegressor. ### Parameters * **n_neighbors** (int) - The number of neighbors to use by default for kneighbors queries. * **weights** (int) - Weight function used in prediction. Possible values are 'uniform' (all points in each neighbor have the same weight) or 'distance' (weight point in smaller distance is larger). * **algorithm** (int) - Algorithm used to compute the nearest neighbors. Possible values are 'auto', 'ball_tree', 'kd_tree', 'brute'. * **leaf_size** (int) - Leaf size passed to BallTree or KDTree. * **p** (int) - Power parameter for the Minkowski metric. * **random_state** (int) - Determines random number generation for algorithms that need it. * **hyperparam_search_iterations** (int | None) - Number of iterations for hyperparameter search. * **model** (Any) - Pre-instantiated model to use. * **kwargs** (Any) - Additional keyword arguments. ### Example ```pycon >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("kneighbors") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) ``` ### Methods #### _fit(X: DataFrame, *args: Any, **kwargs: Any) -> KNeighborsRegressorPlugin #### _predict(X: DataFrame, *args: Any, **kwargs: Any) -> DataFrame #### hyperparameter_space(*args: Any, **kwargs: Any) -> List[Params] Returns the hyperparameter space for the KNeighborsRegressorPlugin. #### set_score_request(, sample_weight: bool | None | str = '$UNCHANGED$') -> KNeighborsRegressorPlugin Configure whether metadata should be requested to be passed to the `score` method. Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with `enable_metadata_routing=True` (see `sklearn.set_config()`). Please check the User Guide on how the routing mechanism works. The options for each parameter are: - `True`: metadata is requested, and passed to `score` if provided. The request is ignored if metadata is not provided. - `False`: metadata is not requested and the meta-estimator will not pass it to `score`. - `None`: metadata is not requested, and the meta-estimator will raise an error if the user provides it. - `str`: metadata should be passed to the meta-estimator with this given alias instead of the original name. The default (`sklearn.utils.metadata_routing.UNCHANGED`) retains the existing request. This allows you to change the request for some parameters and not others. * **Parameters:** **sample_weight** (*str* *,* *True* *,* *False* *,* *or* *None* *,* *default=sklearn.utils.metadata_routing.UNCHANGED*) – Metadata routing for `sample_weight` parameter in `score`. * **Returns:** **self** – The updated object. * **Return type:** object #### name() -> str Returns the name of the plugin. ``` -------------------------------- ### Load Datasets with Pandas Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_01_hyperimpute_with_naive_search.ipynb Loads various datasets from UCI ML repository and local paths using pandas. Ensure pandas is installed and accessible. ```python import pandas as pd X_raw_breast_cancer = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data") X_raw_california = pd.read_csv("https://download.mlcc.google.com/machine-learning/demos/california_housing.csv") X_raw_diab = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/00294/diabetes.csv") X_raw_iris = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data") climate_model_samples = pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/00470/த்தல்_model.csv", skiprows=1, ) climate_model_df = pd.DataFrame(climate_model_samples) raw_datasets = { "airfoil": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/00291/airfoil_self_noise.dat", header=None, sep="\\t", ), "blood": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/blood-transfusion/transfusion.data" ), "bc": X_raw_breast_cancer, "california": X_raw_california, "climate": climate_model_df, "compression": pd.read_excel( "https://archive.ics.uci.edu/ml/machine-learning-databases/concrete/compressive/Concrete_Data.xls" ), "slump": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/concrete/slump/slump_test.data" ), "sonar": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data", header=None, ), "diabetes": X_raw_diab, "wine_red": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv", sep=";", ), "wine_white": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv", sep=";", ), "iris": X_raw_iris, "libras": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/libras/movement_libras.data", sep=",", header=None, ), "parkinsons": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/parkinsons/parkinsons.data", sep=",", ), "yacht": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/00243/yacht_hydrodynamics.data", sep="\\s+", header=None, ), "ionosphere": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/ionosphere/ionosphere.data", sep=",", header=None, ), "letter": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/letter-recognition/letter-recognition.data", header=None, ), "spam": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/spambase/spambase.data" ), "credit": pd.read_csv( "https://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.data", header=None, ), } ``` -------------------------------- ### Initialize parallel processing and output directory Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_04_gain_of_function.ipynb Sets up parallel processing with 2 jobs and defines the output directory for results. This is useful for speeding up computations. ```python dispatcher = Parallel(n_jobs=2) output_dir = Path("extras_gain_of_function_results") baseline_regressors = ["linear_regression", "random_forest_regressor"] ``` -------------------------------- ### Instantiate and Use XGBoostPlugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_xgboost.md Demonstrates how to instantiate the XGBoostPlugin for classification tasks and use it to fit and predict on a dataset. Requires importing the Predictions class and a dataset like Iris from scikit-learn. ```python from hyperimpute.plugins.prediction import Predictions plugin = Predictions(category="classifiers").get("xgboost") from sklearn.datasets import load_iris X, y = load_iris(return_X_y=True) plugin.fit_predict(X, y) ``` -------------------------------- ### Partial Model Count Generation Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_03_insights_hyperimpute_model_selection.ipynb This snippet shows the beginning of the `generate_normalized_model_count` function, specifically the initialization of `model_tracking` and the start of the nested loops to process data. ```python def generate_normalized_model_count(data, scenario, col_type): model_tracking = {} for data_set in data: for missingness in ["0.1", "0.3", "0.5", "0.7"]: trace = data[data_set][scenario][missingness] models_selected = compress_model_trace(trace["model_trace"], col_type) for model in models_selected: if not model_tracking.get(missingness, {}).get(model, {}): if not model_tracking.get(missingness, {}): model_tracking[missingness] = {model: 1} else: ``` -------------------------------- ### List Available Imputers in Hyperimpute Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/README.md Use this snippet to see all the imputation methods supported by Hyperimpute. No specific setup is required beyond importing the Imputers class. ```python from hyperimpute.plugins.imputers import Imputers imputers = Imputers() imputers.list() ``` -------------------------------- ### Prepare datasets for imputation testing Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/tutorials/tutorial_00_imputer_plugins.ipynb Sets up datasets by simulating missing values across different mechanisms and percentages. This prepares the data for performance evaluation of imputation plugins. ```python datasets = {} headers = ["Plugin"] pct = 0.3 mechanisms = ["MAR", "MNAR", "MCAR"] percentages = [pct] plugins = ["mean"] # imputers.list() # default plugins X_train, X_test, y_train, y_test = dataset() for ampute_mechanism in mechanisms: for p_miss in percentages: if ampute_mechanism not in datasets: datasets[ampute_mechanism] = {} headers.append(ampute_mechanism + "-" + str(p_miss)) datasets[ampute_mechanism][p_miss] = ampute(X_train, ampute_mechanism, p_miss) ``` -------------------------------- ### Impute Dataset with a Specific Method Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/README.md Apply a chosen imputation method (e.g., 'gain') to a pandas DataFrame containing missing values. Ensure pandas and numpy are installed. ```python import pandas as pd import numpy as np from hyperimpute.plugins.imputers import Imputers X = pd.DataFrame([[1, 1, 1, 1], [4, 5, np.nan, np.nan], [3, 3, 9, 9], [2, 2, 2, 2]]) method = "gain" plugin = Imputers().get(method) out = plugin.fit_transform(X.copy()) print(method, out) ``` -------------------------------- ### Instantiate and Use MIRACLE Imputer Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_miracle.md Demonstrates how to instantiate the MIRACLE imputer plugin and apply it to a dataset with missing values. Ensure numpy is imported for NaN values. ```python import numpy as np from hyperimpute.plugins.imputers import Imputers plugin = Imputers().get("miracle") plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### Define imputer and result saving functions Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/experiments/experiments_01_hyperimpute_with_bayesian_optimization.ipynb Defines functions to get a Bayesian-optimized imputer and to save evaluation results to a JSON file. These are utility functions for the benchmarking process. ```python from pathlib import Path def get_imputer(): return imputers.get("hyperimpute", optimizer="bayesian") def save_results(fname, results): path = Path("experiments_01_hyperimpute_with_bayesian_optimization") path.mkdir(parents=True, exist_ok=True) out = path / fname with open(out, "w") as outfile: json.dump(results, outfile) ``` -------------------------------- ### Instantiate and Use MostFrequentPlugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.imputers.plugin_most_frequent.md Demonstrates how to get the 'most_frequent' imputer plugin and apply it to a dataset with missing values using fit_transform. Ensure numpy is imported for NaN values. ```python >>> import numpy as np >>> from hyperimpute.plugins.imputers import Imputers >>> plugin = Imputers().get("most_frequent") >>> plugin.fit_transform([[1, 1, 1, 1], [np.nan, np.nan, np.nan, np.nan], [1, 2, 2, 1], [2, 2, 2, 2]]) ``` -------------------------------- ### CatBoostRegressorPlugin Initialization Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.regression.plugin_catboost_regressor.md Initializes the CatBoostRegressorPlugin with various parameters to configure the CatBoost model for regression tasks. ```APIDOC ## CatBoostRegressorPlugin ### Description Regression plugin based on the CatBoost framework. CatBoost provides a gradient boosting framework which attempts to solve for Categorical features using a permutation driven alternative compared to the classical algorithm. It uses Ordered Boosting to overcome over fitting and Symmetric Trees for faster execution. ### Parameters - **depth** (int | None, optional) - The maximum depth of the tree. - **grow_policy** (int, optional) - The tree growing policy. Defaults to 0. - **n_estimators** (int | None, optional) - The number of boosting rounds or trees to build. Defaults to 10. - **hyperparam_search_iterations** (int | None, optional) - Number of iterations for hyperparameter search. - **random_state** (int, optional) - Controls the random number generator. Defaults to 0. - **l2_leaf_reg** (float, optional) - L2 regularization term for leaf weights. Defaults to 3. - **learning_rate** (float, optional) - Boosting learning rate. Defaults to 0.001. - **min_data_in_leaf** (int, optional) - Minimum number of data points in a leaf. Defaults to 1. - **random_strength** (float, optional) - Controls the randomness of splitting. Defaults to 1. - **kwargs** (Any) - Additional keyword arguments. ### Example ```pycon >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="regression").get("catboost_regressor") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` ``` -------------------------------- ### Instantiate and Use Logistic Regression Plugin Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_logistic_regression.md Demonstrates how to instantiate the LogisticRegressionPlugin and use it to fit data and predict probabilities using the Iris dataset. This snippet shows the basic workflow for applying the classifier. ```python >>> from hyperimpute.plugins.prediction import Predictions >>> plugin = Predictions(category="classifiers").get("logistic_regression") >>> from sklearn.datasets import load_iris >>> X, y = load_iris(return_X_y=True) >>> plugin.fit_predict(X, y) # returns the probabilities for each class ``` -------------------------------- ### ClassifierPlugin.get_args Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.md Retrieves the arguments used to initialize the classifier plugin. ```APIDOC ## get_args() -> dict ### Description Retrieves the arguments used to initialize the classifier plugin. ### Returns * **dict** – A dictionary containing the initialization arguments. ``` -------------------------------- ### KNeighborsClassifierPlugin Initialization Source: https://github.com/vanderschaarlab/hyperimpute/blob/main/docs/generated/hyperimpute.plugins.prediction.classifiers.plugin_kneighbors.md Instantiate the KNeighborsClassifierPlugin with various configuration options. ```APIDOC ## KNeighborsClassifierPlugin ### Description Classification plugin based on the KNeighborsClassifier classifier. ### Parameters - **n_neighbors** (int) - The number of neighbors to use by default for kneighbors queries. - **weights** (int) - Weight function used in prediction. Possible values are \'uniform\' (all points in each neighborhood are weighted equally) and \'distance\' (each point's weight is the inverse of its distance from the query point). - **algorithm** (int) - Algorithm used to compute the nearest neighbors. Possible values are \'auto\', \'ball_tree\', \'kd_tree\', or \'brute\'. - **leaf_size** (int) - Leaf size passed to BallTree or KDTree. - **p** (int) - Power parameter for the Minkowski metric. - **random_state** (int) - Determines random number generation for dataset creation. Pass an int for reproducible output across multiple function calls. - **hyperparam_search_iterations** (int | None) - Number of iterations for hyperparameter search. - **model** (Any) - Pre-trained model to use. - **kwargs** (Any) - Additional keyword arguments. ```