.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_decoding/plot_haxby_multiclass.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 or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_02_decoding_plot_haxby_multiclass.py: The haxby dataset: different multi-class strategies =================================================== We compare one vs all and one vs one multi-class strategies: the overall cross-validated accuracy and the confusion matrix. .. include:: ../../../examples/masker_note.rst .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: Python import numpy as np import pandas as pd from nilearn import datasets from nilearn.plotting import plot_matrix, show .. GENERATED FROM PYTHON SOURCE LINES 19-21 Load the Haxby data dataset --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-47 .. code-block:: Python # By default 2nd subject from haxby datasets will be fetched. haxby_dataset = datasets.fetch_haxby() # Print basic information on the dataset print(f"Mask nifti images are located at: {haxby_dataset.mask}") print(f"Functional nifti images are located at: {haxby_dataset.func[0]}") func_filename = haxby_dataset.func[0] mask_filename = haxby_dataset.mask # Load the behavioral data that we will predict labels = pd.read_csv(haxby_dataset.session_target[0], sep=" ") y = labels["labels"] run = labels["chunks"] # Remove the rest condition, it is not very interesting non_rest = y != "rest" y = y[non_rest] # Get the labels of the numerical conditions represented by the vector y unique_conditions, order = np.unique(y, return_index=True) # Sort the conditions by the order of appearance unique_conditions = unique_conditions[np.argsort(order)] .. rst-class:: sphx-glr-script-out .. code-block:: none Mask nifti images are located at: /home/himanshu/nilearn_data/haxby2001/mask.nii.gz Functional nifti images are located at: /home/himanshu/nilearn_data/haxby2001/subj2/bold.nii.gz .. GENERATED FROM PYTHON SOURCE LINES 48-50 Prepare the :term:`fMRI` data ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 50-67 .. code-block:: Python from nilearn.maskers import NiftiMasker # For decoding, standardizing is often very important nifti_masker = NiftiMasker( mask_img=mask_filename, standardize="zscore_sample", runs=run, smoothing_fwhm=4, memory="nilearn_cache", memory_level=1, ) X = nifti_masker.fit_transform(func_filename) # Remove the "rest" condition X = X[non_rest] run = run[non_rest] .. rst-class:: sphx-glr-script-out .. code-block:: none /home/himanshu/.local/miniconda3/envs/nilearnpy/lib/python3.12/site-packages/nilearn/image/resampling.py:492: UserWarning: The provided image has no sform in its header. Please check the provided file. Results may not be as expected. .. GENERATED FROM PYTHON SOURCE LINES 68-72 Build the decoders, using scikit-learn -------------------------------------- Here we use a Support Vector Classification, with a linear kernel, and a simple feature selection step .. GENERATED FROM PYTHON SOURCE LINES 72-96 .. code-block:: Python from sklearn.feature_selection import SelectKBest, f_classif from sklearn.multiclass import OneVsOneClassifier, OneVsRestClassifier from sklearn.pipeline import Pipeline from sklearn.svm import SVC svc_ovo = OneVsOneClassifier( Pipeline( [ ("anova", SelectKBest(f_classif, k=500)), ("svc", SVC(kernel="linear")), ] ) ) svc_ova = OneVsRestClassifier( Pipeline( [ ("anova", SelectKBest(f_classif, k=500)), ("svc", SVC(kernel="linear")), ] ) ) .. GENERATED FROM PYTHON SOURCE LINES 97-99 Now we compute cross-validation scores -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 99-108 .. code-block:: Python from sklearn.model_selection import cross_val_score cv_scores_ovo = cross_val_score(svc_ovo, X, y, cv=5, verbose=1) cv_scores_ova = cross_val_score(svc_ova, X, y, cv=5, verbose=1) print("OvO:", cv_scores_ovo.mean()) print("OvA:", cv_scores_ova.mean()) .. rst-class:: sphx-glr-script-out .. code-block:: none OvO: 0.601855088049469 OvA: 0.6712058072321548 .. GENERATED FROM PYTHON SOURCE LINES 109-111 Plot barplots of the prediction scores -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 111-118 .. code-block:: Python from matplotlib import pyplot as plt plt.figure(figsize=(4, 3)) plt.boxplot([cv_scores_ova, cv_scores_ovo]) plt.xticks([1, 2], ["One vs All", "One vs One"]) plt.title("Prediction: accuracy score") .. image-sg:: /auto_examples/02_decoding/images/sphx_glr_plot_haxby_multiclass_001.png :alt: Prediction: accuracy score :srcset: /auto_examples/02_decoding/images/sphx_glr_plot_haxby_multiclass_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Prediction: accuracy score') .. GENERATED FROM PYTHON SOURCE LINES 119-123 Plot a confusion matrix ----------------------- We fit on the first 10 runs and plot a confusion matrix on the last 2 runs .. GENERATED FROM PYTHON SOURCE LINES 123-147 .. code-block:: Python from sklearn.metrics import confusion_matrix svc_ovo.fit(X[run < 10], y[run < 10]) y_pred_ovo = svc_ovo.predict(X[run >= 10]) plot_matrix( confusion_matrix(y_pred_ovo, y[run >= 10]), labels=unique_conditions, title="Confusion matrix: One vs One", cmap="hot_r", ) svc_ova.fit(X[run < 10], y[run < 10]) y_pred_ova = svc_ova.predict(X[run >= 10]) plot_matrix( confusion_matrix(y_pred_ova, y[run >= 10]), labels=unique_conditions, title="Confusion matrix: One vs All", cmap="hot_r", ) show() .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/02_decoding/images/sphx_glr_plot_haxby_multiclass_002.png :alt: Confusion matrix: One vs One :srcset: /auto_examples/02_decoding/images/sphx_glr_plot_haxby_multiclass_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/02_decoding/images/sphx_glr_plot_haxby_multiclass_003.png :alt: Confusion matrix: One vs All :srcset: /auto_examples/02_decoding/images/sphx_glr_plot_haxby_multiclass_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 36.784 seconds) **Estimated memory usage:** 3156 MB .. _sphx_glr_download_auto_examples_02_decoding_plot_haxby_multiclass.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/nilearn/nilearn/main?urlpath=lab/tree/notebooks/auto_examples/02_decoding/plot_haxby_multiclass.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_haxby_multiclass.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_haxby_multiclass.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_