-1

在我遵循的指南中,我遇到了一个错误:CoreMD using Python

需要按照指南创建一个简单的数据集。指南之间的唯一区别是由我制作的:

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")

代替:

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

终端中的错误日志:

python分类器.py

Traceback(最近一次调用最后一次):文件“classifier.py”,第 20 行,在 data.save("ptype.sframe")

文件“/usr/local/lib/python2.7/site-packages/turicreate/data_structures/sframe.py”,第 2808 行,在保存中引发 ValueError("Unsupported format: {}".format(format))

文件“/usr/local/lib/python2.7/site-packages/turicreate/cython/context.py”,第 49 行,退出 raise exc_type(exc_value)

RuntimeError:python 回调函数评估中的异常:

TypeError("无法将类型 'function' 转换为灵活类型。",):

回溯(最后一次调用):文件“turicreate/cython/cy_pylambda_workers.pyx”,第 427 行,位于 turicreate.cython.cy_pylambda_workers._eval_lambda 文件“turicreate/cython/cy_pylambda_workers.pyx”,第 172 行,位于 turicreate.cython.cy_pylambda_workers .lambda_evaluator.eval_simple
文件“turicreate/cython/cy_flexible_type.pyx”,第 1306 行,在 turicreate.cython.cy_flexible_type.process_common_typed_list 文件“turicreate/cython/cy_flexible_type.pyx”,第 1251 行,在 turicreate.cython.cy_flexible_type._fill_typed_sequence 文件中“ turicreate/cython/cy_flexible_type.pyx”,第 1636 行,在 turicreate.cython.cy_flexible_type._ft_translate

TypeError:无法将类型“函数”转换为灵活类型。

问题可能是什么,因为我无法使用 Python 2.7 运行我的 classifier.py

4

3 回答 3

1

语法错误:

     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"

正确的语法:

    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")))))))))))))))
于 2018-04-15T22:24:23.823 回答
1

用一个简单的函数替换您的嵌套if/构造。else

下面是一个例子:

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
于 2018-04-16T08:00:19.000 回答
1

这里的问题是,如果第一个评估为真,则您的函数返回一个字符串,否则它返回一个 lambda 函数,因为它不调用此函数。因此,由于 SFrame 列不能容纳不同的类型(字符串或函数),因此会引发类型错误。我强烈建议定义一个 long if else 函数并将其传递给 apply 或类似的更高效的函数。

jpp 的代码为了简单而修改并使用 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      |
#+---------+-----------------+
于 2018-05-18T20:54:46.117 回答