Pendant le guide que j'ai suivi, j'ai une erreur: CoreMD utilisant Python
Besoin de créer un jeu de données simple en suivant le guide. La seule différence entre le guide est faite par moi:
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")
Au lieu de:
data["foodType"] = data["path"].apply(lambda path: "Rice" if "rice"
Le journal des erreurs dans le terminal:
python classifier.py
Traceback (dernier appel le plus récent): fichier "classifier.py", ligne 20, dans data.save ("ptype.sframe")
Fichier "/usr/local/lib/python2.7/site-packages/turicreate/data_structures/sframe.py", ligne 2808, dans save raise ValueError ("Format non pris en charge: {}". Format (format))
Fichier "/usr/local/lib/python2.7/site-packages/turicreate/cython/context.py", ligne 49, dans sortie augmenter exc_type (exc_value)
RuntimeError: exception dans l'évaluation de la fonction de rappel python:
TypeError ("Impossible de convertir le type 'fonction' en type flexible.",):
Traceback (dernier appel le plus récent): Fichier "turicreate / cython / cy_pylambda_workers.pyx", ligne 427, dans turicreate.cython.cy_pylambda_workers._eval_lambda File "turicreate / cython / cy_pylambda_workers.pyx", ligne 172, dans turicreate.cython.cy_pylambda_workers.lambda_evaluator.eval_simple
Fichier "turicreate / cython / cy_flexible_type.pyx", ligne 1306, dans turicreate.cython.cy_flexible_type.process_common_typed_list File "turicreate / cython / cy_flexible_type.pyx", ligne 1251, dans turicreate.cython.cy_flexible_type._fill_typed_sequence File "turicreate / cython / cy_flexible_type.pyx", ligne 1636, dans turicreate.cython.cy_flexible_type._ft_translateTypeError: impossible de convertir le type 'fonction' en type flexible.
Quel pourrait être le problème, car je ne peux pas exécuter mon classifier.py avec Python 2.7
3 réponses
Remplacez votre construction if
/ else
imbriquée par une fonction simple.
Voici un exemple:
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
Le problème ici est que votre fonction renvoie une chaîne si la première évaluation est vraie, sinon elle renvoie une fonction lambda, car elle n'appelle pas cette fonction. Pour cette raison, une erreur de type est levée car une colonne SFrame ne peut pas contenir différents types (chaîne ou fonction). Je recommande fortement de définir une fonction longue sinon sinon et de la transmettre à l'appliquer ou à une fonction similaire plus efficace.
Le code de jpp a été modifié pour plus de simplicité et pour utiliser 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 |
#+---------+-----------------+
Syntaxe incorrecte:
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"
Syntaxe correcte:
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")))))))))))))))
Questions connexes
De nouvelles questions
python
Python est un langage de programmation multi-paradigme, typé dynamiquement et polyvalent. Il est conçu pour être rapide à apprendre, comprendre, utiliser et appliquer une syntaxe propre et uniforme. Veuillez noter que Python 2 est officiellement hors support à partir du 01-01-2020. Néanmoins, pour les questions Python spécifiques à la version, ajoutez la balise [python-2.7] ou [python-3.x]. Lorsque vous utilisez une variante Python (par exemple, Jython, PyPy) ou une bibliothèque (par exemple, Pandas et NumPy), veuillez l'inclure dans les balises.