[Python] Evaluate transformations with the same model in scikit-learn

Discussão em 'Python' iniciado por Stack, Agosto 27, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to perform a regression analysis and test different transformations of the input variables for the same model. To accomplish this, I created a dictionary with the different pipelines, which I loop through:

    import numpy as np
    import pandas as pd
    from sklearn.preprocessing import FunctionTransformer
    from sklearn.linear_model import LinearRegression
    from sklearn.pipeline import make_pipeline
    from sklearn.metrics import mean_squared_error
    from sklearn.preprocessing import PowerTransformer
    from sklearn.compose import TransformedTargetRegressor

    # Define transformations and models
    models = {
    'linear': LinearRegression(),
    'power': make_pipeline(PowerTransformer(), LinearRegression()),
    'log': make_pipeline(FunctionTransformer(np.log, np.exp),
    LinearRegression()),
    'log-sqrt': TransformedTargetRegressor(
    regressor=make_pipeline(
    FunctionTransformer(np.log, np.exp),
    LinearRegression()),
    func=np.sqrt,
    inverse_func=np.square
    )
    }

    parameters = pd.DataFrame()
    for name, model in models.items():
    model.fit(x_train, y_train)
    y_hat = model.predict(x_hat)
    y_hat_train = model.predict(x_train)
    r2 = model.score(x_train, y_train)
    parameters.at[name, 'MSE'] = mean_squared_error(y_train, y_hat_train)
    parameters.at[name, 'R2'] = r2
    best_model = parameters['R2'].idxmax()


    This works. However, there is probably a more elegant solution similar to GridSearchCV for evaluating models. Can anyone give me a tip on what I should look for?

    Continue reading...

Compartilhe esta Página