Transforming target variable with z-score

This example uses the sklearn diabetes regression dataset, and transforms the target variable, in this case, using z-score. Then, we perform a regression analysis using Ridge Regression model.

# Authors: Lya K. Paas Oliveros <l.paas.oliveros@fz-juelich.de>
#          Sami Hamdan <s.hamdan@fz-juelich.de>
#
# License: AGPL

import pandas as pd
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split

from julearn import run_cross_validation
from julearn.utils import configure_logging

from julearn.pipeline import PipelineCreator, TargetPipelineCreator

Set the logging level to info to see extra information.

configure_logging(level="INFO")
2026-05-29 20:46:12,891 - julearn - INFO - ===== Lib Versions =====
2026-05-29 20:46:12,891 - julearn - INFO - numpy: 2.4.6
2026-05-29 20:46:12,891 - julearn - INFO - scipy: 1.17.1
2026-05-29 20:46:12,891 - julearn - INFO - sklearn: 1.8.0
2026-05-29 20:46:12,891 - julearn - INFO - pandas: 3.0.3
2026-05-29 20:46:12,892 - julearn - INFO - julearn: 0.3.5
2026-05-29 20:46:12,892 - julearn - INFO - ========================

Load the diabetes dataset from sklearn as a pandas.DataFrame.

features, target = load_diabetes(return_X_y=True, as_frame=True)

Dataset contains ten variables age, sex, body mass index, average blood pressure, and six blood serum measurements (s1-s6) diabetes patients and a quantitative measure of disease progression one year after baseline which is the target we are interested in predicting.

print("Features: \n", features.head())
print("Target: \n", target.describe())
Features:
         age       sex       bmi        bp        s1        s2        s3        s4        s5        s6
0  0.038076  0.050680  0.061696  0.021872 -0.044223 -0.034821 -0.043401 -0.002592  0.019907 -0.017646
1 -0.001882 -0.044642 -0.051474 -0.026328 -0.008449 -0.019163  0.074412 -0.039493 -0.068332 -0.092204
2  0.085299  0.050680  0.044451 -0.005670 -0.045599 -0.034194 -0.032356 -0.002592  0.002861 -0.025930
3 -0.089063 -0.044642 -0.011595 -0.036656  0.012191  0.024991 -0.036038  0.034309  0.022688 -0.009362
4  0.005383 -0.044642 -0.036385  0.021872  0.003935  0.015596  0.008142 -0.002592 -0.031988 -0.046641
Target:
 count    442.000000
mean     152.133484
std       77.093005
min       25.000000
25%       87.000000
50%      140.500000
75%      211.500000
max      346.000000
Name: target, dtype: float64

Let’s combine features and target together in one dataframe and define X and y.

data_diabetes = pd.concat([features, target], axis=1)

X = ["age", "sex", "bmi", "bp", "s1", "s2", "s3", "s4", "s5", "s6"]
y = "target"

Split the dataset into train and test.

Let’s create the model. Since we will be transforming the target variable we will first need to create a TargetPipelineCreator for this.

target_creator = TargetPipelineCreator()
target_creator.add("zscore")
<julearn.pipeline.target_pipeline_creator.TargetPipelineCreator object at 0x11dccc850>

Now we can create the pipeline using a PipelineCreator.

creator = PipelineCreator(problem_type="regression")
creator.add(target_creator, apply_to="target")
creator.add("ridge")

scores, model = run_cross_validation(
    X=X,
    y=y,
    data=train_diabetes,
    model=creator,
    return_estimator="final",
    scoring="neg_mean_absolute_error",
)

print(scores.head(5))
2026-05-29 20:46:12,922 - julearn - INFO - Adding step jutargetpipeline that applies to ColumnTypes<types={'target'}; pattern=(?:target)>
2026-05-29 20:46:12,923 - julearn - INFO - Step added
2026-05-29 20:46:12,923 - julearn - INFO - Adding step ridge that applies to ColumnTypes<types={'continuous'}; pattern=(?:__:type:__continuous)>
2026-05-29 20:46:12,923 - julearn - INFO - Step added
2026-05-29 20:46:12,923 - julearn - INFO - ==== Input Data ====
2026-05-29 20:46:12,923 - julearn - INFO - Using dataframe as input
2026-05-29 20:46:12,924 - julearn - INFO -      Features: ['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']
2026-05-29 20:46:12,924 - julearn - INFO -      Target: target
2026-05-29 20:46:12,924 - julearn - INFO -      Expanded features: ['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']
2026-05-29 20:46:12,924 - julearn - INFO -      X_types:{}
2026-05-29 20:46:12,924 - julearn - WARNING - The following columns are not defined in X_types: ['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']. They will be treated as continuous.
/private/var/folders/09/t22x2_p106j7p24khr0jdxrw0000gn/T/tmpyvhr0tue/julearn/prepare.py:592: RuntimeWarning: The following columns are not defined in X_types: ['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']. They will be treated as continuous.
  warn_with_log(
2026-05-29 20:46:12,926 - julearn - INFO - ====================
2026-05-29 20:46:12,926 - julearn - INFO -
2026-05-29 20:46:12,926 - julearn - INFO - = Model Parameters =
2026-05-29 20:46:12,927 - julearn - INFO - ====================
2026-05-29 20:46:12,927 - julearn - INFO -
2026-05-29 20:46:12,927 - julearn - INFO - = Data Information =
2026-05-29 20:46:12,927 - julearn - INFO -      Problem type: regression
2026-05-29 20:46:12,927 - julearn - INFO -      Number of samples: 309
2026-05-29 20:46:12,927 - julearn - INFO -      Number of features: 10
2026-05-29 20:46:12,927 - julearn - INFO - ====================
2026-05-29 20:46:12,928 - julearn - INFO -
2026-05-29 20:46:12,928 - julearn - INFO -      Target type: float64
2026-05-29 20:46:12,928 - julearn - INFO - Using outer CV scheme KFold(n_splits=5, random_state=None, shuffle=False) (incl. final model)
   fit_time  score_time  test_score  n_train  n_test  repeat  fold                          cv_mdsum
0  0.004479    0.002150  -46.841131      247      62       0     0  b10eef89b4192178d482d7a1587a248a
1  0.005649    0.002071  -51.472092      247      62       0     1  b10eef89b4192178d482d7a1587a248a
2  0.004302    0.002070  -53.857139      247      62       0     2  b10eef89b4192178d482d7a1587a248a
3  0.004216    0.003104  -52.551950      247      62       0     3  b10eef89b4192178d482d7a1587a248a
4  0.006661    0.002164  -52.845546      248      61       0     4  b10eef89b4192178d482d7a1587a248a

Mean value of mean absolute error across CV

print(scores["test_score"].mean() * -1)
51.513571519143696

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

Gallery generated by Sphinx-Gallery