tudou
tudou

Reputation: 515

How to Pass tuple lists to a function arguments in python

I have the following code which works ok:

def func1():
    return Pipeline([('cont_inf_replacer', replaceInf()),
                     ])

make_column_transformer((func1(), features['cat1']),
                         (func2(), features['cat2']),
                         (func3(), features['cat3'])
)

Now, I would like to pass the function argument as a variable

func_dict = {'cat1': func1, 'cat2':func2, 'cat3': func3}
for c in features.keys():
    arg_tuple += (func_dict[c], features[c])

 make_column_transformer(arg_tuple)     

I would expect arg_tuple should expand/unpack into

                         func1(), features['cat1']),
                         (func2(), features['cat2']),
                         (func2(), features['cat3'])

But received the following error. I did a search and could not find the proper solution

 ValueError: not enough values to unpack (expected 2, got 1)

This is how make_column_transformer() is defined:

make_column_transformer(*transformers, **kwargs)

Unpack with *arg_tuple seems to work (no error when call make_column_transfer, but the results are different, see below)

make_column_transformer((func1(),features['_cat_'])): output

ColumnTransformer(n_jobs=None, remainder='drop', sparse_threshold=0.3,
     transformer_weights=None,
     transformers=[('pipeline', Pipeline(memory=None,
 steps=[('cont_inf_replacer', <__main__.replaceInf object at 0x7f87d19d6ba8>)]), ['cat1', 'cat2', 'cat3'])])

With *arg_tuple,

make_column_transformer(*arg_tuple)

ColumnTransformer(n_jobs=None, remainder='drop', sparse_threshold=0.3,
     transformer_weights=None,
     transformers=[('function', <function _pip_cont at 0x7f87d1a006a8>, ['cat1', 'cat2', 'cat3'])])

Upvotes: 0

Views: 576

Answers (3)

Enxi
Enxi

Reputation: 111

  1. make_column_transformer(*arg_tuple) will unpack ((),(),()) correctly
  2. You should instead try the following

    pipline_step['cat1'] = [('test_pipeline', Pipeline([SimpleImputer(xxxxxxxx)]))] list_pipeline = Pipeline() list_pipeline.steps.append(pipeline)

So in this way, instead of a for loop, you can attach the steps depending on the availability of the subsets (in this example, cat1 is available). And you can pass the subset name as a key as the pipeline steps.

Upvotes: 1

Frnndo
Frnndo

Reputation: 21

Try with this:

def tupler(txt):
  x = eval('tuple(' + str(txt) + ')')
  print len(x)

tuple1 = (1,2,3,4,5)
string1 = str(tuple1)

tupler(string1)

Eval function is the trick.

For more details about eval function, click here

Upvotes: 0

tudou
tudou

Reputation: 515

Per @onyambu, *arg_tuple solved the issue

so the following code works

make_column_transfer(*arg_tuple)

This will unpack a "list" of tuples and pass it to the function *transformers

Upvotes: 0

Related Questions