首页 > 解决方案 > 'float' 对象不能被解释为整数 python

问题描述

我正在关注 Keras 中的数据生成器教程:

https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly

在 __data_generation 函数中,他们创建了一个空的 numpy 数组 X。但是,当我这样做时,我收到以下错误:

TypeError:'float' 对象不能解释为整数

我输入的维度是 (1,7000,208) 和 (1,7000)。但是,当我尝试制作 numpy 数组时,我得到了我之前提到的错误。这就是我所做的:


dim_snp = (1,7000,208)
dim_pos = (1,7000)

X_snp = np.empty([1,dim_snp,1])
X_pos = np.empty((1,dim_pos))

有人可以解释为什么我会收到这个错误吗?

标签: python-3.xnumpy

解决方案


In [23]: dim_snp = (1,7000,208)                                                 
In [24]: (1, dim_snp, 1)                                                        
Out[24]: (1, (1, 7000, 208), 1)

In [25]: np.zeros((1, dim_snp, 1))                                              
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-25-10a1e967522d> in <module>
----> 1 np.zeros((1, dim_snp, 1))

TypeError: 'tuple' object cannot be interpreted as an integer

In [26]: np.zeros((1, 1, 700, 208, 1)).shape                                    
Out[26]: (1, 1, 700, 208, 1)

推荐阅读