.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/decoding/plot_01_basic_decoding.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_auto_examples_decoding_plot_01_basic_decoding.py: ===================================================== Basic Decoding: Classification and Model Verification ===================================================== This example demonstrates the core functionality of the ``coco_pipe.decoding`` module. We generate a synthetic classification dataset and evaluate two classical machine learning models (Logistic Regression and Random Forest) using a rigorous cross-validation pipeline. The decoding engine automatically handles data splitting, model fitting, metric calculation, and produces an `ExperimentResult` object that natively integrates with our visualization tools. .. GENERATED FROM PYTHON SOURCE LINES 17-19 Imports and Setup ----------------- .. GENERATED FROM PYTHON SOURCE LINES 19-31 .. code-block:: Python import matplotlib.pyplot as plt from sklearn.datasets import make_classification from coco_pipe.decoding import Experiment, ExperimentConfig from coco_pipe.decoding.configs import ( CVConfig, LogisticRegressionConfig, RandomForestClassifierConfig, ) from coco_pipe.viz.decoding import plot_confusion_matrix, plot_decoding_scores .. GENERATED FROM PYTHON SOURCE LINES 32-36 1. Generate Synthetic Data -------------------------- We create a synthetic 2-class dataset with 500 samples and 20 features, where only 5 features are truly informative. .. GENERATED FROM PYTHON SOURCE LINES 36-44 .. code-block:: Python X, y = make_classification( n_samples=500, n_features=20, n_informative=5, random_state=42, ) .. GENERATED FROM PYTHON SOURCE LINES 45-50 2. Configure the Experiment --------------------------- The ``ExperimentConfig`` defines the entire analytical pipeline declaratively. We specify the task, the models to evaluate, the evaluation metrics, and the cross-validation strategy. .. GENERATED FROM PYTHON SOURCE LINES 50-64 .. code-block:: Python config = ExperimentConfig( task="classification", models={ "LogisticRegression": LogisticRegressionConfig(), "RandomForest": RandomForestClassifierConfig(n_estimators=50, random_state=42), }, metrics=["accuracy", "roc_auc", "f1"], cv=CVConfig(strategy="stratified", n_splits=5, random_state=42), ) # Initialize the experiment engine exp = Experiment(config) .. GENERATED FROM PYTHON SOURCE LINES 65-69 3. Execute the Decoding Pipeline -------------------------------- The ``run`` method takes the data and executes the cross-validation loop for all declared models. .. GENERATED FROM PYTHON SOURCE LINES 69-74 .. code-block:: Python print("Executing decoding pipeline...") result = exp.run(X, y) print("Decoding complete!") .. rst-class:: sphx-glr-script-out .. code-block:: none Executing decoding pipeline... [Parallel(n_jobs=-1)]: Using backend LokyBackend with 4 concurrent workers. [Parallel(n_jobs=-1)]: Done 5 out of 5 | elapsed: 2.8s finished [Parallel(n_jobs=-1)]: Using backend LokyBackend with 4 concurrent workers. [Parallel(n_jobs=-1)]: Done 5 out of 5 | elapsed: 0.3s finished Decoding complete! .. GENERATED FROM PYTHON SOURCE LINES 75-79 4. Visualize Results -------------------- We can pass the ``ExperimentResult`` directly into ``coco_pipe.viz.decoding`` functions to instantly generate publication-ready plots. .. GENERATED FROM PYTHON SOURCE LINES 79-90 .. code-block:: Python # Plot aggregate scalar scores (Accuracy, ROC AUC, F1) plot_decoding_scores(result, kind="bar") plt.show() # Plot the confusion matrix for the Random Forest model plot_confusion_matrix( result, model="RandomForest", title="Random Forest: Confusion Matrix" ) plt.show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/decoding/images/sphx_glr_plot_01_basic_decoding_001.png :alt: Decoding Scores :srcset: /auto_examples/decoding/images/sphx_glr_plot_01_basic_decoding_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/decoding/images/sphx_glr_plot_01_basic_decoding_002.png :alt: Random Forest: Confusion Matrix :srcset: /auto_examples/decoding/images/sphx_glr_plot_01_basic_decoding_002.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 91-98 Interpretation -------------- - **Decoding Scores**: The bar chart shows the mean performance metrics across all 5 cross-validation folds, including standard error bands. Random Forest often outperforms Logistic Regression on non-linear informative features. - **Confusion Matrix**: The matrix visualizes the out-of-fold predictions, normalized by true class counts, highlighting the model's sensitivity and specificity. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.523 seconds) .. _sphx_glr_download_auto_examples_decoding_plot_01_basic_decoding.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_basic_decoding.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_basic_decoding.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_basic_decoding.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_