运行以下代码时出现错误,该如何解决?
错误:
csv.Error:可迭代,不是numpy.int64
y_test = np.array(test_labels)
print('y_test_labels:', y_test.shape) # (230,123)
with open('/content/drive/My Drive/GEINet_and_PEINet/VGG_CSV/test.csv', mode='w') as employee_file:
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
#employee_writer.writerow(y_test)
x,y = y_test.shape
print('x: ',x)
for num in range(0, x):
employee_writer.writerows(y_test[num,:])
尝试此方法employee_writer.writerows(list(y_test[num,:]))
您应该使y_test[num,:]
迭代,将其修改为可以用y_test[num,:]
替换map(lambda x: [x], y_test[num,:])
的列表序列。另一种方法是代替NumPy数组y_test
,而使用pandas test_labels
中的to_csv()
方法从Series
数据帧中迭代出来。