cerlymarco / linear-tree

A python library to build Model Trees with Linear Models at the leaves.
MIT License
338 stars 54 forks source link

How to gridsearch tree and regression parameters? #26

Closed zuzannakarwowska closed 1 year ago

zuzannakarwowska commented 1 year ago

Hi, I am wondering how to perform a GridsearchCV to find best parameters for the tree and regression model? For now I am able to tune the tree component of my model:

`

 param_grid={
    'n_estimators': [50, 100, 500, 700],
    'max_depth': [10, 20, 30, 50],
    'min_samples_split' : [2, 4, 8, 16, 32],
    'max_features' : ['sqrt', 'log2', None]
}
cv = RepeatedKFold(n_repeats=3,
                   n_splits=3,
                   random_state=1)

model = GridSearchCV(
    LinearForestRegressor(ElasticNet(random_state = 0), random_state=42),
    param_grid=param_grid,
    n_jobs=-1,
    cv=cv,
    scoring='neg_root_mean_squared_error'
    )

`

cerlymarco commented 1 year ago

Since ALL the estimators in linear-tree are sklearn stimators, you can use GridSearchCV according to the standard sklearn syntax.

parameters = {'base_estimator__alpha':[1, 5, 10], 'n_estimators':[50, 100, 500, 700]}
model = GridSearchCV(LinearForestRegressor(Ridge()), parameters, n_jobs=-1)
model.fit(X,y)

If you support the project, don't forget to leave a star ;-)