.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/Builtin/spatial_perturbation_logistic_regression.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_Builtin_spatial_perturbation_logistic_regression.py: Spatial perturbations and logistic regression ============================================= This example illustrates the use of the :class:`skwdro.linear_models.LogisticRegression` class on datasets that are shifted at test time. .. GENERATED FROM PYTHON SOURCE LINES 8-19 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import make_blobs, make_moons from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split from skwdro.linear_models import LogisticRegression from utils.classifier_comparison import plot_classifier_comparison .. GENERATED FROM PYTHON SOURCE LINES 20-22 Setup ~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 22-40 .. code-block:: Python n = 500 # Total number of samples n_train = (3 * n) // 4 # Number of training samples n_test = n - n_train # Number of test samples sdevs = [(2.5, 5), (1, 5)] # Fix centers for blobs dataset pos = 4 centers = [np.array([-pos,-pos]), np.array([pos,pos])] # Create datasets with variance that is shifted at test time datasets = [] for (sdev_1, sdev_2) in sdevs: train_dataset = make_blobs(n_samples=n_train, centers=centers, cluster_std=(sdev_1, sdev_2)) # type: ignore test_dataset = make_blobs(n_samples=n_test, centers=centers, cluster_std=(sdev_2, sdev_1)) # type: ignore datasets.append((train_dataset, test_dataset)) .. GENERATED FROM PYTHON SOURCE LINES 41-43 WDRO classifiers ~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 43-62 .. code-block:: Python # Rho chosen analytically rhos = [0, 2*4**2] # Kappa: weight of label shift kappa = 1000 # Cost: # t: torch backend # NLC: norm cost that takes labels into account # 2 2 : squared 2-norm # kappa: weight of label shift cost = f"t-NLC-2-2-{kappa}" # WDRO classifier classifiers = [] for rho in rhos: classifiers.append(LogisticRegression(rho=rho, cost=cost)) .. GENERATED FROM PYTHON SOURCE LINES 63-65 Make plot ~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 65-69 .. code-block:: Python names = ["Logistic Regression", "WDRO Logistic Regression"] levels = [0., 0.25, 0.45, 0.5, 0.55, 0.75, 1.] plot_classifier_comparison(names, classifiers, datasets, levels=levels) # type: ignore .. image-sg:: /examples/Builtin/images/sphx_glr_spatial_perturbation_logistic_regression_001.png :alt: Training data, Testing data, Logistic Regression, WDRO Logistic Regression :srcset: /examples/Builtin/images/sphx_glr_spatial_perturbation_logistic_regression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Dataset 0, Classifier Logistic Regression: Test Acc. 95%, F1 95% Dataset 0, Classifier WDRO Logistic Regression: Test Acc. 85%, F1 83% Dataset 1, Classifier Logistic Regression: Test Acc. 81%, F1 84% Dataset 1, Classifier WDRO Logistic Regression: Test Acc. 90%, F1 91% .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 33.304 seconds) .. _sphx_glr_download_examples_Builtin_spatial_perturbation_logistic_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: spatial_perturbation_logistic_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: spatial_perturbation_logistic_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: spatial_perturbation_logistic_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_