Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';浮动';对象不能解释为索引_Python_Deep Learning_Keras - Fatal编程技术网

Python 类型错误:';浮动';对象不能解释为索引

Python 类型错误:';浮动';对象不能解释为索引,python,deep-learning,keras,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(ran

我面临着一个以前从未发生过的问题,也许有些规则已经改变了

    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
回溯(最近一次呼叫上次)
在()
5 n_示例=X.shape[0]
6 n_系列=n_示例*0.5
---->7列idx=np.random.choice(范围(0,n个示例),大小=n列,替换=False)
8测试idx=列表(集合(范围(0,n个示例))-集合(序列idx))
9列车=X[列车idx]
mtrand.RandomState.choice中的mtrand.pyx(numpy/random/mtrand/mtrand.c:18822)()
TypeError:“float”对象不能解释为索引

问题可能在于Python附带的
range
函数。它的参数必须是整数<当
n\u示例
乘以
0.5时,code>n\u列
变为浮点。您只需将其重新转换为int,如
int(n_examples*0.5)
。这实际上是正确的做法。如果你有
11个
示例,那么让
5.5
训练和测试示例是没有意义的。

只是猜测,但是
n_-train=n_-examples*0.5
可能不是int。试着做:
n_-train=int(n_-examples*0.5)
?如果这是正确答案,请将其标记为这样,以便Horia可以收获他的美誉点数。