### Install smote_variants directly from GitHub URL Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/installation.rst Installs the smote_variants package directly from its GitHub repository URL using pip. ```bash pip install git+https://github.com:gykovacs/smote_variants.git ``` -------------------------------- ### Install smote_variants by cloning from GitHub Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/installation.rst Clones the smote_variants repository from GitHub, navigates into the directory, and installs the package in editable mode using pip. ```bash git clone git@github.com:gykovacs/smote_variants.git cd smote_variants pip install . ``` -------------------------------- ### Install imbalanced_databases by cloning from GitHub Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/installation.rst Clones the imbalanced_databases repository from GitHub, navigates into the directory, and installs the package using pip. This package is optional but useful for evaluation. ```bash git clone git@github.com:gykovaded/imbalanced_databases.git cd imbalanced_databases pip install . ``` -------------------------------- ### Install smote_variants via pip (PyPi) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/installation.rst Installs the smote_variants package from the Python Package Index (PyPi) using the pip package manager. ```bash pip install smote_variants ``` -------------------------------- ### Install imbalanced_databases directly from GitHub URL Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/installation.rst Installs the optional imbalanced_databases package directly from its GitHub repository URL using pip. This is an alternative method for obtaining the evaluation package. ```bash pip install git+https://github.com:gykovacs/imbalanced_databases.git ``` -------------------------------- ### Starting Dataset Iteration Loop - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/031-deterministic-test-joblib.ipynb Initializes a loop to process each dataset loaded previously, preparing to store results for the current dataset. ```python for data_loader in datasets: results = [] ``` -------------------------------- ### Install imbalanced_databases via pip (PyPi) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/installation.rst Installs the optional imbalanced_databases package from PyPi using pip. This package is recommended for testing and evaluation purposes. ```bash pip install imbalanced_databases ``` -------------------------------- ### Example Call to do_job (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/031-deterministic-test-joblib.ipynb Demonstrates how to call the do_job function with a single job and description pair obtained from the job_generator, likely used for testing or debugging a single job execution. ```python do_job(*next(job_generator(datasets[0]))) ``` -------------------------------- ### Citing smote-variants Package (BibTex) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/getting_started.rst BibTex entry for citing the smote-variants package publication. This entry includes details about the author, title, journal, volume, pages, year, and relevant links. ```BibTex @article{smote-variants, author={Gy"orgy Kov'acs}, title={smote-variants: a Python Implementation of 85 Minority Oversampling Techniques}, journal={Neurocomputing}, note={(IF-2019=4.07)}, volume={366}, pages={352--354}, link={https://www.sciencedirect.com/science/article/pii/S0925231219311622}, year={2019}, group={journal}, code= {https://github.com/analyticalmindsltd/smote_variants}, doi={10.1016/j.neucom.2019.06.100} } ``` -------------------------------- ### Initializing and Fitting ITML_Supervised Model (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/028_metric_learn_bug.ipynb This snippet demonstrates how to import numpy and metric_learn, print the numpy version, and initialize and fit an ITML_Supervised model from the metric_learn library using randomly generated data and labels. It serves as a basic example of model usage. ```python import numpy as np import metric_learn as ml print(np.__version__) ml.ITML_Supervised().fit(np.random.random_sample(size=(100, 5)), np.random.randint(2, size=100)) ``` -------------------------------- ### Importing Libraries for Data and Classifier (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/027_mlpwrapper.ipynb Imports necessary functions for loading binary classification datasets (`load_appendicitis`, `load_spectf`, `load_spectfheart`) from `common_datasets.binary_classification` and the `MLPClassifierWrapper` class from `smote_variants.classifiers`. These imports are prerequisites for the subsequent code examples. ```python from common_datasets.binary_classification import load_appendicitis, load_spectf, load_spectfheart from smote_variants.classifiers import MLPClassifierWrapper ``` -------------------------------- ### Installing smote-variants Package Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/examples/009_oversampling_LLE_SMOTE_images.ipynb Uses the pip package manager command (prefixed with `!`, common in notebooks) to install the `smote-variants` library from PyPI, which is required to perform the LLE-SMOTE oversampling. ```python !pip install smote-variants ``` -------------------------------- ### SMOTE_RSB Example Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Illustrates the basic steps to use the SMOTE_RSB oversampling method provided by smote_variants. The example initializes the oversampler and then calls the sample method with input data X and y to obtain the oversampled data X_samp and y_samp. ```Python oversampler= smote_variants.SMOTE_RSB() X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Citing smote-variants Comparison Study (BibTex) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/getting_started.rst BibTex entry for citing the comparative study evaluating minority oversampling techniques. This entry provides citation details for the research published in Applied Soft Computing. ```BibTex @article{smote-comparison, author={Gy"orgy Kov'acs}, title={An empirical comparison and evaluation of minority oversampling techniques on a large number of imbalanced datasets}, journal={Applied Soft Computing}, note={(IF-2019=4.873)}, year={2019}, volume={83}, pages={105662}, link={https://www.sciencedirect.com/science/article/pii/S1568494619304429}, group={journal}, code={https://github.com/analyticalmindsltd/smote_variants}, doi={10.1016/j.asoc.2019.105662} } ``` -------------------------------- ### Starting Julia Interpreter Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/use_in_julia.rst Command to start the Julia interactive interpreter. This is the initial step before executing Julia code. ```Julia julia ``` -------------------------------- ### Starting Iteration Over Loaded Datasets in Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/031-deterministic-test-joblib-ml.ipynb Initiates a loop to iterate through each data loader object present in the `datasets` collection. Inside the loop, an empty list named `results` is created, likely intended to accumulate experiment outcomes for the current dataset. ```python for data_loader in datasets: results = [] ``` -------------------------------- ### Example Conversion to DataFrame (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/031-deterministic-test-joblib.ipynb Shows how to convert a dictionary of results into a pandas DataFrame using pd.DataFrame.from_dict, potentially for inspection or further processing before saving. ```python tmp = pd.DataFrame.from_dict(results) ``` -------------------------------- ### Getting Parameters from Instantiated SMOTE_TomekLinks Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Instantiates SMOTE_TomekLinks with the randomly selected parameters and then retrieves the parameters using the get_params method, confirming the parameters were set correctly. ```python params2 = sv.SMOTE_TomekLinks(**params).get_params() ``` -------------------------------- ### Initializing Environment and Logging (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Imports necessary libraries (logging, json, pandas, smote_variants, common_datasets), configures smote_variants warnings, and sets up a critical-level logger for the 'smote_variants' module. ```python import logging import json import pandas as pd from smote_variants import get_simplex_sampling_oversamplers from common_datasets.binary_classification import get_filtered_data_loaders from smote_variants.evaluation import evaluate_oversamplers import smote_variants smote_variants.config.suppress_external_warnings(False) smote_variants.config.suppress_internal_warnings(False) logger = logging.getLogger('smote_variants') logger.setLevel(logging.CRITICAL) ``` -------------------------------- ### Checking Dependency Versions (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/028_metric_learn_bug.ipynb This snippet imports and prints the versions of key libraries used in the project, including platform, sys, numpy, scipy, scikit-learn, and metric-learn. It is useful for verifying the development environment setup. ```python import platform; print(platform.platform()) import sys; print("Python", sys.version) import numpy; print("NumPy", numpy.__version__) import scipy; print("SciPy", scipy.__version__) import sklearn; print("Scikit-Learn", sklearn.__version__) import metric_learn; print("Metric-Learn", metric_learn.__version__) ``` -------------------------------- ### Oversample with Random Parameter Combination (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/examples.rst Demonstrates how to use a random, reasonable parameter combination for an oversampler. Loads a dataset, gets parameter combinations from `SMOTE_Cosine`, selects one randomly, instantiates the oversampler with it, and applies it. ```Python import numpy as np import smote_variants as sv import imbalanced datasets as imbd dataset= imbd.load_yeast1() par_combs= SMOTE_Cosine.parameter_combinations() oversampler= SMOTE_Cosine(**np.random.choice(par_combs)) X_samp, y_samp= oversampler.sample(dataset['data'], dataset['target']) ``` -------------------------------- ### Executing Single Evaluation Job Example - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/031-deterministic-test-joblib-ml.ipynb This line demonstrates how to execute a single job generated by `job_generator`. It retrieves the first job and description pair using `next()` on the generator for the first dataset (`datasets[0]`) and unpacks them using `*` as arguments to the `do_job` function. Useful for testing or debugging a single job execution. ```python do_job(*next(job_generator(datasets[0]))) ``` -------------------------------- ### Importing Core Libraries for smote_variants Examples Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Imports necessary libraries including numpy for array manipulation, logging and pytest (likely for testing context), and the smote_variants library itself, aliased as sv. ```python import numpy as np import logging import pytest import smote_variants as sv ``` -------------------------------- ### Getting Parameter Combinations for SMOTE_TomekLinks Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Retrieves a list of valid parameter dictionaries for the SMOTE_TomekLinks class using the parameter_combinations class method, useful for hyperparameter tuning. ```python parameters = sv.SMOTE_TomekLinks.parameter_combinations() ``` -------------------------------- ### Load Example Multiclass Dataset Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/examples/001_multiclass_oversampling.ipynb Loads an example 3-class dataset provided by the `smote_variants` library, assigning the feature data to `X` and the target labels to `y`. ```python # loading the dataset dataset= load_illustration_3_class() X, y= dataset['data'], dataset['target'] ``` -------------------------------- ### Checking for Parameter Existence (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/010_functions.ipynb Uses the `inspect` module to get the signature of the `SUNDO` class constructor (`inspect.signature(SUNDO)`), retrieves the parameter names (`.parameters.keys()`), converts them to a list, and checks if the string 'proportion' is present in that list. ```python 'proportion' in list(inspect.signature(SUNDO).parameters.keys()) ``` -------------------------------- ### Install smote-variants via pip Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/README.rst Command to install the latest stable version of the smote-variants package from the PyPI repository using pip. ```bash pip install smote-variants ``` -------------------------------- ### Sampling with Supervised_SMOTE Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Provides an example of instantiating the Supervised_SMOTE oversampler and executing the sampling process on the provided data X and labels y. ```Python >>> oversampler= smote_variants.Supervised_SMOTE() >>> X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Importing Libraries for Evaluation Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/010_functions.ipynb Imports necessary libraries: pandas for data manipulation, sklearn.datasets for loading standard datasets, and specific evaluation and model selection functions from the smote_variants library. ```python import pandas as pd from sklearn import datasets from smote_variants.evaluation import evaluate_oversamplers, model_selection ``` -------------------------------- ### Printing Parameters from Instantiated Object Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Prints the dictionary of parameters retrieved from the instantiated SMOTE_TomekLinks object using get_params. ```python params2 ``` -------------------------------- ### Sampling with SMOTE_TomekLinks Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Shows a direct call to the sample method of the SMOTE_TomekLinks class from smote_variants using the previously defined sample data. This executes the combined SMOTE and Tomek Links algorithm. ```python sv.SMOTE_TomekLinks().sample(X_1_min_some_maj, y_1_min_some_maj) ``` -------------------------------- ### Evaluate Oversamplers Across Multiple Datasets (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/examples.rst Shows how to use the `evaluate_oversamplers` function for comparing oversampling techniques on multiple datasets. Defines a list of datasets and retrieves a set of oversamplers for evaluation. ```Python import smote_variants as sv import imbalanced_datasets as imbd datasets= [imbd.load_glass2, imbd.load_ecoli4] oversamplers = sv.get_all_oversamplers(n_quickest=5) ``` -------------------------------- ### Perform Simple Oversampling with SMOTE_ENN (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/examples.rst Demonstrates basic oversampling using the `smote_variants` package. Imports `SMOTE_ENN`, instantiates it, and calls the `sample` method on input data `X` and target `y` to generate synthetic samples. ```Python import smote_variants as sv oversampler= sv.SMOTE_ENN() # supposing that X and y contain some the feature and target data of some dataset X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Importing instantiate_obj Utility Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Imports the instantiate_obj utility function from smote_variants, which is used to dynamically create instances of objects given their module, class name, and parameters. ```python from smote_variants import instantiate_obj ``` -------------------------------- ### Loading Illustration Dataset (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/illustration/016_visualization.ipynb Loads the example dataset X_illustration and y_illustration from the smote_variants.datasets module. This dataset is used to demonstrate the imbalanced nature and the effect of oversampling. ```python X, y= sv.datasets.X_illustration, sv.datasets.y_illustration ``` -------------------------------- ### Importing Libraries for Experiment Setup Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/034-test-regularization.ipynb Imports necessary libraries for data manipulation (numpy, pandas), parallel processing (joblib), progress tracking (tqdm), machine learning models and metrics (sklearn), and custom oversampling methods and dataset loaders (smote_variants, common_datasets). These imports provide the foundational components for the experiment. ```python import datetime from joblib import Parallel, delayed import numpy as np import pandas as pd import tqdm from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.preprocessing import StandardScaler from sklearn.metrics import roc_auc_score from smote_variants.oversampling import SMOTE, NoSMOTE, ADASYN, Borderline_SMOTE1, ProWSyn, SMOTE_IPF, Lee, SMOBD from common_datasets.binary_classification import get_filtered_data_loaders import common_datasets.binary_classification as binclas ``` -------------------------------- ### Print Oversampler and Dataset Counts - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb This snippet prints the number of oversampler configurations defined and the number of datasets available for evaluation. ```python print(len(all_oversamplers)) print(len(datasets)) ``` -------------------------------- ### Instantiating MulticlassOversampling (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/010_functions.ipynb Creates an instance of the `MulticlassOversampling` class, initializing it with the string 'SUNDO', which likely specifies the underlying oversampling strategy to be used. ```python mo = MulticlassOversampling('SUNDO') ``` -------------------------------- ### Using CondensedNearestNeighbors in Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/noise_filters.rst This example shows how to initialize the CondensedNearestNeighbors noise filter and use its remove_noise method to clean a dataset (X, y). This is a standard way to apply prototype selection techniques from the library. ```Python noise_filter= smote_variants.noise_removal.CondensedNearestNeighbors() X_samp, y_samp= noise_filter.remove_noise(X, y) ``` -------------------------------- ### Perform Multiclass Oversampling (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/examples.rst Shows how to perform multiclass oversampling using `smote_variants.MulticlassOversampling`. Loads the wine dataset, instantiates `MulticlassOversampling` with a specified base oversampler, and applies it to balance the dataset. ```Python import smote_variants as sv import sklearn.datasets as datasets dataset= datasets.load_wine() oversampler= sv.MulticlassOversampling(oversampler='distance_SMOTE', oversampler_params={}) X_samp, y_samp= oversampler.sample(dataset['data'], dataset['target']) ``` -------------------------------- ### Selecting a Random Parameter Combination Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Selects one random dictionary of parameters from the list obtained in the previous step, demonstrating how to pick a specific configuration. ```python params = np.random.choice(parameters) ``` -------------------------------- ### Getting Parameters for AMSCO Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Instantiates the AMSCO class and calls its get_params method to retrieve its default parameters, similar to the pattern shown for SMOTE_TomekLinks. ```python AMSCO().get_params() ``` -------------------------------- ### Create Sample Pandas DataFrame - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Creates a small sample pandas DataFrame with 'name' and 'value' columns for demonstration or testing purposes. ```python tmp = pd.DataFrame({'name': ['a', 'b', 'a', 'c', 'a'], 'value': [0, 1, 2, 3, 4]}) ``` -------------------------------- ### Setting up Experiment Loop and Parameters (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/000_metric_learning.ipynb Initializes a list to store results and begins iterating through the selected datasets. Inside the loop, it loads the dataset, prints basic information, and then iterates through cross-validation splits. It also defines parameter dictionaries for nearest neighbor calculations and oversampling, although the core classification and evaluation logic within the inner loops is not fully present in this snippet. ```python results= [] for j, dataset in enumerate(datasets): """ X, y= make_classification(n_samples=100, n_features=4, n_informative=2, n_redundant=1, n_repeated=0, n_clusters_per_class=2, weights=np.array([0.8, 0.2]), random_state=j) ds= {'name': str(j)} """ results_dataset= [] ds= dataset() X, y= ds['data'], ds['target'] print(j, ds['name'], len(X), len(np.unique(X, axis=0)), len(X[y == 1])) for i, (train, test) in enumerate(validator.split(X, y)): X_train, y_train = X[train], y[train] X_test, y_test = X[test], y[test] nn_params_0= {'metric': 'precomputed', 'metric_learning_method': 'id', 'algorithm': 'brute'} nn_params_1= {'metric': 'precomputed', 'metric_learning_method': 'gmean', 'algorithm': 'brute'} #metric_tensor= sv.MetricTensor(**nn_params_1).tensor(X_train, y_train) #nn_params_1['metric_tensor']= metric_tensor oversampler_params_0= {'random_state': 5, 'nn_params': nn_params_0, 'sampling_params': {'simplex_sampling': 'uniform', 'within_simplex_sampling': 'random', 'n_dim': 2}, 'n_neighbors': 5, 'proportion': 1.0} oversampler_params_1= {'random_state': 5, 'nn_params': nn_params_0, 'n_neighbors': 5, 'sampling_params': {'simplex_sampling': 'uniform', ``` -------------------------------- ### Modify NumPy Array Elements - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Modifies elements of the sample NumPy array using slicing, subtracting 1 from every second element starting from the first. ```python tmp[::2] = tmp[::2] - 1 ``` -------------------------------- ### Using NearestNeighborsWithMetricTensor for Minority Class Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Demonstrates initializing and fitting the NearestNeighborsWithMetricTensor from smote_variants to the minority class data, then finding k-nearest neighbors. This is a common step in many SMOTE variants. ```python from smote_variants import NearestNeighborsWithMetricTensor X = X_1_min_some_maj y = y_1_min_some_maj min_label = 1 n_neighbors = 5 nn_params = {} X_min = X[y == min_label] # fitting the model n_neighbors = min([len(X_min), n_neighbors+1]) nn_mt= NearestNeighborsWithMetricTensor(n_neighbors=n_neighbors, n_jobs=1, **nn_params) nn_mt.fit(X_min) _, ind_min = nn_mt.kneighbors(X_min, return_distance=True) ``` -------------------------------- ### Defining Parameter Dictionaries (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Sets up various dictionaries containing parameters for cross-validation (`validator_params`), simplex sampling (`ss_params`), vanilla oversampling (`vanilla_params`), and deterministic oversampling (`deterministic_params`). ```python validator_params = {'n_repeats': 1, 'n_splits': 2, 'random_state': 5} ss_params = {'within_simplex_sampling': 'deterministic', 'simplex_sampling': 'deterministic'} vanilla_params = {'random_state': 5, 'n_jobs': 1} deterministic_params = {'random_state': 5, 'ss_params': ss_params, 'n_jobs': 1} ``` -------------------------------- ### TRIM_SMOTE Example Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Shows the basic usage of the TRIM_SMOTE oversampler from smote_variants. It demonstrates how to create an instance of the oversampler and use its sample method on input data X and y to produce synthetic samples X_samp and y_samp. ```Python oversampler= smote_variants.TRIM_SMOTE() X_samp, y_samp= overampler.sample(X, y) ``` -------------------------------- ### Defining Oversampling Method and Parameters Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/009_evaluation.ipynb Specifies the oversampling method to be used as 'SMOTE' and initializes an empty dictionary for its parameters, indicating default parameters will be used. ```python oversampler = 'SMOTE' oversampler_params = {} ``` -------------------------------- ### Generate Oversampler Parameter Combinations - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/examples.rst This line generates parameter combinations for the specified oversamplers, limiting the maximum number of combinations per oversampler to 5. This is a preparatory step for the evaluation. ```Python oversamplers = sv.generate_parameter_combinations(oversamplers, n_max_comb=5) ``` -------------------------------- ### Configuring Cross-Validation and Classifiers (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/000_metric_learning.ipynb Sets up the cross-validation strategy using RepeatedStratifiedKFold with 1000 repeats and 5 splits. It also defines a list of classifier models (Decision Tree, SVC, KNeighborsClassifier) with their respective parameters to be used in the evaluation, although some are commented out. ```python validator = RepeatedStratifiedKFold(n_repeats=1000, n_splits=5, random_state=5) classifiers= [(DecisionTreeClassifier, {'random_state': 5}), #(RandomForestClassifier, {'random_state': 5}), #(RandomForestClassifier, {'random_state': 5, 'min_samples_leaf': 15}), (SVC, {'probability': True, 'random_state': 5, 'C': 1.0}), #(KNeighborsClassifier, {'algorithm': 'brute', 'weights': 'distance'}) (KNeighborsClassifier, {'algorithm': 'brute', 'weights': 'distance', 'n_neighbors': 5}) ] ``` -------------------------------- ### Instantiate NEATER and Sample Data (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Demonstrates how to initialize the NEATER oversampler and use it to generate synthetic samples from input data X and y. ```Python >>> oversampler= smote_variants.NEATER() >>> X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Importing Libraries and Setting Up Logging - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/examples/005_speed_test.ipynb Imports required libraries for oversampling, dataset loading, plotting, timing, numerical operations, data handling, and logging. Configures the 'smote_variants' logger to the CRITICAL level to suppress verbose output. ```python import smote_variants as sv import common_datasets.binary_classification as bin_clas import matplotlib.pyplot as plt import time import numpy as np import pandas as pd import logging logger = logging.getLogger('smote_variants') logger.setLevel(logging.CRITICAL) ``` -------------------------------- ### Setup Evaluation Framework (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/002_ensemble_smote.ipynb Initializes a `RepeatedStratifiedKFold` cross-validation validator. Defines lists of classifiers (Decision Tree, SVC, KNeighborsClassifier) with their parameters and base oversamplers (SMOTE, Borderline-SMOTE1/2, ADASYN). Starts a loop to iterate through loaded datasets, preparing for an evaluation process. ```python validator = RepeatedStratifiedKFold(n_repeats=10, n_splits=5, random_state=5) results= [] classifiers= [(DecisionTreeClassifier, {'random_state': 5}), (SVC, {'probability': True, 'random_state': 5}), (KNeighborsClassifier, {'n_neighbors': 5})] oversamplers_base = [sv.SMOTE, sv.Borderline_SMOTE1, sv.Borderline_SMOTE2, sv.ADASYN] #nn_params_0= {} #nn_params_1= {} for j, dataset in enumerate(datasets): """ X, y= make_classification(n_samples=100, n_features=4, n_informative=2, n_redundant=1, n_repeated=0, n_clusters_per_class=2, weights=np.array([0.8, 0.2]), random_state=j) ds= {'name': str(j)} """ results_dataset= [] ds= dataset() ``` -------------------------------- ### Importing necessary libraries Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/007_folding.ipynb Imports the `Folding` class from `smote_variants` for dataset handling and `datasets` from `sklearn` to load a sample dataset. ```python from smote_variants import Folding from sklearn import datasets ``` -------------------------------- ### Setting Up Cache Directory (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/examples/002_evaluation_multiple_datasets.ipynb Defines a path for caching evaluation results, typically in the user's home directory under 'smote_test'. It checks if the directory exists and creates it if necessary to store intermediate results and speed up subsequent runs. ```python # Setting the cache_path which is used for caching during the evaluation cache_path= os.path.join(os.path.expanduser('~'), 'smote_test') if not os.path.exists(cache_path): os.makedirs(cache_path) ``` -------------------------------- ### Selecting Oversampler Parameters Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/034-test-regularization.ipynb Initializes a dictionary `oversamplers` by iterating through the `oversampler_classes`. For each oversampler, it retrieves all parameter combinations, filters for those with `proportion=1.0`, and randomly samples up to 10 unique combinations to be used in the experiment. ```python oversamplers = {} for oversampler in oversampler_classes: random_state = np.random.RandomState(5) params = oversampler.parameter_combinations() params = [comb for comb in params if comb.get('proportion', 1.0) == 1.0] n_params = min(10, len(params)) oversamplers[oversampler] = random_state.choice(params, n_params, replace=False) ``` -------------------------------- ### Sampling Simplex with SimplexSamplingMixin Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Demonstrates using the sample_simplex method from SimplexSamplingMixin, likely a utility or base class within smote_variants, using the minority class data and pre-calculated neighbor indices. ```python sv.SimplexSamplingMixin().sample_simplex(X_min, indices=ind_min, n_to_sample=6) ``` -------------------------------- ### Initializing Sample Data and Dissimilarity Matrix (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/011_metrictensor.ipynb Creates sample numpy arrays for features `X` and a pre-calculated pairwise dissimilarity matrix `dissim_matrix` to be used in subsequent demonstrations. ```python X = np.array([[1.0, 0.0], [0.0, 1.0], [1.0, 1.0]]) dissim_matrix = np.array([[0.0, np.sqrt(2.0), 1.0], [np.sqrt(2.0), 0.0, 1.0], [1.0, 1.0, 0.0]]) ``` -------------------------------- ### Checking for Finite Values with NumPy Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/010_functions.ipynb Imports the NumPy library and shows a basic example of using `np.isfinite()` to check if a value, specifically `np.nan` (Not a Number), is finite. ```python import numpy as np np.isfinite(np.nan) ``` -------------------------------- ### Instantiate and Sample with ASMOBD (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Shows the process of creating an instance of the ASMOBD oversampler and using its `sample` method to generate synthetic data (X_samp, y_samp) from the input dataset (X, y). Requires the `smote_variants` library and input data X and y. ```Python oversampler= smote_variants.ASMOBD() X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Calculating and Printing Combined Partitions (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/tmp.ipynb Initializes variables `p` and `n`. It then iterates through values of `k` from 2 up to the minimum of `p` and `n` (exclusive). For each `k`, it calculates the product of `partitions(n, k)` and `partitions(p, k)` using the previously defined function and prints `k` along with the calculated product. ```python p = 150 n = 50 for k in range(2, min(p, n)): print(k, partitions(n, k)*partitions(p,k)) ``` -------------------------------- ### Importing SMOTE Variants and Sklearn Datasets - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/008_sampling.ipynb Imports the `Folding` and `SamplingJob` classes from `smote_variants` for data splitting and oversampling tasks, and the `datasets` module from `sklearn` to load example datasets. ```python from smote_variants import Folding, SamplingJob from sklearn import datasets ``` -------------------------------- ### Instantiate and Sample with Assembled_SMOTE (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Provides an example of initializing the Assembled_SMOTE oversampler and calling the `sample` method to produce synthetic samples (X_samp, y_samp) from the original dataset (X, y). Requires the `smote_variants` library and input data X and y. ```Python oversampler= smote_variants.Assembled_SMOTE() X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Importing Libraries for SMOTE Evaluation - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/031-deterministic-test-joblib.ipynb Imports core libraries including data manipulation, machine learning models, cross-validation, scaling, metrics, parallel processing, and custom SMOTE and data loading functions for setting up an evaluation pipeline. ```python import datetime from joblib import Parallel, delayed import numpy as np import pandas as pd from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.preprocessing import StandardScaler from sklearn.metrics import roc_auc_score from smote_variants.oversampling import SMOTE from common_datasets.binary_classification import get_filtered_data_loaders ``` -------------------------------- ### Visualizing an Oversampled Image Example Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/examples/009_oversampling_LLE_SMOTE_images.ipynb Uses the `array_to_img` function from Keras preprocessing utilities to convert a specific image (at index 700) from the oversampled dataset `X_samp` from a numpy array format into a displayable image object, showing an example of a synthetically generated image. ```python #visualising oversampled images b=array_to_img(X_samp[700]) b ``` -------------------------------- ### Applying MSYN Sampling to Dataset (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/026_performance.ipynb Initializes the `MSYN` sampler from `smote_variants` and applies its `sample` method to the loaded dataset's features (`data['data']`) and target labels (`data['target']`) to generate a new dataset with synthetic samples. ```python X, y = MSYN().sample(data['data'], data['target']) ``` -------------------------------- ### Printing Selected Parameters Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Prints the dictionary of parameters randomly selected in a previous step. ```python params ``` -------------------------------- ### Initializing the Folding class Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/007_folding.ipynb Creates an instance of the `Folding` class, passing the loaded dataset. It specifies a cache path and sets `reset=True` to clear any existing cache at that location. ```python folding = Folding(dataset, cache_path='/home/gykovacs/smote_cache/', reset=True) ``` -------------------------------- ### Checking Dataset Count (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Displays the number of datasets remaining after the filtering process. ```python len(datasets) ``` -------------------------------- ### Displaying Cross-Validation Scores (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/illustration/025_oversampling_classifier.ipynb Displays the results of the cross-validation stored in the scores variable. ```python scores ``` -------------------------------- ### Instantiate and Sample with Edge_Det_SMOTE (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Shows how to initialize the Edge_Det_SMOTE oversampler and apply it to a dataset using the sample method. Assumes X and y are available input data. ```Python oversampler= smote_variants.Edge_Det_SMOTE() X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Displaying Sample NumPy Array Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/003_vector_random_neighbor.ipynb Prints the contents of the 'tmp' NumPy array to the console. ```python tmp ``` -------------------------------- ### Importing Libraries Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/032-smote-rf.ipynb Imports necessary libraries for numerical operations (numpy, pandas), statistical tests (scipy.stats), machine learning models (sklearn.ensemble, sklearn.tree), metrics (sklearn.metrics), model selection (sklearn.model_selection), preprocessing (sklearn.preprocessing), dimensionality reduction (sklearn.decomposition), custom classifier operators, SMOTE implementation, and common binary classification datasets. ```python import numpy as np import pandas as pd from scipy.stats import wilcoxon from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import roc_auc_score from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn.tree import DecisionTreeClassifier from conditioning_bias import OperatorRandomForestClassifier, OperatorDecisionTreeClassifier from smote_variants.oversampling import SMOTE import common_datasets.binary_classification as binclas ``` -------------------------------- ### Initializing and Sampling with SSO in Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Demonstrates how to initialize the SSO oversampling technique and apply it to sample data. Requires input data X and labels y. Outputs sampled data X_samp and corresponding labels y_samp. ```Python oversampler= smote_variants.SSO() X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Create Sample NumPy Array - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Creates a simple one-dimensional NumPy array with integer values. ```python tmp = np.array([1, 2, 3, 4, 5]) ``` -------------------------------- ### Import NumPy - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Imports the NumPy library, commonly used for numerical operations, under the alias `np`. ```python import numpy as np ``` -------------------------------- ### Instantiate and Sample with CBSO (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Illustrates the process of creating a CBSO oversampler instance and generating synthetic samples from input data X and y using the sample function. ```Python oversampler= smote_variants.CBSO() X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Display Pandas DataFrame - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Displays the contents of the created pandas DataFrame containing the evaluation results. ```python pdf ``` -------------------------------- ### Instantiate DEAGO and Sample Data (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Demonstrates how to initialize the DEAGO oversampler and use it to generate synthetic samples from input data X and y. ```Python >>> oversampler= smote_variants.DEAGO() >>> X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Display Modified NumPy Array - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Displays the contents of the NumPy array after its elements have been modified by the previous operation. ```python tmp ``` -------------------------------- ### Importing KNeighborsClassifier (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/010_functions.ipynb Imports the `KNeighborsClassifier` class from the `sklearn.neighbors._classification` module. This is a standard classifier from the scikit-learn library. ```python from sklearn.neighbors._classification import KNeighborsClassifier ``` -------------------------------- ### Instantiating and Sampling with MWMOTE (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst This snippet demonstrates how to initialize the MWMOTE oversampling technique from the `smote_variants` library and apply it to sample a dataset. It requires the `smote_variants` library and input data `X` (features) and `y` (labels). The output is the oversampled dataset `X_samp` and corresponding labels `y_samp`. ```Python >>> oversampler= smote_variants.MWMOTE() >>> X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Importing Libraries for Machine Learning and SMOTE in Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/031-deterministic-test-joblib-ml.ipynb Imports necessary libraries for data manipulation (pandas, numpy), parallel processing (joblib), date/time handling (datetime), machine learning models (sklearn classifiers), model selection and evaluation (sklearn), data scaling (sklearn), SMOTE oversampling (smote_variants), and dataset loading (common_datasets). ```python import datetime from joblib import Parallel, delayed import numpy as np import pandas as pd from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.neighbors import KNeighborsClassifier from sklearn.model_selection import RepeatedStratifiedKFold from sklearn.preprocessing import StandardScaler from sklearn.metrics import roc_auc_score from smote_variants.oversampling import SMOTE from common_datasets.binary_classification import get_filtered_data_loaders ``` -------------------------------- ### Importing AMSCO Class Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Imports the AMSCO class, another specific SMOTE variant implementation available in the smote_variants library. ```python from smote_variants import AMSCO ``` -------------------------------- ### Defining Oversamplers for Evaluation (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/examples/002_evaluation_multiple_datasets.ipynb Lists the oversampling techniques from the `smote_variants` library that will be evaluated. Each entry is a tuple specifying the module, class name, and an empty dictionary for potential parameters. `SMOTE_ENN`, `NEATER`, and `Lee` are chosen as examples. ```python oversamplers = [('smote_variants', 'SMOTE_ENN', {}), ('smote_variants', 'NEATER', {}), ('smote_variants', 'Lee', {})] ``` -------------------------------- ### Print Evaluation Results Count - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Prints the total number of evaluation results obtained from the `evaluate_oversamplers` function call. ```python print(len(results)) ``` -------------------------------- ### Defining Cache Path (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Specifies the file system path where results or intermediate data from the evaluation might be cached. ```python cache_path='/home/gykovacs/smote-deterministic2/' ``` -------------------------------- ### Importing Libraries for Evaluation (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/examples/002_evaluation_multiple_datasets.ipynb Imports necessary modules: `os.path` for path manipulation, `KNeighborsClassifier` and `DecisionTreeClassifier` from `sklearn` for classification, `smote_variants` for oversampling methods and evaluation tools, and `imbalanced_databases` for loading datasets. ```python import os.path from sklearn.neighbors import KNeighborsClassifier from sklearn.tree import DecisionTreeClassifier import smote_variants as sv import imbalanced_databases as imbd ``` -------------------------------- ### Displaying Selected Classifier Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/010_functions.ipynb Displays the `classifier` object returned by the `model_selection` function, which represents the chosen classification model. ```python classifier ``` -------------------------------- ### Displaying Selected Sampler Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/010_functions.ipynb Displays the `sampler` object returned by the `model_selection` function, which represents the chosen oversampling method. ```python sampler ``` -------------------------------- ### Instantiating and Sampling with MSMOTE (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Demonstrates how to initialize the MSMOTE oversampler and apply it to generate synthetic samples for an imbalanced dataset. Requires input data `X` and labels `y`. ```Python oversampler= smote_variants.MSMOTE() >>> X_samp, y_samp= oversampler.sample(X, y) ``` -------------------------------- ### Test Deterministic Sample Function - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/025-evaluation.ipynb Calls the `deterministic_sample` utility function with specific arguments to test its behavior with a single choice. ```python deterministic_sample(np.arange(1), 3, np.array([1])) ``` -------------------------------- ### Initializing NDO_sampling Oversampler in Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/oversamplers.rst Illustrates how to create an instance of the NDO_sampling oversampling technique. This is the first step before applying the sampler to data. ```Python oversampler= smote_variants.NDO_sampling() ``` -------------------------------- ### Importing numpy (Redundant) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/006_tests.ipynb Another import statement for the numpy library. This might be redundant if numpy was already imported at the beginning of the script or notebook. ```python import numpy as np ``` -------------------------------- ### Displaying NumPy Array Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/003_vector_random_neighbor.ipynb Prints the contents of the 'dists' NumPy array to the console, showing the generated and normalized probability distributions. ```python dists ``` -------------------------------- ### Initializing New Sample Data (Python) Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/011_metrictensor.ipynb Creates new sample numpy arrays for features `X` and labels `y` to be used in subsequent demonstrations, likely for neighbor-based operations. ```python X = np.array([[1.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 2.0]]) y = np.array([0, 0, 1, 1]) ``` -------------------------------- ### Define Classifiers for Evaluation - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/docs/examples.rst This list defines the classifiers that will be used to evaluate the performance of the oversampling techniques. It includes KNeighborsClassifier with different neighbor counts and a DecisionTreeClassifier. ```Python classifiers = [('sklearn.neighbors', 'KNeighborsClassifier', {'n_neighbors': 3}), ('sklearn.neighbors', 'KNeighborsClassifier', {'n_neighbors': 5}), ('sklearn.tree', 'DecisionTreeClassifier', {})] ``` -------------------------------- ### Configuring SMOTE Variants Logger - Python Source: https://github.com/analyticalmindsltd/smote_variants/blob/master/notebooks/development/031-deterministic-test-joblib.ipynb Sets the logging level for the 'smote_variants' logger to ERROR to suppress informational or warning messages during execution. ```python import logging logger = logging.getLogger('smote_variants') logger.setLevel(logging.ERROR) ```