8

I have a 3D Numpy array with the shape [1953,949,13]. I want to write it to a CSV file where each row should contain a 2D array of shape [949 13] and csv file should contain 1953 rows. I tried np.savetext and it supports only 1D and 2D arrays. Then I tried line by line writing to a CSV but it requires each matrix to be converted to a string. How can I get this done in python? My requirement is different from the question Storing values in a 3D array to csv

rpb
  • 81
  • 1
  • 1
  • 6
  • 3
    Possible duplicate of [Python: Storing values in a 3D array to csv](https://stackoverflow.com/questions/46852741/python-storing-values-in-a-3d-array-to-csv) – Sebastian Mendez May 22 '18 at 02:57
  • 2
    How do you display a 3d structure as simple rows and columns? And read it back with code that expects that same csv layout? You can't without some sort of manipulation. Basically either reshape to and from 2d, or save the planes as separate 2d blocks. It's up to you to decide what the csv is supposed to look like. – hpaulj May 22 '18 at 03:01
  • @hpaulj got it. Thanks – rpb May 22 '18 at 03:06
  • @Sebastian I am expecting to write 2D matrices in each row of the CSV. I checked the answer to the question you pointed and the requirement is a bit different here. – rpb May 22 '18 at 03:11

2 Answers2

7

I'm not sure if it's the best way to doing it, but I faced the same problem and here's how I solved it.

import csv
import numpy as np
fil_name = 'file'
example = np.zeros((2,3,4))
example = example.tolist()
with open(fil_name+'.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerows(example)

#to read file you saved
with open(fil_name+'.csv', 'r') as f:
  reader = csv.reader(f)
  examples = list(reader)

print(examples)
nwexamples = []
for row in examples:
    nwrow = []
    for r in row:
        nwrow.append(eval(r))
    nwexamples.append(nwrow)
print(nwexamples)
4

I used this method instead, not aware of any better method:

# reshaping the array from 3D matrice to 2D matrice.
arrReshaped = arr.reshape(arr.shape[0], -1)
# saving reshaped array to file.
np.savetxt(filename, arrReshaped)
# retrieving data from file.
loadedArr = np.loadtxt(filename)
# This loadedArr is a 2D array, therefore we need to convert it to the original array shape.
# reshaping to get original matrice with original shape.
loadedOriginal = loadedArr.reshape(loadedArr.shape[0], loadedArr.shape[1] // arr.shape[2], arr.shape[2])
Chetan
  • 101
  • 1
  • 5