我使用以下代码行来定义一个多维数组,以保存一组表示为二维数组的图像。
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
。它是什么意思以及如何解决它?
答案 0 :(得分:0)
如果image_rows
或image_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)