Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridSearchCV gives ValueError: continuous is not supported for DecisionTreeRegressor

I'm learning ML and doing the task for Boston house price predictions. I have following code:

from sklearn.metrics import fbeta_score, make_scorer
from sklearn.model_selection import GridSearchCV

def fit_model(X, y):
    """ Tunes a decision tree regressor model using GridSearchCV on the input data X 
        and target labels y and returns this optimal model. """

    # Create a decision tree regressor object
    regressor = DecisionTreeRegressor()

    # Set up the parameters we wish to tune
    parameters = {'max_depth':(1,2,3,4,5,6,7,8,9,10)}

    # Make an appropriate scoring function
    scoring_function = make_scorer(fbeta_score, beta=2)

    # Make the GridSearchCV object
    reg = GridSearchCV(regressor, param_grid=parameters, scoring=scoring_function)

    print reg
    # Fit the learner to the data to obtain the optimal model with tuned parameters
    reg.fit(X, y)

    # Return the optimal model
    return reg.best_estimator_

reg = fit_model(housing_features, housing_prices)

This gives me ValueError: continuous is not supported for the reg.fit(X, y) line and I don't understand why. What is the reason for this, what am I missing here?

like image 878
wasd Avatar asked Mar 08 '23 10:03

wasd


1 Answers

That's because of the line:

scoring_function = make_scorer(fbeta_score, beta=2)

This sets the scoring-metric to fbeta, which is for classification tasks!

Your are doing regression here as seen in:

regressor = DecisionTreeRegressor()

From the docs

enter image description here

like image 109
sascha Avatar answered Apr 30 '23 17:04

sascha