简体   繁体   中英

load numpy.ndarray from csv string

I converted images to numpy array and saved to csv file

back_ground = Back_ground()
X = make_test_set('back_ground.csv',back_ground,3500)
Y = back_ground.make_answer()

with open('background.csv','w',newline='') as csvfile:
    fieldnames = ['image','answer']
    writer = csv.DictWriter(csvfile,fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'image': X, 'answer':Y})

make_test_set() and make_answer() return numpy.ndarray

But when I want to use this data, like this

with open('background.csv','r') as csvfile:
    reader = csv.DictReader(csvfile)
    for number,value in enumerate(reader):
        print(number)
        current_img = value['image']
        print(type(current_img))
        plt.imshow(current_img)
        plt.show()

type of currnet_img is string so I can't use any numpy functions how can I convert to numpy.ndarray? or are there any good method to save numpy.ndarray?

In [26]: import csv
In [27]: X = np.arange(12).reshape(3,4)
In [28]: Y = np.arange(12)
In [29]: with open('background.csv','w',newline='') as csvfile:
    ...:     fieldnames = ['image','answer']
    ...:     writer = csv.DictWriter(csvfile,fieldnames=fieldnames)
    ...: 
    ...:     writer.writeheader()
    ...:     writer.writerow({'image': X, 'answer':Y})
    ...:     
In [30]: cat background.csv
image,answer
"[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]",[ 0  1  2  3  4  5  6  7  8  9 10 11]

The DictWriter has written the str representation of the 2 values, and quoted the multiline one:

In [31]: str(X)
Out[31]: '[[ 0  1  2  3]\n [ 4  5  6  7]\n [ 8  9 10 11]]'

There isn't a convenient way of converting that str(X) back into X .

A good way of saving these 2 arrays would be with savez :

In [40]: np.savez('background.npz', image=X, answer=Y)
In [42]: data = np.load('background.npz')
In [43]: list(data.keys())
Out[43]: ['image', 'answer']
In [44]: data['image']
Out[44]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [45]: data['answer']
Out[45]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM