0

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?

1
  • What are X and Y? What does the resulting csv file look like? The csv format is just numbers (or strings) separated by delimiters. The are numpy functions for writing and reading csv - np.savetxt and np.loadtxt. But they work best with simple 2d numeric arrays. There also binary save methods like np.save and np.load. Commented May 10, 2018 at 16:09

1 Answer 1

0
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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.