Tuning Hyperparameters#

This example uses the fmri dataset, performs simple binary classification using a Support Vector Machine classifier and analyze the model.

References#

Waskom, M.L., Frank, M.C., Wagner, A.D. (2016). Adaptive engagement of cognitive control in context-dependent decision-making. Cerebral Cortex.

# Authors: Federico Raimondo <f.raimondo@fz-juelich.de>
# License: AGPL

import numpy as np
from seaborn import load_dataset

from julearn import run_cross_validation
from julearn.utils import configure_logging
from julearn.pipeline import PipelineCreator

Set the logging level to info to see extra information.

configure_logging(level="INFO")
/home/runner/work/julearn/julearn/julearn/utils/logging.py:66: UserWarning: The '__version__' attribute is deprecated and will be removed in MarkupSafe 3.1. Use feature detection, or `importlib.metadata.version("markupsafe")`, instead.
  vstring = str(getattr(module, "__version__", None))
2024-10-17 14:15:49,794 - julearn - INFO - ===== Lib Versions =====
2024-10-17 14:15:49,794 - julearn - INFO - numpy: 1.26.4
2024-10-17 14:15:49,794 - julearn - INFO - scipy: 1.14.1
2024-10-17 14:15:49,794 - julearn - INFO - sklearn: 1.5.2
2024-10-17 14:15:49,794 - julearn - INFO - pandas: 2.2.3
2024-10-17 14:15:49,795 - julearn - INFO - julearn: 0.3.4
2024-10-17 14:15:49,795 - julearn - INFO - ========================

Set the random seed to always have the same example.

Load the dataset.

df_fmri = load_dataset("fmri")
df_fmri.head()
subject timepoint event region signal
0 s13 18 stim parietal -0.017552
1 s5 14 stim parietal -0.080883
2 s12 18 stim parietal -0.081033
3 s11 18 stim parietal -0.046134
4 s10 18 stim parietal -0.037970


Set the dataframe in the right format.

df_fmri = df_fmri.pivot(
    index=["subject", "timepoint", "event"], columns="region", values="signal"
)

df_fmri = df_fmri.reset_index()
df_fmri.head()
region subject timepoint event frontal parietal
0 s0 0 cue 0.007766 -0.006899
1 s0 0 stim -0.021452 -0.039327
2 s0 1 cue 0.016440 0.000300
3 s0 1 stim -0.021054 -0.035735
4 s0 2 cue 0.024296 0.033220


Let’s do a first attempt and use a linear SVM with the default parameters.

X = ["frontal", "parietal"]
y = "event"

creator = PipelineCreator(problem_type="classification")
creator.add("zscore")
creator.add("svm", kernel="linear")

scores = run_cross_validation(X=X, y=y, data=df_fmri, model=creator)

