Search code examples
python-3.xpandasscikit-learnspacysklearn-pandas

Beginner: TypeError: <lambda>() got an unexpected keyword argument 'func_names'


df['Pattern'] = df['phrases'].apply(lambda texte:Preprocess, func_names=['tokenizeTexte_0'])

import TreeTagger OK
Traceback (most recent call last):
  File "classifier.py", line 41, in <module>
    df['Pattern'] = df['phrases'].apply(lambda texte:Preprocess, func_names=['tokenizeTexte_0']) # modify the parameter each time you want to change the preprocess steps
  File "/home/ke/anaconda3/lib/python3.7/site-packages/pandas/core/series.py", line 3194, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/_libs/src/inference.pyx", line 1472, in pandas._libs.lib.map_infer
  File "/home/k/anaconda3/lib/python3.7/site-packages/pandas/core/series.py", line 3181, in <lambda>
    f = lambda x: func(x, *args, **kwds)
TypeError: <lambda>() got an unexpected keyword argument 'func_names'

The file looks like this (it is an example since , i cannot put the original data)

id    phrases                       etiquette    sous-classe
23    C'est un psychiatre canadien.  Med          p
56    le Pr. Moldofsky, qui a fait   Med          n
émerger en 1975 cette maladie, 
45    en identifiant des plaintes    Fed          ne
78équivalentes par privation de sommeil 
chez des patientes volontaires. 
789    Reconnue Outre-Atlantique,    Ged          p
elle reste peu connue dans l'Hexagone. 


Solution

  • A generic way to do this is to store the functions in a dictionary where you use keys to find the function you want. Here is an example below where I create a function combine_functions which takes a list of strings as an argument. This allows you to pick which function should be run in which order. Technically, this also allows you to run the same function multiple times.

    def func1(x):
    
        print("I am func1")
    
        return x
    
    
    def func2(x):
    
        print("I am func2")
    
        return x
    
    
    def func3(x):
    
        print("I am func3")
    
        return x
    
    
    def combine_functions(x, func_names=[]):
    
        functions = {"func1": func1,
                     "func2": func2,
                     "func3": func3}
    
        for func_name in func_names:
    
            x = functions[func_name](x)
    
        return x