【问题标题】:TypeError: 'float' object cannot be interpreted as an indexTypeError:“浮动”对象不能解释为索引
【发布时间】:2017-03-29 07:05:02
【问题描述】:

我遇到了一个以前没有发生过的问题,可能是某些规则已更改。

    Traceback (most recent call last)
    <ipython-input-3-f47687e192a7> in <module>()
          5 n_examples = X.shape[0]
          6 n_train = n_examples * 0.5
    ----> 7 train_idx = np.random.choice(range(0,n_examples), size=n_train, replace=False)
          8 test_idx = list(set(range(0,n_examples))-set(train_idx))
          9 X_train = X[train_idx]

    mtrand.pyx in mtrand.RandomState.choice (numpy/random/mtrand/mtrand.c:18822)()

    TypeError: 'float' object cannot be interpreted as an index

【问题讨论】:

  • 只是猜测,但n_train = n_examples * 0.5 可能不是整数。尝试做:n_train = int(n_examples * 0.5)?

标签: python deep-learning keras


【解决方案1】:

问题可能出在 Python 附带的 range 函数上。它的参数必须是整数。当n_examples 乘以0.5 时,n_train 变为浮点数。您只需要将其重新转换为 int(n_examples * 0.5) 之类的 int。这实际上是正确的做法。如果您有 11 示例,那么拥有 5.5 训练和测试示例就没有意义。

【讨论】:

  • 如果这是正确答案,请将其标记为正确答案,以便霍里亚获得他的美誉。
猜你喜欢
  • 2018-12-15
  • 1970-01-01
  • 2018-05-14
  • 1970-01-01
  • 2021-12-05
  • 2017-10-11
  • 2021-03-03
  • 2015-06-18
  • 2015-04-01
相关资源
最近更新 更多