print(scores["test_score"].mean())
2024-10-17 14:15:49,804 - julearn - INFO - Adding step zscore that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:49,804 - julearn - INFO - Step added
2024-10-17 14:15:49,804 - julearn - INFO - Adding step svm that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:49,804 - julearn - INFO - Setting hyperparameter kernel = linear
2024-10-17 14:15:49,804 - julearn - INFO - Step added
2024-10-17 14:15:49,804 - julearn - INFO - ==== Input Data ====
2024-10-17 14:15:49,804 - julearn - INFO - Using dataframe as input
2024-10-17 14:15:49,804 - julearn - INFO -      Features: ['frontal', 'parietal']
2024-10-17 14:15:49,804 - julearn - INFO -      Target: event
2024-10-17 14:15:49,805 - julearn - INFO -      Expanded features: ['frontal', 'parietal']
2024-10-17 14:15:49,805 - julearn - INFO -      X_types:{}
2024-10-17 14:15:49,805 - julearn - WARNING - The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
/home/runner/work/julearn/julearn/julearn/prepare.py:509: RuntimeWarning: The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
  warn_with_log(
2024-10-17 14:15:49,805 - julearn - INFO - ====================
2024-10-17 14:15:49,805 - julearn - INFO -
2024-10-17 14:15:49,806 - julearn - INFO - = Model Parameters =
2024-10-17 14:15:49,806 - julearn - INFO - ====================
2024-10-17 14:15:49,806 - julearn - INFO -
2024-10-17 14:15:49,806 - julearn - INFO - = Data Information =
2024-10-17 14:15:49,806 - julearn - INFO -      Problem type: classification
2024-10-17 14:15:49,806 - julearn - INFO -      Number of samples: 532
2024-10-17 14:15:49,806 - julearn - INFO -      Number of features: 2
2024-10-17 14:15:49,806 - julearn - INFO - ====================
2024-10-17 14:15:49,806 - julearn - INFO -
2024-10-17 14:15:49,807 - julearn - INFO -      Number of classes: 2
2024-10-17 14:15:49,807 - julearn - INFO -      Target type: object
2024-10-17 14:15:49,807 - julearn - INFO -      Class distributions: event
cue     266
stim    266
Name: count, dtype: int64
2024-10-17 14:15:49,807 - julearn - INFO - Using outer CV scheme KFold(n_splits=5, random_state=None, shuffle=False)
2024-10-17 14:15:49,808 - julearn - INFO - Binary classification problem detected.
0.5939164168576971

The score is not so good. Let’s try to see if there is an optimal regularization parameter (C) for the linear SVM. We will use a grid search to find the best C.

creator = PipelineCreator(problem_type="classification")
creator.add("zscore")
creator.add("svm", kernel="linear", C=[0.01, 0.1])

search_params = {
    "kind": "grid",
    "cv": 2,  # to speed up the example
}

scores, estimator = run_cross_validation(
    X=X,
    y=y,
    data=df_fmri,
    model=creator,
    search_params=search_params,
    return_estimator="final",
)

print(scores["test_score"].mean())
2024-10-17 14:15:49,867 - julearn - INFO - Adding step zscore that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:49,867 - julearn - INFO - Step added
2024-10-17 14:15:49,867 - julearn - INFO - Adding step svm that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:49,867 - julearn - INFO - Setting hyperparameter kernel = linear
2024-10-17 14:15:49,867 - julearn - INFO - Tuning hyperparameter C = [0.01, 0.1]
2024-10-17 14:15:49,867 - julearn - INFO - Step added
2024-10-17 14:15:49,867 - julearn - INFO - ==== Input Data ====
2024-10-17 14:15:49,867 - julearn - INFO - Using dataframe as input
2024-10-17 14:15:49,867 - julearn - INFO -      Features: ['frontal', 'parietal']
2024-10-17 14:15:49,867 - julearn - INFO -      Target: event
2024-10-17 14:15:49,867 - julearn - INFO -      Expanded features: ['frontal', 'parietal']
2024-10-17 14:15:49,867 - julearn - INFO -      X_types:{}
2024-10-17 14:15:49,868 - julearn - WARNING - The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
/home/runner/work/julearn/julearn/julearn/prepare.py:509: RuntimeWarning: The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
  warn_with_log(
2024-10-17 14:15:49,868 - julearn - INFO - ====================
2024-10-17 14:15:49,868 - julearn - INFO -
2024-10-17 14:15:49,869 - julearn - INFO - = Model Parameters =
2024-10-17 14:15:49,869 - julearn - INFO - Tuning hyperparameters using grid
2024-10-17 14:15:49,869 - julearn - INFO - Hyperparameters:
2024-10-17 14:15:49,869 - julearn - INFO -      svm__C: [0.01, 0.1]
2024-10-17 14:15:49,869 - julearn - INFO - Using inner CV scheme KFold(n_splits=2, random_state=None, shuffle=False)
2024-10-17 14:15:49,869 - julearn - INFO - Search Parameters:
2024-10-17 14:15:49,869 - julearn - INFO -      cv: KFold(n_splits=2, random_state=None, shuffle=False)
2024-10-17 14:15:49,869 - julearn - INFO - ====================
2024-10-17 14:15:49,869 - julearn - INFO -
2024-10-17 14:15:49,869 - julearn - INFO - = Data Information =
2024-10-17 14:15:49,869 - julearn - INFO -      Problem type: classification
2024-10-17 14:15:49,869 - julearn - INFO -      Number of samples: 532
2024-10-17 14:15:49,869 - julearn - INFO -      Number of features: 2
2024-10-17 14:15:49,870 - julearn - INFO - ====================
2024-10-17 14:15:49,870 - julearn - INFO -
2024-10-17 14:15:49,870 - julearn - INFO -      Number of classes: 2
2024-10-17 14:15:49,870 - julearn - INFO -      Target type: object
2024-10-17 14:15:49,870 - julearn - INFO -      Class distributions: event
cue     266
stim    266
Name: count, dtype: int64
2024-10-17 14:15:49,871 - julearn - INFO - Using outer CV scheme KFold(n_splits=5, random_state=None, shuffle=False) (incl. final model)
2024-10-17 14:15:49,871 - julearn - INFO - Binary classification problem detected.
0.588308940222183

This did not change much, lets explore other kernels too.

creator = PipelineCreator(problem_type="classification")
creator.add("zscore")
creator.add("svm", kernel=["linear", "rbf", "poly"], C=[0.01, 0.1])

scores, estimator = run_cross_validation(
    X=X,
    y=y,
    data=df_fmri,
    model=creator,
    search_params=search_params,
    return_estimator="final",
)

print(scores["test_score"].mean())
2024-10-17 14:15:50,168 - julearn - INFO - Adding step zscore that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:50,169 - julearn - INFO - Step added
2024-10-17 14:15:50,169 - julearn - INFO - Adding step svm that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:50,169 - julearn - INFO - Tuning hyperparameter kernel = ['linear', 'rbf', 'poly']
2024-10-17 14:15:50,169 - julearn - INFO - Tuning hyperparameter C = [0.01, 0.1]
2024-10-17 14:15:50,169 - julearn - INFO - Step added
2024-10-17 14:15:50,169 - julearn - INFO - ==== Input Data ====
2024-10-17 14:15:50,169 - julearn - INFO - Using dataframe as input
2024-10-17 14:15:50,169 - julearn - INFO -      Features: ['frontal', 'parietal']
2024-10-17 14:15:50,169 - julearn - INFO -      Target: event
2024-10-17 14:15:50,169 - julearn - INFO -      Expanded features: ['frontal', 'parietal']
2024-10-17 14:15:50,169 - julearn - INFO -      X_types:{}
2024-10-17 14:15:50,169 - julearn - WARNING - The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
/home/runner/work/julearn/julearn/julearn/prepare.py:509: RuntimeWarning: The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
  warn_with_log(
2024-10-17 14:15:50,170 - julearn - INFO - ====================
2024-10-17 14:15:50,170 - julearn - INFO -
2024-10-17 14:15:50,171 - julearn - INFO - = Model Parameters =
2024-10-17 14:15:50,171 - julearn - INFO - Tuning hyperparameters using grid
2024-10-17 14:15:50,171 - julearn - INFO - Hyperparameters:
2024-10-17 14:15:50,171 - julearn - INFO -      svm__kernel: ['linear', 'rbf', 'poly']
2024-10-17 14:15:50,171 - julearn - INFO -      svm__C: [0.01, 0.1]
2024-10-17 14:15:50,172 - julearn - INFO - Using inner CV scheme KFold(n_splits=2, random_state=None, shuffle=False)
2024-10-17 14:15:50,172 - julearn - INFO - Search Parameters:
2024-10-17 14:15:50,172 - julearn - INFO -      cv: KFold(n_splits=2, random_state=None, shuffle=False)
2024-10-17 14:15:50,172 - julearn - INFO - ====================
2024-10-17 14:15:50,172 - julearn - INFO -
2024-10-17 14:15:50,172 - julearn - INFO - = Data Information =
2024-10-17 14:15:50,172 - julearn - INFO -      Problem type: classification
2024-10-17 14:15:50,172 - julearn - INFO -      Number of samples: 532
2024-10-17 14:15:50,172 - julearn - INFO -      Number of features: 2
2024-10-17 14:15:50,172 - julearn - INFO - ====================
2024-10-17 14:15:50,172 - julearn - INFO -
2024-10-17 14:15:50,173 - julearn - INFO -      Number of classes: 2
2024-10-17 14:15:50,173 - julearn - INFO -      Target type: object
2024-10-17 14:15:50,173 - julearn - INFO -      Class distributions: event
cue     266
stim    266
Name: count, dtype: int64
2024-10-17 14:15:50,174 - julearn - INFO - Using outer CV scheme KFold(n_splits=5, random_state=None, shuffle=False) (incl. final model)
2024-10-17 14:15:50,174 - julearn - INFO - Binary classification problem detected.
0.7087109857168048

It seems that we might have found a better model, but which one is it?

{'svm__C': 0.1, 'svm__kernel': 'rbf'}

Now that we know that a RBF kernel is better, lest test different gamma parameters.

creator = PipelineCreator(problem_type="classification")
creator.add("zscore")
creator.add("svm", kernel="rbf", C=[0.01, 0.1], gamma=[1e-2, 1e-3])

scores, estimator = run_cross_validation(
    X=X,
    y=y,
    data=df_fmri,
    model=creator,
    search_params=search_params,
    return_estimator="final",
)

print(scores["test_score"].mean())
print(estimator.best_params_)
2024-10-17 14:15:50,965 - julearn - INFO - Adding step zscore that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:50,965 - julearn - INFO - Step added
2024-10-17 14:15:50,965 - julearn - INFO - Adding step svm that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:50,965 - julearn - INFO - Setting hyperparameter kernel = rbf
2024-10-17 14:15:50,965 - julearn - INFO - Tuning hyperparameter C = [0.01, 0.1]
2024-10-17 14:15:50,965 - julearn - INFO - Tuning hyperparameter gamma = [0.01, 0.001]
2024-10-17 14:15:50,965 - julearn - INFO - Step added
2024-10-17 14:15:50,965 - julearn - INFO - ==== Input Data ====
2024-10-17 14:15:50,965 - julearn - INFO - Using dataframe as input
2024-10-17 14:15:50,965 - julearn - INFO -      Features: ['frontal', 'parietal']
2024-10-17 14:15:50,965 - julearn - INFO -      Target: event
2024-10-17 14:15:50,965 - julearn - INFO -      Expanded features: ['frontal', 'parietal']
2024-10-17 14:15:50,965 - julearn - INFO -      X_types:{}
2024-10-17 14:15:50,965 - julearn - WARNING - The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
/home/runner/work/julearn/julearn/julearn/prepare.py:509: RuntimeWarning: The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
  warn_with_log(
2024-10-17 14:15:50,966 - julearn - INFO - ====================
2024-10-17 14:15:50,966 - julearn - INFO -
2024-10-17 14:15:50,967 - julearn - INFO - = Model Parameters =
2024-10-17 14:15:50,967 - julearn - INFO - Tuning hyperparameters using grid
2024-10-17 14:15:50,967 - julearn - INFO - Hyperparameters:
2024-10-17 14:15:50,967 - julearn - INFO -      svm__C: [0.01, 0.1]
2024-10-17 14:15:50,967 - julearn - INFO -      svm__gamma: [0.01, 0.001]
2024-10-17 14:15:50,967 - julearn - INFO - Using inner CV scheme KFold(n_splits=2, random_state=None, shuffle=False)
2024-10-17 14:15:50,967 - julearn - INFO - Search Parameters:
2024-10-17 14:15:50,967 - julearn - INFO -      cv: KFold(n_splits=2, random_state=None, shuffle=False)
2024-10-17 14:15:50,967 - julearn - INFO - ====================
2024-10-17 14:15:50,967 - julearn - INFO -
2024-10-17 14:15:50,967 - julearn - INFO - = Data Information =
2024-10-17 14:15:50,967 - julearn - INFO -      Problem type: classification
2024-10-17 14:15:50,967 - julearn - INFO -      Number of samples: 532
2024-10-17 14:15:50,967 - julearn - INFO -      Number of features: 2
2024-10-17 14:15:50,967 - julearn - INFO - ====================
2024-10-17 14:15:50,968 - julearn - INFO -
2024-10-17 14:15:50,968 - julearn - INFO -      Number of classes: 2
2024-10-17 14:15:50,968 - julearn - INFO -      Target type: object
2024-10-17 14:15:50,968 - julearn - INFO -      Class distributions: event
cue     266
stim    266
Name: count, dtype: int64
2024-10-17 14:15:50,969 - julearn - INFO - Using outer CV scheme KFold(n_splits=5, random_state=None, shuffle=False) (incl. final model)
2024-10-17 14:15:50,969 - julearn - INFO - Binary classification problem detected.
0.5188855581026275
{'svm__C': 0.01, 'svm__gamma': 0.001}

It seems that without tuning the gamma parameter we had a better accuracy. Let’s add the default value and see what happens.

creator = PipelineCreator(problem_type="classification")
creator.add("zscore")
creator.add("svm", kernel="rbf", C=[0.01, 0.1], gamma=[1e-2, 1e-3, "scale"])
X = ["frontal", "parietal"]
y = "event"

search_params = {"cv": 2}

scores, estimator = run_cross_validation(
    X=X,
    y=y,
    data=df_fmri,
    model=creator,
    return_estimator="final",
    search_params=search_params,
)

print(scores["test_score"].mean())
print(estimator.best_params_)
2024-10-17 14:15:51,534 - julearn - INFO - Adding step zscore that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:51,534 - julearn - INFO - Step added
2024-10-17 14:15:51,534 - julearn - INFO - Adding step svm that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2024-10-17 14:15:51,534 - julearn - INFO - Setting hyperparameter kernel = rbf
2024-10-17 14:15:51,534 - julearn - INFO - Tuning hyperparameter C = [0.01, 0.1]
2024-10-17 14:15:51,534 - julearn - INFO - Tuning hyperparameter gamma = [0.01, 0.001, 'scale']
2024-10-17 14:15:51,534 - julearn - INFO - Step added
2024-10-17 14:15:51,534 - julearn - INFO - ==== Input Data ====
2024-10-17 14:15:51,534 - julearn - INFO - Using dataframe as input
2024-10-17 14:15:51,534 - julearn - INFO -      Features: ['frontal', 'parietal']
2024-10-17 14:15:51,534 - julearn - INFO -      Target: event
2024-10-17 14:15:51,534 - julearn - INFO -      Expanded features: ['frontal', 'parietal']
2024-10-17 14:15:51,534 - julearn - INFO -      X_types:{}
2024-10-17 14:15:51,535 - julearn - WARNING - The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
/home/runner/work/julearn/julearn/julearn/prepare.py:509: RuntimeWarning: The following columns are not defined in X_types: ['frontal', 'parietal']. They will be treated as continuous.
  warn_with_log(
2024-10-17 14:15:51,535 - julearn - INFO - ====================
2024-10-17 14:15:51,535 - julearn - INFO -
2024-10-17 14:15:51,536 - julearn - INFO - = Model Parameters =
2024-10-17 14:15:51,536 - julearn - INFO - Tuning hyperparameters using grid
2024-10-17 14:15:51,536 - julearn - INFO - Hyperparameters:
2024-10-17 14:15:51,536 - julearn - INFO -      svm__C: [0.01, 0.1]
2024-10-17 14:15:51,536 - julearn - INFO -      svm__gamma: [0.01, 0.001, 'scale']
2024-10-17 14:15:51,536 - julearn - INFO - Using inner CV scheme KFold(n_splits=2, random_state=None, shuffle=False)
2024-10-17 14:15:51,536 - julearn - INFO - Search Parameters:
2024-10-17 14:15:51,536 - julearn - INFO -      cv: KFold(n_splits=2, random_state=None, shuffle=False)
2024-10-17 14:15:51,536 - julearn - INFO - ====================
2024-10-17 14:15:51,536 - julearn - INFO -
2024-10-17 14:15:51,536 - julearn - INFO - = Data Information =
2024-10-17 14:15:51,536 - julearn - INFO -      Problem type: classification
2024-10-17 14:15:51,536 - julearn - INFO -      Number of samples: 532
2024-10-17 14:15:51,537 - julearn - INFO -      Number of features: 2
2024-10-17 14:15:51,537 - julearn - INFO - ====================
2024-10-17 14:15:51,537 - julearn - INFO -
2024-10-17 14:15:51,537 - julearn - INFO -      Number of classes: 2
2024-10-17 14:15:51,537 - julearn - INFO -      Target type: object
2024-10-17 14:15:51,537 - julearn - INFO -      Class distributions: event
cue     266
stim    266
Name: count, dtype: int64
2024-10-17 14:15:51,538 - julearn - INFO - Using outer CV scheme KFold(n_splits=5, random_state=None, shuffle=False) (incl. final model)
2024-10-17 14:15:51,538 - julearn - INFO - Binary classification problem detected.
0.7087109857168048
{'svm__C': 0.1, 'svm__gamma': 'scale'}
print(estimator.best_estimator_["svm"]._gamma)
0.5

Total running time of the script: (0 minutes 2.539 seconds)

Gallery generated by Sphinx-Gallery