Health and life sciences

Machine learning for neuroimaging with scikit-learn

With scikit-learn and Nilearn you can decode which stimulus a person viewed from fMRI voxels, predict traits such as age group from functional connectivity, and map which brain regions carry the predictive signal. Nilearn turns brain images into arrays that scikit-learn estimators accept.

How scikit-learn is used on brain images

Decoding what a person saw or did

Voxels inside a mask become features, each trial or block becomes a row, and a linear classifier predicts the condition. Cross-validation runs across scanning runs so the score reflects new data.

Real exampleNilearn decoding example: faces versus housesAn official Nilearn example that selects voxels with an ANOVA step and classifies face versus house trials in the Haxby fMRI dataset with a support vector machine.

Predicting participant traits from connectivity

Functional connectivity matrices summarise how regions covary over time, and a classifier learns which patterns separate groups. The choice of connectivity measure usually matters more than the classifier.

Real exampleNilearn example: classification of age groupsAn official Nilearn example that builds functional connectivity measures from resting-state data and classifies age groups with scikit-learn.

Mapping where the predictive signal lies

A searchlight moves a small sphere across the brain and fits a classifier inside each one, producing a map of local decoding accuracy. It answers where information is, rather than how well the whole brain decodes.

Real exampleNilearn searchlight exampleAn official Nilearn example that runs a searchlight analysis for face versus house recognition and compares the resulting map with a univariate F-score map.

Standard analyses with a standard toolkit

Nilearn turns images into arrays and plots results, while scikit-learn supplies the estimators, pipelines and cross-validation. Using the same toolkit as published analyses makes results easier to check and reuse.

Real exampleMachine learning for neuroimaging with scikit-learn (Abraham et al., 2014)The paper describing how scikit-learn is applied to functional neuroimaging, covering supervised decoding and encoding settings and unsupervised analysis of image sets.

Can you learn neuroimaging machine learning?

Yes, and this is one of the friendliest entry points because the data, the tools and the tutorials are all public. Learn Python and the image formats first, then scikit-learn pipelines and cross-validation, then Nilearn's maskers and decoder. Deep learning is optional here and rarely the first thing you need.

  1. Python and arraysWork comfortably with NumPy arrays and pandas tables, because every image eventually becomes a matrix of samples by voxels.Learn it in Python
  2. Neuroimaging data basicsLearn NIfTI files, masks, preprocessing and run structure with NiBabel and fMRIPrep outputs, so you know what each dimension means.
  3. scikit-learn pipelinesBuild pipelines so scaling and feature selection are fitted inside each training fold rather than on the whole dataset.Learn it in Machine Learning
  4. Validation and statisticsUse run-wise or subject-wise cross-validation, compare against chance, and test significance with permutations.Learn it in Statistics
  5. Nilearn in practiceUse maskers, the Decoder object and the plotting functions to go from images to a result and a figure in one script.

Moving into neuroimaging machine learning

If you already work in the field

Neuroimagers already understand preprocessing, design matrices and the pitfalls of group analysis. Add scikit-learn pipelines and cross-validation, then rerun one of your published univariate analyses as a decoding analysis and compare what each tells you.

If you come from data science or ML

Machine learning practitioners should learn what the scanner measures, how slow the signal is and how few independent samples a study has. Start from the Nilearn examples on public datasets, then work with an imaging researcher on the design and the splits.

Roles that do this workNeuroimaging data scientistResearch software engineer, neuroimagingComputational neuroscientistImaging research analyst

Tools you will use

ToolWhat you use it for
NilearnMask brain images, extract connectivity features and run decoders that wrap scikit-learn estimators.
scikit-learnFit linear models and support vector machines, and run grouped cross-validation over runs or subjects.
NiBabelRead and write NIfTI and other neuroimaging file formats as arrays.
fMRIPrepPreprocess raw BIDS fMRI data with a standard pipeline before feature extraction.

Public data to practise on

Your first project

Download the Haxby visual object recognition dataset, decode faces versus houses with a linear support vector classifier on masked voxels, and compare it with a dummy classifier using leave-one-run-out accuracy.

Common questions

Do you need deep learning for fMRI decoding?

Usually not. With a few hundred trials per subject, regularised linear models are the standard and often the best choice. Deep models become interesting with large multi-subject datasets.

How should you cross-validate brain imaging data?

Leave out whole runs within a subject, or whole subjects for group questions. Random trial splits leak the slow autocorrelation of the signal and produce accuracies that do not hold up.

Which public datasets are good for practice?

The Haxby and Miyawaki datasets that Nilearn downloads automatically are small enough to iterate on, and OpenNeuro hosts many full studies when you want something larger.

What does a decoding accuracy above chance actually show?

That the condition is predictable from the signal in your mask. It does not show that the region causes the behaviour or that the representation is the one you had in mind.

Courses for these skills

New to programming or maths? Start with Python, or follow the machine learning path in order. Published lessons are free to read; compare Free and Pro.