Home > Mobile >  Fine-tune hyperparameters of a FunctionTransformer in a pipeline
Fine-tune hyperparameters of a FunctionTransformer in a pipeline

Time:10-03

I have a function here I would like to fine tune with the 'num_corr_threshold' parameter :

def categorical_anticorr(X_cat_in,num_corr_threshold=0.5):
    if type(X_cat_in) == np.ndarray:
        X_cat_in = pd.DataFrame(X_cat_in)
    elif type(X_cat_in) == type(csr_matrix(0)):
        X_cat_in = pd.DataFrame(X_cat_in.toarray())
    corr_num = X_cat_in.corr(method='spearman')
    upper = corr_num.where(np.triu(np.ones(corr_num.shape), k=1).astype(bool)).abs()
    col_to_drop = [column for column in upper.columns if any(upper[column] > num_corr_threshold)]
    return X_cat_in.drop(columns=col_to_drop)

I inserted this function in a complex pipeline named 'preproc'.

In order to find the name of the parameter to fine tune, I used the command : 'preproc.get_params()'.

I fand the parameter in the list, it was :

'columntransformer__pipeline-2__functiontransformer__func': <function main.categorical_anticorr(X_cat_in, num_corr_threshold=0.5)>

After I tried to insert this hyperparam in my grid with the command :

param_grid = {'columntransformer__pipeline2__functiontransformer__func': <function main.categorical_anticorr(X_cat_in, num_corr_threshold=0.5)>}

Unfortunately, I got the error message : SyntaxError: invalid syntax.

Please, does anybody know the correct syntax to fine tune for example the 'num_corr_threshold' parameter of the categorical_anticorr function ?

Thanks in advance.

Best regards.

CodePudding user response:

I have found the solution to my problem, the good syntax was :

param_grid = {'columntransformer__pipeline-2__functiontransformer__kw_args': [{'num_corr_threshold': 0.0}]}

I found the solution on this topic : How can I make the FunctionTransformer along with GridSearchCV into a pipeline?

Thank you !

  • Related