Target Generation

This example uses the iris dataset and tests a regression model in which the target variable is generated from some features within the cross-validation procedure. We will use the Iris dataset and generate a target variable using PCA on the petal features. Then, we will evaluate if a regression model can predict the generated target from the sepal features

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

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

Set the logging level to info to see extra information.

configure_logging(level="DEBUG")
2026-10-27 10:54:51,524 - julearn - INFO - ===== Lib Versions =====
2026-10-27 10:54:51,525 - julearn - INFO - numpy: 2.5.4
2026-10-27 10:54:51,525 - julearn - INFO - scipy: 1.18.1
2026-10-27 10:54:51,525 - julearn - INFO - sklearn: 1.9.1
2026-10-27 10:54:51,525 - julearn - INFO - pandas: 3.1.0
2026-10-27 10:54:51,525 - julearn - INFO - julearn: 0.3.6.dev108
2026-10-27 10:54:51,525 - julearn - INFO - ========================
df_iris = load_dataset("iris")

As features, we will use the sepal length, width and petal length. We will try to predict the species.

X = ["sepal_length", "sepal_width", "petal_length", "petal_width"]
y = "__generated__"  # to indicate to julearn that the target will be generated


# Define our feature types
X_types = {
    "sepal": ["sepal_length", "sepal_width"],
    "petal": ["petal_length", "petal_width"],
}

We now use a Pipeline Creator to create the pipeline that will generate the features. This special pipeline should be configured to be a “transformer” and apply to the “petal” feature types.

target_creator = PipelineCreator(problem_type="transformer", apply_to="petal")
target_creator.add("pca", n_components=2)
# Select only the first component
target_creator.add("pick_columns", keep="pca__pca0")
2026-10-27 10:54:51,527 - julearn - INFO - Adding step pca that applies to ColumnTypes<types={'petal'}; pattern=(?:__:type:__petal)>
2026-10-27 10:54:51,528 - julearn - INFO - Setting hyperparameter n_components = 2
2026-10-27 10:54:51,528 - julearn - DEBUG - Getting estimator from string: pca
2026-10-27 10:54:51,528 - julearn - INFO - Step added
2026-10-27 10:54:51,528 - julearn - INFO - Adding step pick_columns that applies to ColumnTypes<types={'petal'}; pattern=(?:__:type:__petal)>
2026-10-27 10:54:51,528 - julearn - INFO - Setting hyperparameter keep = pca__pca0
2026-10-27 10:54:51,529 - julearn - DEBUG - Getting estimator from string: pick_columns
2026-10-27 10:54:51,529 - julearn - INFO - Step added

<julearn.pipeline.pipeline_creator.PipelineCreator object at 0x703749fb5750>

We now create the pipeline that will be used to predict the target. This pipeline will be a regression pipeline. The step previous to the model should be the the generate_target, applying to the “petal” features and using the target_creator pipeline as the transformer.

creator = PipelineCreator(problem_type="regression")
creator.add("zscore", apply_to="*")
creator.add("generate_target", apply_to="petal", transformer=target_creator)
creator.add("linreg", apply_to="sepal")
2026-10-27 10:54:51,529 - julearn - INFO - Adding step zscore that applies to ColumnTypes<types={'*'}; pattern=.*>
2026-10-27 10:54:51,530 - julearn - DEBUG - Getting estimator from string: zscore
2026-10-27 10:54:51,530 - julearn - INFO - Step added
2026-10-27 10:54:51,530 - julearn - INFO - Adding step generate_target that applies to ColumnTypes<types={'petal'}; pattern=(?:__:type:__petal)>
2026-10-27 10:54:51,530 - julearn - INFO - Setting hyperparameter transformer = PipelineCreator:
  Step 0: pca
    estimator:     PCA(n_components=2)
    apply to:      ColumnTypes<types={'petal'}; pattern=(?:__:type:__petal)>
    needed types:  ColumnTypes<types={'petal'}; pattern=(?:__:type:__petal)>
    tuning params: {}
  Step 1: pick_columns
    estimator:     PickColumns(keep='pca__pca0')
    apply to:      ColumnTypes<types={'petal'}; pattern=(?:__:type:__petal)>
    needed types:  ColumnTypes<types={'*'}; pattern=.*>
    tuning params: {}

2026-10-27 10:54:51,531 - julearn - DEBUG - Special step is generate_target
2026-10-27 10:54:51,532 - julearn - INFO - Step added
2026-10-27 10:54:51,532 - julearn - INFO - Adding step linreg that applies to ColumnTypes<types={'sepal'}; pattern=(?:__:type:__sepal)>
2026-10-27 10:54:51,532 - julearn - DEBUG - Getting estimator from string: linreg
2026-10-27 10:54:51,532 - julearn - INFO - Step added

<julearn.pipeline.pipeline_creator.PipelineCreator object at 0x703768182950>

We finally evaluate the model within the cross validation.

scores, model = run_cross_validation(
    X=X,
    y=y,
    X_types=X_types,
    data=df_iris,
    model=creator,
    return_estimator="final",
    cv=2,
)

