Python 我已将我的类型更改为int,但仍然得到;用作索引的数组必须是整数(或布尔)类型;
我发现很多人通过向numpy数组中添加astype(int)来解决这个问题,我也尝试过,但没有成功。这是我的密码Python 我已将我的类型更改为int,但仍然得到;用作索引的数组必须是整数(或布尔)类型;,python,numpy,Python,Numpy,我发现很多人通过向numpy数组中添加astype(int)来解决这个问题,我也尝试过,但没有成功。这是我的密码 print('----loading data----') t0 = time.time() f1 = 'D:/building/big data/not test' def get_imlist(path): return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
print('----loading data----')
t0 = time.time()
f1 = 'D:/building/big data/not test'
def get_imlist(path):
return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
imagePath1 = get_imlist(f1)
test_N = len(imagePath1)
test_data = np.ones((test_N,256,256,3),dtype='float64')
def loadIMG(imagePath , number, Array):
while number > 0:
img = cv2.imread(imagePath[number-1])
img = cv2.resize(img,(256,256),interpolation=cv2.INTER_AREA)
img_ndarray=np.asarray(img,dtype=int)
Array[number-1] = img_ndarray
number = number - 1
loadIMG(imagePath1, test_N, test_data)
test_data = test_data/255
test_data.astype(int)
print('---finish loading----')
t1 = time.time()
print('time:'+ str(round((t1-t0),4))+ ' sec')
print('----start predicting----')
t2 = time.time()
#load model
autoencoder = load_model('segnet2_outputgray_v3.h5')
for i in test_data:
result = autoencoder.predict(test_data[i])
a=result[0]
ret, th1 = cv2.threshold(a, np.mean(result), 255 , 0)
th1 = cv2.cvtColor(th1,cv2.COLOR_GRAY2RGB)
imgstack = np.hstack((test_data, th1))
cv2.imwrite('D:/building/big data/predict/'+str(i)+'.jpg', imgstack)
i = i + 1
t3 = time.time()
print('----finish predicting----')
错误发生在这一行
result = autoencoder.predict(test_data[i])
还有索引器
IndexError: arrays used as indices must be of integer (or boolean) type
当我将numpy ones数组创建为float64时,我专门定义了数据类型。即使我将dytpe更改为int,它也无法工作。错误读取
用作索引的数组必须是整数(或布尔)类型
然后你说
我在创建numpy ones数组到float64时专门定义了数据类型,所以我不知道发生了什么
你不知道发生了什么事
正如您自己所说的,您显式地将数据类型定义为float64,这就是您的问题
float64顾名思义是“float”,而不是“int”。Numpy需要一个整数或整数数组作为索引,不能与float64一起使用。对不起,删除它后我仍然收到相同的错误。错误仍然在那一行@楊尚峰 那么,您试图用作索引的数组的数据类型是什么?我无法从你的密码判断。您需要确保您使用的是Numpy允许的索引,即整数或布尔。试着去做;```打印(i.dtype)``就在尝试使用“i”作为索引的行上方。
test\u data.astype(int)
生成一个新数组。它不会更改测试数据。你自己检查一下。