TypeError:“ float”对象无法解释为索引

iMa*_*rix 3 python deep-learning keras

我遇到的问题以前从未发生过,也许某些规则已更改。

    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
Run Code Online (Sandbox Code Playgroud)

Hor*_*man 6

问题可能出在rangePython附带的函数上。它的参数必须是整数。乘以n_train时会变成浮点数。您只需要将其转换为int即可。这实际上是正确的做法。如果您有示例,那么进行培训和测试示例将毫无意义。n_examples0.5int(n_examples * 0.5)115.5

  • 如果这是正确的答案,请将其标记为这样,以便Horia可以收获他的甜美声誉。 (2认同)