0
$\begingroup$

For a project where a classifier and a regressor are combined in an scikit-learn pipeline, the input variable has to be a list (or sth equivalent) of two pandas DataFrames. When it comes to hyperparameter tuning, we need GridSearchCV to accept an input in the same structure. For obvious reasons, scikit-learn's GridSearchCV only accepts an input X with the shape (n_samples, n_features).

Is there a functionality that generalizes this to the present case? Please find a small MWE attached.

import numpy as np
import pandas as pd

from sklearn.base import BaseEstimator, RegressorMixin
from sklearn.tree import DecisionTreeRegressor

from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV

rands = np.random.randint(100,size=(100))
data1 = np.array([np.repeat(range(25), 4), rands, rands, rands, rands]).T
data2 = ["Placeholder"]

df1 = pd.DataFrame(data1, columns=["id", "feat1", "feat2", "feat3", "target"])
df2 = pd.DataFrame(data2, columns=["placeholder"])

class customEstimator(BaseEstimator, RegressorMixin):
    def __init__(self, **kwargs):
        super().__init__()
        self.regression_params = kwargs

    def set_params(self, **kwargs):
        self.regression_params = kwargs
    
    def fit(self, X=None, y=None):
        model = DecisionTreeRegressor(**self.regression_params)
        model.fit(X, y)
        self.model = model

    def predict(self, X=[None, None]):
        return self.model.predict(X)

pipe = Pipeline(steps=[("regressor", customEstimator())])

X_1 = df1.drop("target", axis=1)
X_2 = df2
y = df1["target"]

# works
# pipe.fit(X=[X_1, X_2], y=y)
# pipe.predict([X_1, X_2])

param_grid={"regressor__max_depth":[1,2,3,4,5]}
clf = GridSearchCV(pipe, param_grid)
clf.fit(X=[X_1, X_2], y=y)

Thanks.

$\endgroup$
1
  • $\begingroup$ I'm surprised to hear that the pipe.fit works with a pair of frames for X! $\endgroup$ Aug 19 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.