.. 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 JupyterLite or 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. .. GENERATED FROM PYTHON SOURCE LINES 9-16 .. 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 17-19 Load the Haxby data dataset --------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-45 .. 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 [fetch_haxby] Dataset directory found: /home/runner/nilearn_data/haxby2001 Mask nifti images are located at: /home/runner/nilearn_data/haxby2001/mask.nii.gz Functional nifti images are located at: /home/runner/nilearn_data/haxby2001/subj2/bold.nii.gz .. GENERATED FROM PYTHON SOURCE LINES 46-48 Prepare the :term:`fMRI` data ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 48-65 .. code-block:: Python from nilearn.maskers import NiftiMasker # For decoding, standardizing is often very important nifti_masker = NiftiMasker( mask_img=mask_filename, runs=run, smoothing_fwhm=4, memory="nilearn_cache", memory_level=1, verbose=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 \[NiftiMasker.wrapped] Loading mask from '/home/runner/nilearn_data/haxby2001/mask.nii.gz' \[NiftiMasker.wrapped] Loading data from '/home/runner/nilearn_data/haxby2001/subj2/bold.nii.gz' \[NiftiMasker.wrapped] Resampling mask \[NiftiMasker.wrapped] Finished fit /home/runner/work/nilearn/nilearn/examples/02_decoding/plot_haxby_multiclass.py:59: FutureWarning: boolean values for 'standardize' will be deprecated in nilearn 0.15.0. Use 'zscore_sample' instead of 'True' or use 'None' instead of 'False'. ________________________________________________________________________________ [Memory] Calling nilearn.maskers.nifti_masker.filter_and_mask... filter_and_mask('/home/runner/nilearn_data/haxby2001/subj2/bold.nii.gz', , { 'clean_args': None, 'clean_kwargs': {}, 'cmap': 'gray', 'detrend': False, 'dtype': None, 'high_pass': None, 'high_variance_confounds': False, 'low_pass': None, 'reports': True, 'runs': 0 0 1 0 2 0 3 0 4 0 .. 1447 11 1448 11 1449 11 1450 11 1451 11 Name: chunks, Length: 1452, dtype: int64, 'smoothing_fwhm': 4, 'standardize': False, 'standardize_confounds': True, 't_r': None, 'target_affine': None, 'target_shape': None}, memory_level=1, memory=Memory(location=nilearn_cache/joblib), verbose=1, confounds=None, sample_mask=None, copy=True, sklearn_output_config=None) \[NiftiMasker.wrapped] Loading data from \[NiftiMasker.wrapped] Smoothing images \[NiftiMasker.wrapped] Extracting region signals \[NiftiMasker.wrapped] Cleaning extracted signals _________________________________________________filter_and_mask - 10.1s, 0.2min .. GENERATED FROM PYTHON SOURCE LINES 66-70 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 70-94 .. 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 95-97 Now we compute cross-validation scores -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 97-106 .. 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 [Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers. [Parallel(n_jobs=1)]: Done 5 out of 5 | elapsed: 6.4s finished [Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers. [Parallel(n_jobs=1)]: Done 5 out of 5 | elapsed: 3.5s finished OvO: 0.4721804005914773 OvA: 0.5450867052023122 .. GENERATED FROM PYTHON SOURCE LINES 107-109 Plot barplots of the prediction scores -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 109-116 .. 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 117-121 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 121-145 .. 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="inferno", ) 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="inferno", ) 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 25.741 seconds) **Estimated memory usage:** 2414 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/0.14.0?urlpath=lab/tree/notebooks/auto_examples/02_decoding/plot_haxby_multiclass.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/02_decoding/plot_haxby_multiclass.ipynb :alt: Launch JupyterLite :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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_haxby_multiclass.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_