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可以收获他的美誉点数。