【问题标题】:How to fix syntax error in this beginner Python AI code?如何修复这个初学者 Python AI 代码中的语法错误?
【发布时间】:2019-04-11 22:26:12
【问题描述】:

让初学者 Ai 从重量和质地来区分苹果和橙子,并且标签上有语法错误 代码如下:

from sklearn import tree

## In Features 1 = Smooth, 0 = Bumpy

features = [[140, 1], [130, 1], [150, 0], [170, 0]
labels = ["apple", "apple", "orange", "orange"]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(features, labels)
print clf.predict([[150, 0]])

【问题讨论】:

  • 查看如何创建minimal reproducible example
  • 错误在哪一行?您可以删除哪些行但仍然出现错误?将代码减少到您仍然不明白问题所在的最低限度。
  • features 的嵌套列表缺少右括号。

标签: python python-2.7 artificial-intelligence


【解决方案1】:

您缺少]

from sklearn import tree

    ## In Features 1 = Smooth, 0 = Bumpy

    features = [[140, 1], [130, 1], [150, 0], [170, 0]] # missed an ] here
    labels = ["apple", "apple", "orange", "orange"]
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(features, labels)
    print clf.predict([[150, 0]])

【讨论】:

  • 这是由于输入错误而关闭的原因;该问题不符合正常答案的条件。
【解决方案2】:

您在运行 Python 脚本时遇到了什么错误?

例如:

>>> x = [ ["x", y"]

给我:

SyntaxError: EOL while scanning string literal

甚至指向它失败的那一行。你试过谷歌搜索吗?我觉得您正在尝试学习有关 AI 的一些知识,但是您对这里的语言还是陌生的。

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多