0

I am getting an error while running the following code, how do I fix it?

Error:

csv.Error: iterable expected, not 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,:])
  • u can directly dump numpy array to csv file, see if this helps, https://stackoverflow.com/questions/6081008/dump-a-numpy-array-into-a-csv-file – sushanth May 25 '20 at 14:31

4 Answers4

0

Try this instead employee_writer.writerows(list(y_test[num,:]))

0

You should make y_test[num,:] iterable, modifying it to be a sequence of lists you can replace y_test[num,:] with map(lambda x: [x], y_test[num,:]). Another way is instead of making a NumPy arrayy_test, making an iterable out of the test_labels data frame using to_csv() method from pandasSeries from scratch.

13leak
  • 53
  • 7
0

You can call writerows directly on the array:

employee_writer.writerows(y_test)
Milan Cermak
  • 5,444
  • 2
  • 36
  • 51
0

You can just do this:

with open('/content/drive/My Drive/GEINet_and_PEINet/VGG_CSV/test.csv', mode='w') as employee_file:
    employee_writer.writerows(y_test)
Pratik Joshi
  • 179
  • 1
  • 11