pythonpython-2.7coremlturi-create

Python 2.7 several conditions for lambda? Elif?


During the guide I followed from I have an error: CoreMD using Python

Need to create a simple dataset following the guide. The only difference between guide is made by me:

data["personalityType"] = data["path"].apply(     lambda path: "Enfj" if "enfj" in path
                                             else lambda path: "Enfp" if "enfp" in path
                                             else lambda path: "Entj" if "entj" in path
                                             else lambda path: "Entp" if "entp" in path
                                             else lambda path: "Esfj" if "esfj" in path
                                             else lambda path: "Esfp" if "esfp" in path
                                             else lambda path: "Estj" if "estj" in path
                                             else lambda path: "Estp" if "estp" in path
                                             else lambda path: "Infj" if "Infj" in path
                                             else lambda path: "Infp" if "infp" in path
                                             else lambda path: "Intj" if "intj" in path
                                             else lambda path: "Intp" if "intp" in path
                                             else lambda path: "Isfj" if "isfj" in path
                                             else lambda path: "Isfp" if "isfp" in path
                                             else lambda path: "Istj" if "istj" in path
                                             else "Istp")

instead of:

data["foodType"] = data["path"].apply(lambda path: "Rice" if "rice"

The error log in Terminal:

python classifier.py

Traceback (most recent call last): File "classifier.py", line 20, in data.save("ptype.sframe")

File "/usr/local/lib/python2.7/site-packages/turicreate/data_structures/sframe.py", line 2808, in save raise ValueError("Unsupported format: {}".format(format))

File "/usr/local/lib/python2.7/site-packages/turicreate/cython/context.py", line 49, in exit raise exc_type(exc_value)

RuntimeError: Exception in python callback function evaluation:

TypeError("Cannot convert type 'function' into flexible type.",):

Traceback (most recent call last): File "turicreate/cython/cy_pylambda_workers.pyx", line 427, in turicreate.cython.cy_pylambda_workers._eval_lambda File "turicreate/cython/cy_pylambda_workers.pyx", line 172, in turicreate.cython.cy_pylambda_workers.lambda_evaluator.eval_simple
File "turicreate/cython/cy_flexible_type.pyx", line 1306, in turicreate.cython.cy_flexible_type.process_common_typed_list File "turicreate/cython/cy_flexible_type.pyx", line 1251, in turicreate.cython.cy_flexible_type._fill_typed_sequence File "turicreate/cython/cy_flexible_type.pyx", line 1636, in turicreate.cython.cy_flexible_type._ft_translate

TypeError: Cannot convert type 'function' into flexible type.

What the problem could be, because I can't run my classifier.py with Python 2.7


Solution

  • Replace your nested if / else construct with a simple function.

    Below is an example:

    import pandas as pd, numpy as np
    
    df = pd.DataFrame({'A': ['enfpD', 'iNfp', 'sadintj', 'abc']})
    
    choices = {'enfp', 'entj' , 'entp', 'esfj' , 'esfp',
               'estj', 'estp', 'infj', 'infp', 'intj',
               'intp', 'isfj', 'isfp', 'istj'}
    
    def changer(x):
        match = next((c for c in choices if c in x), None)
        if match:
            return match.title()
        else:
            return 'Istp'
    
    df['A'] = df['A'].apply(changer)
    
    print(df)
    
    #       A
    # 0  Enfp
    # 1  Istp
    # 2  Intj
    # 3  Istp