关于float对象的错误消息不能解释为整数

时间:2017-03-11 22:10:35

标签: python python-3.x numpy scipy

我使用以下代码行来定义一个多维数组,以保存一组表示为二维数组的图像。

 import numpy as np
 imgs = np.ndarray((100, 1, image_rows, image_cols), dtype=np.float32)

此处,100表示总共有100张图片。 但是,运行该程序会出现以下错误消息TypeError: 'float' object cannot be interpreted as an integer。它是什么意思以及如何解决它?

1 个答案:

答案 0 :(得分:0)

如果image_rowsimage_cols是浮点值,您将收到该错误:

In [15]: imgs = np.ndarray((100, 1, 5.0, 10.0), dtype=np.float32)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-c7783d157b42> in <module>()
----> 1 imgs = np.ndarray((100, 1, 5.0, 10.0), dtype=np.float32)

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

首先将值转换为整数:

imgs = np.ndarray((100, 1, int(image_rows), int(image_cols)), dtype=np.float32)