print(scores["test_score"])  # type: ignore
2026-10-27 10:54:51,533 - julearn - INFO - ==== Input Data ====
2026-10-27 10:54:51,533 - julearn - INFO - Using dataframe as input
2026-10-27 10:54:51,533 - julearn - INFO -      Features: ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
2026-10-27 10:54:51,533 - julearn - INFO -      Target: __generated__
2026-10-27 10:54:51,534 - julearn - INFO -      Expanded features: ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
2026-10-27 10:54:51,534 - julearn - INFO -      X_types:{'sepal': ['sepal_length', 'sepal_width'], 'petal': ['petal_length', 'petal_width']}
2026-10-27 10:54:51,535 - julearn - INFO - Target will be generated
2026-10-27 10:54:51,535 - julearn - INFO - ====================
2026-10-27 10:54:51,535 - julearn - INFO -
2026-10-27 10:54:51,535 - julearn - DEBUG - Generating pipeline from PipelineCreator or list of them
2026-10-27 10:54:51,535 - julearn - DEBUG - Creating pipeline
2026-10-27 10:54:51,535 - julearn - DEBUG - Ensuring target generator pipeline
2026-10-27 10:54:51,535 - julearn - DEBUG - Creating pipeline
2026-10-27 10:54:51,536 - julearn - DEBUG - Creating a pipeline with no model added
2026-10-27 10:54:51,536 - julearn - DEBUG - Adding transformer pca
2026-10-27 10:54:51,536 - julearn - DEBUG -      Estimator: PCA(n_components=2)
2026-10-27 10:54:51,536 - julearn - DEBUG -      Params to tune: {}
2026-10-27 10:54:51,537 - julearn - DEBUG - Adding transformer pick_columns
2026-10-27 10:54:51,537 - julearn - DEBUG -      Estimator: PickColumns(keep='pca__pca0')
2026-10-27 10:54:51,537 - julearn - DEBUG -      Params to tune: {}
2026-10-27 10:54:51,537 - julearn - INFO - = Model Parameters =
2026-10-27 10:54:51,537 - julearn - INFO - ====================
2026-10-27 10:54:51,538 - julearn - INFO -
2026-10-27 10:54:51,538 - julearn - DEBUG - Pipeline created
2026-10-27 10:54:51,538 - julearn - DEBUG - Target generator pipeline created
2026-10-27 10:54:51,538 - julearn - DEBUG - Adding transformer zscore
2026-10-27 10:54:51,538 - julearn - DEBUG -      Estimator: StandardScaler()
2026-10-27 10:54:51,539 - julearn - DEBUG -      Params to tune: {}
2026-10-27 10:54:51,539 - julearn - DEBUG - Adding model linreg
2026-10-27 10:54:51,539 - julearn - DEBUG - Wrapping linreg
2026-10-27 10:54:51,539 - julearn - DEBUG -      Estimator: WrapModel(apply_to=ColumnTypes<types={'sepal'}; pattern=(?:__:type:__sepal)>,
          copy_X=True, fit_intercept=True, model=LinearRegression(),
          n_jobs=None, positive=False, tol=1e-06)
2026-10-27 10:54:51,541 - julearn - DEBUG -      Looking for nested pipeline creators
2026-10-27 10:54:51,541 - julearn - DEBUG -      Params to tune: {}
2026-10-27 10:54:51,541 - julearn - DEBUG - Wrapping target model linreg as target_generate
2026-10-27 10:54:51,541 - julearn - INFO - = Model Parameters =
2026-10-27 10:54:51,541 - julearn - INFO - ====================
2026-10-27 10:54:51,541 - julearn - INFO -
2026-10-27 10:54:51,542 - julearn - DEBUG - Pipeline created
2026-10-27 10:54:51,542 - julearn - DEBUG - Pipeline has target generator
2026-10-27 10:54:51,542 - julearn - INFO - = Data Information =
2026-10-27 10:54:51,543 - julearn - INFO -      Problem type: regression
2026-10-27 10:54:51,543 - julearn - INFO -      Number of samples: 150
2026-10-27 10:54:51,543 - julearn - INFO -      Number of features: 4
2026-10-27 10:54:51,543 - julearn - INFO - ====================
2026-10-27 10:54:51,543 - julearn - INFO -
2026-10-27 10:54:51,543 - julearn - INFO -      Target type: float64
2026-10-27 10:54:51,543 - julearn - INFO - Using outer CV scheme KFold(n_splits=2, random_state=None, shuffle=False) (incl. final model)
2026-10-27 10:54:51,549 - julearn - DEBUG - Setting column types for Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width'],
      dtype='str')
2026-10-27 10:54:51,550 - julearn - DEBUG -     Column mappers for {'sepal_length': 'sepal_length__:type:__sepal', 'sepal_width': 'sepal_width__:type:__sepal', 'petal_length': 'petal_length__:type:__petal', 'petal_width': 'petal_width__:type:__petal'}
2026-10-27 10:54:51,554 - julearn - DEBUG - Fitting the target generator
2026-10-27 10:54:51,554 - julearn - DEBUG - Setting column types for Index(['sepal_length__:type:__sepal', 'sepal_width__:type:__sepal',
       'petal_length__:type:__petal', 'petal_width__:type:__petal'],
      dtype='str')
