-1

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

J A S K I E R
  • 1,976
  • 3
  • 24
  • 42

3 Answers3

1

Incorrect syntax:

     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"

Correct syntax:

    lambda path: "Enfj" if "enfj" in path 
else("Enfp" if "enfp" in path
else("Entj" if "entj" in path
else("Entp" if "entp" in path
else("Esfj" if "esfj" in path
else("Esfp" if "esfp" in path
else("Estj" if "estj" in path
else("Estp" if "estp" in path
else("Infj" if "Infj" in path
else("Infp" if "infp" in path
else("Intj" if "intj" in path
else("Intp" if "intp" in path
else("Isfj" if "isfj" in path
else("Isfp" if "isfp" in path
else("Istj" if "istj" in path
else "Istp")))))))))))))))
J A S K I E R
  • 1,976
  • 3
  • 24
  • 42
1

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
jpp
  • 159,742
  • 34
  • 281
  • 339
  • I give up. Finally, I have an issue of 104 if else statements and this is the only solution :) Thank you! However, could you please help to solve another similar example, because I don't see the connection between choices (16 paths/names... got this.) and DataFrame... Only 4? What does it mean A variable? https://stackoverflow.com/questions/51182760/python-stack-overflow-104-if-statements-is-defx-the-only-solution-to-opti – J A S K I E R Jul 05 '18 at 15:05
1

The issue here is that your function returns a string if the first evaluation is true, otherwise it returns a lambda function, as it does not call this function. Because of this a type error is thrown as a SFrame column cannot hold different types (string or function). I would highly recommend defining a long if else function and passing this to the apply or a similar, more efficient function.

jpp's code modified for simplicity and to use Turicreate

import turicreate as tc

sf = tc.SFrame({'path': ['enfpD', 'iNfp', 'sadintj', 'abc']})

choices = ['enfp', 'entj' , 'entp', 'esfj' , 'esfp',
           'estj', 'estp', 'infj', 'infp', 'intj',
           'intp', 'isfj', 'isfp', 'istj']

def changer(x):
    for choice in choices:
        if choice in x:
            return choice.capitalize() 
    return 'Istp'

sf['personalityType'] = sf['path'].apply(changer)

print(sf)

#+---------+-----------------+
#|   path  | personalityType |
#+---------+-----------------+
#|  enfpD  |       Enfp      |
#|   iNfp  |       istp      |
#| sadintj |       Intj      |
#|   abc   |       Istp      |
#+---------+-----------------+
  • I agree with you, but I'm just starting to learn Python and could you help me please to make it clear in a similar example of mine? Now I have much more samples, and It's a problem for if/else statement )) https://stackoverflow.com/questions/51182760/python-stack-overflow-104-if-statements-is-defx-the-only-solution-to-opti – J A S K I E R Jul 05 '18 at 15:07
  • @Oleksandr Even though you are using TuriCreate instead of Pandas the code in app's should still work. – computerstaat Jul 06 '18 at 20:37
  • could you please provide an example? – J A S K I E R Jul 06 '18 at 20:39
  • 1
    @Oleksandr I modified jpp's code to be simpler and to use turicreate. – computerstaat Jul 06 '18 at 20:56
  • where? Because I’ve found your similar topic of your: { x for x in list if x.split in output.split()} but I have only errors if to create list with all items: list=[„ars1”,”ars2”,...] – J A S K I E R Jul 06 '18 at 21:43
  • Its in the process of being peer reviewed, I'll just add it to my post – computerstaat Jul 06 '18 at 21:56
  • I've used this: https://stackoverflow.com/questions/50452760/turi-create-please-use-dropna-to-drop-rows/50456223#50456223 It helped a lot! Thank you! However, I don't see the logic of these random paths in SFrame. – J A S K I E R Jul 06 '18 at 22:11