how to pass string y label validation for TransformedTargetRegressor?

Viewed 52

I need help in transforming target y label.

Currently, I'm working on an NLP prediction labeling. I'm using sklearn pipeline to apply list of transformers (*using tfidf vectorizer) and a final estimator (*using linearSVC classification) in the pipeline, then using TransformedTargetRegressor to transform the target y (*using MultiLabelBinarizer).

It works well when i just use the pipeline without TransformedTargetRegressor to fit and predict the label (i transform and inverse_transform the target y manually). But since I need to bundle all them up end to end, I use TransformedTargetRegressor to do the target y transformation, which returning some error:

ValueError                                Traceback (most recent call last)
~/miniconda3/lib/python3.9/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
    778             try:
--> 779                 array = array.astype(np.float64)
    780             except ValueError as e:

ValueError: could not convert string to float: 'account'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
/var/folders/2b/cyjjz4sn4fq5vz72qz2qcnvh9dlt67/T/ipykernel_74402/135390795.py in <module>
     18 
     19 with mlflow.start_run(run_name="TFIDF + LinearSVC + with Pipeline and column"):
---> 20     regr.fit(X_train[:1], tesst_y)
     21     mlflow.set_tag('model','NLP')
     22     #mlflow.log_metric('j_score', j_score(y_test, y_pred))

~/miniconda3/lib/python3.9/site-packages/sklearn/compose/_target.py in fit(self, X, y, **fit_params)
    208             Fitted estimator.
    209         """
--> 210         y = check_array(
    211             y,
    212             accept_sparse=False,

~/miniconda3/lib/python3.9/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
    779                 array = array.astype(np.float64)
    780             except ValueError as e:
--> 781                 raise ValueError(
    782                     "Unable to convert array of bytes/strings "
    783                     "into decimal numbers with dtype='numeric'"

ValueError: Unable to convert array of bytes/strings into decimal numbers with dtype='numeric'

From the error message, says that the error comes from the target y. Here's a glimpse of the data:

array([['account', 'change_number'],
       ['apple', 'banana']], dtype='<U13')

So, I assume that the error comes from the y label is not in numeric, then I tried using numeric y label (I'm using dummy data here), like this and it works:

array([[1, 5],
       [2, 6]])

Looks like TransformedTargetRegressor need the target y label in numeric (i've observe the validation.py in the source code as well). Is there any posibilities for the target y label to be in string? or did I miss anything here to make it happen?

0 Answers
Related