[Python] HalvingGridSearchCV cannot fit multi label DecisionTreeClassifier

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use HalvingGridSearch to find the best DecisionTree model. My model performs a multi-label prediction on a single example, it is trained on a batch of data of size (n_samples x n_features=9418) and has an output of size (n_samples x n_output_features=281). The output labels contain multiple classes for a given example and use one-hot encoding, so the vector is in this form: [0,0,1,0,1,...,1].

    tree_classifier = DecisionTreeClassifier(random_state=42)

    train_split = int(tree_train_fraction * len(labels))

    tree_inputs_train = tree_inputs[:train_split]
    tree_inputs_eval = tree_inputs[train_split:]
    labels_train = labels[:train_split]
    labels_eval = labels[train_split:]

    param_grid = {
    'max_depth': [ 2, 4, 6, 8, None],
    'min_samples_split': [2, 5, 10],
    'min_samples_leaf': [1, 2, 3, 4]
    }

    search = HalvingGridSearchCV(
    estimator = tree_classifier,
    param_grid = param_grid,
    factor = 2, # ogni iterazione dimezza il numero di candidati
    resource = 'n_samples',
    max_resources = 'auto',
    scoring = 'f1_micro',
    random_state = 42
    )

    search.fit(tree_inputs_train, labels_train)
    tree_classifier = search.best_estimator_


    it return the following error when attempting to fit the models: ValueError: y should be a 1d array, got an array of shape (154, 281) instead.

    I've tried fitting the same models using GridSearchCV and it doesn't return this error. Is it possible that there is a bug in the HalvingGridSearchCV implementation?

    full error traceback:


    Traceback (most recent call last): File "/home/gbenanti/Tesi_Benanti/tesi-magistrale-marchetti/DoctorXAI/statistics.py", line 828, in fidelity_synth, hit_synth, fidelity_synth_variance, hit_synth_variance = main(interval, ontological_perturbation, generative_perturbation) File "/home/gbenanti/Tesi_Benanti/tesi-magistrale-marchetti/DoctorXAI/statistics.py", line 801, in main tree, tree_inputs_encoded, model_labels_encoded = explain_label(synt_neigh_icd, synt_neigh_counts, neigh_labels_onehot, max_icd_id, tree_train_fraction, File "/home/gbenanti/Tesi_Benanti/tesi-magistrale-marchetti/DoctorXAI/statistics.py", line 232, in explain_label search.fit(tree_inputs_train, labels_train) File "/home/gbenanti/.venv/lib/python3.10/site-packages/sklearn/base.py", line 1389, in wrapper return fit_method(estimator, *args, **kwargs) File "/home/gbenanti/.venv/lib/python3.10/site-packages/sklearn/model_selection/_search_successive_halving.py", line 247, in fit self._check_input_parameters( File "/home/gbenanti/.venv/lib/python3.10/site-packages/sklearn/model_selection/_search_successive_halving.py", line 164, in _check_input_parameters y = validate_data(self, X="no_validation", y=y) File "/home/gbenanti/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py", line 2946, in validate_data out = _check_y(y, **check_params) File "/home/gbenanti/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py", line 1408, in _check_y y = column_or_1d(y, warn=True) File "/home/gbenanti/.venv/lib/python3.10/site-packages/sklearn/utils/validation.py", line 1485, in column_or_1d raise ValueError( ValueError: y should be a 1d array, got an array of shape (153, 281) instead.
    ​

    Continue reading...

Compartilhe esta Página