79127482

Date: 2024-10-25 22:41:21
Score: 1
Natty:
Report link

This is the correct sklearn method, as far as I know. Reference: https://www.youtube.com/watch?v=v2QpvCJ1ar8

Trick: Treat Model Class as hyperparameter to tune

from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
## create the Pipeline
pipe = Pipeline(
  [
      ('model', None) # Set None as placeholder
  ]
)

params = [
  dict(
    model = [LinearRegression()]
    model__penalty = ['l1', 'l2'],
  ),
  dict(
    model = [RandomForestRegressor()]
    model__n_estimators = [100, 200],
  )
]
grid = GridSearchCV(
  pipe,
  param_grid = params
)

grid.fit(X, y)
import pandas as pd
results = pd.DataFrame(grid.cv_results_)
Reasons:
  • Blacklisted phrase (1): youtube.com
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Ahmed Thahir