【问题标题】:Scikit Learn, Recognizing hand-written digits, mistake in given code exampleScikit Learn,识别手写数字,给定代码示例中的错误
【发布时间】:2017-06-18 02:24:43
【问题描述】:

我正在运行 Scikit 学习页面 (click here for exact code) 上“识别手写数字”代码中的代码。

当我运行以下行时:

classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2])

我的终端出现以下错误:

Traceback (most recent call last):
  File "plot_digits_classification.py", line 35, in <module>
    classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2])
TypeError: slice indices must be integers or None or have an __index__ method

为什么会这样?如何让代码示例工作?

【问题讨论】:

  • 尝试用int(n_samples / 2)替换n_samples / 2

标签: python machine-learning scikit-learn


【解决方案1】:

在 Python 2 上,带有 int 参数的运算符 / 将返回 int。在 Python 3 中,运算符 / 将始终返回 float,即使使用 int 参数也是如此。要使用 Python 3 获取 int,请使用运算符 //,例如 6//3

所以你的代码是:

classifier.fit(data[:n_samples // 2], digits.target[:n_samples // 2])

等等。

【讨论】:

    猜你喜欢
    • 2012-12-08
    • 1970-01-01
    • 2016-08-28
    • 2012-12-09
    • 2019-07-17
    • 2019-04-13
    • 1970-01-01
    • 2013-06-05
    • 2015-08-25
    相关资源
    最近更新 更多