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
-
3Possible duplicate of Python: Storing values in a 3D array to csvSebastian Mendez– Sebastian Mendez2018-05-22 02:57:36 +00:00Commented May 22, 2018 at 2:57
-
2How 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– hpaulj2018-05-22 03:01:50 +00:00Commented May 22, 2018 at 3:01
-
@hpaulj got it. Thanksrpb– rpb2018-05-22 03:06:29 +00:00Commented May 22, 2018 at 3: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– rpb2018-05-22 03:11:10 +00:00Commented May 22, 2018 at 3:11
Add a comment
|
2 Answers
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)
2 Comments
Vahid
This solution is more general and works very well.
Michael James
thank you for this. it worked for a 5D matrix!
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])
1 Comment
Vahid
I think this solution is more efficient!