2026-10-27 10:54:51,555 - julearn - DEBUG -     Column mappers for {'sepal_length': 'sepal_length__:type:__sepal', 'sepal_width': 'sepal_width__:type:__sepal', 'petal_length': 'petal_length__:type:__petal', 'petal_width': 'petal_width__:type:__petal'}
2026-10-27 10:54:51,563 - julearn - DEBUG - Generating target
2026-10-27 10:54:51,567 - julearn - DEBUG - Picking columns: ['pca__pca0']
2026-10-27 10:54:51,567 - julearn - DEBUG - Target generated: pca__pca0
2026-10-27 10:54:51,568 - julearn - DEBUG - Fitting model from generated target
2026-10-27 10:54:51,572 - julearn - DEBUG - Generating target
2026-10-27 10:54:51,575 - julearn - DEBUG - Picking columns: ['pca__pca0']
2026-10-27 10:54:51,575 - julearn - DEBUG - Target generated: pca__pca0
2026-10-27 10:54:51,575 - julearn - DEBUG - Scoring model using generated target
2026-10-27 10:54:51,580 - julearn - DEBUG - Setting column types for Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width'],
      dtype='str')
2026-10-27 10:54:51,580 - julearn - DEBUG -     Column mappers for {'sepal_length': 'sepal_length__:type:__sepal', 'sepal_width': 'sepal_width__:type:__sepal', 'petal_length': 'petal_length__:type:__petal', 'petal_width': 'petal_width__:type:__petal'}
2026-10-27 10:54:51,584 - julearn - DEBUG - Fitting the target generator
2026-10-27 10:54:51,584 - julearn - DEBUG - Setting column types for Index(['sepal_length__:type:__sepal', 'sepal_width__:type:__sepal',
       'petal_length__:type:__petal', 'petal_width__:type:__petal'],
      dtype='str')
2026-10-27 10:54:51,585 - julearn - DEBUG -     Column mappers for {'sepal_length': 'sepal_length__:type:__sepal', 'sepal_width': 'sepal_width__:type:__sepal', 'petal_length': 'petal_length__:type:__petal', 'petal_width': 'petal_width__:type:__petal'}
2026-10-27 10:54:51,593 - julearn - DEBUG - Generating target
2026-10-27 10:54:51,596 - julearn - DEBUG - Picking columns: ['pca__pca0']
2026-10-27 10:54:51,597 - julearn - DEBUG - Target generated: pca__pca0
2026-10-27 10:54:51,597 - julearn - DEBUG - Fitting model from generated target
2026-10-27 10:54:51,602 - julearn - DEBUG - Generating target
2026-10-27 10:54:51,605 - julearn - DEBUG - Picking columns: ['pca__pca0']
2026-10-27 10:54:51,606 - julearn - DEBUG - Target generated: pca__pca0
2026-10-27 10:54:51,606 - julearn - DEBUG - Scoring model using generated target
2026-10-27 10:54:51,610 - julearn - DEBUG - Setting column types for Index(['sepal_length', 'sepal_width', 'petal_length', 'petal_width'],
      dtype='str')
2026-10-27 10:54:51,611 - julearn - DEBUG -     Column mappers for {'sepal_length': 'sepal_length__:type:__sepal', 'sepal_width': 'sepal_width__:type:__sepal', 'petal_length': 'petal_length__:type:__petal', 'petal_width': 'petal_width__:type:__petal'}
2026-10-27 10:54:51,614 - julearn - DEBUG - Fitting the target generator
2026-10-27 10:54:51,615 - julearn - DEBUG - Setting column types for Index(['sepal_length__:type:__sepal', 'sepal_width__:type:__sepal',
       'petal_length__:type:__petal', 'petal_width__:type:__petal'],
      dtype='str')
2026-10-27 10:54:51,615 - julearn - DEBUG -     Column mappers for {'sepal_length': 'sepal_length__:type:__sepal', 'sepal_width': 'sepal_width__:type:__sepal', 'petal_length': 'petal_length__:type:__petal', 'petal_width': 'petal_width__:type:__petal'}
2026-10-27 10:54:51,624 - julearn - DEBUG - Generating target
2026-10-27 10:54:51,627 - julearn - DEBUG - Picking columns: ['pca__pca0']
2026-10-27 10:54:51,627 - julearn - DEBUG - Target generated: pca__pca0
2026-10-27 10:54:51,628 - julearn - DEBUG - Fitting model from generated target
2026-10-27 10:54:51,632 - julearn - DEBUG - Generating target
2026-10-27 10:54:51,635 - julearn - DEBUG - Picking columns: ['pca__pca0']
2026-10-27 10:54:51,635 - julearn - DEBUG - Target generated: pca__pca0
2026-10-27 10:54:51,635 - julearn - DEBUG - Scoring model using generated target
0   -2.368292
1   -0.722797
Name: test_score, dtype: float64

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

Gallery generated by Sphinx-Gallery