2

I'm getting a tuple of numpy arrays as (keypoint, descriptor) when I run the compute function to extract them from an image.

Is there a way to pack this tuple together so that I can save them to a file, or write them into a CSV as a row?

7
  • Are they always one-dimensional or always two-dimensional? Commented Mar 18, 2015 at 1:17
  • 2
    You can save your tuple to a file straight away, e.g. with pickle. Commented Mar 18, 2015 at 1:20
  • I believe they both two dimensional Commented Mar 18, 2015 at 1:21
  • 1
    np.savez can save several arrays to a ZIP archive. np.savetxt writes a CSV like text file. It just arranges its input into a 2d array, and then writes it (with a format string), one row per file line. Nothing fancy. Commented Mar 18, 2015 at 1:34
  • Is this Python 2 or Python 3? Commented Mar 18, 2015 at 1:55

2 Answers 2

2

There are a few ways you can do this:

  • You can use Python csv module, specifically writer objects:

    import csv
    writer = csv.writer(open("file.csv","w"))
    for row in array:
         writer.writerow(str(row))
    

    According to this question, there might be some formatting problems with this.

  • As mentioned in the comments, you can use numpy.savetxt(). This is probably the best way:

    numpy.savetxt("file.csv",array,delimiter=",")
    
  • You can also use the pickle module:

    import pickle
    pickle.dump(array,open("file","w"))
    

    As mentioned in the docs I linked above, pickle.load() should not be used to load untrusted data.

Sign up to request clarification or add additional context in comments.

8 Comments

Pickle seems to be the easiest way of storing a tuple of numpy arrays. Thanks!
csv.writer() takes a file object, not a filename; with open('file.csv', 'wb') as outf: writer = csv.writer(outf).
@MartijnPieters I just noticed that when checking for differences in Python 2 vs. Python 3. I fixed it.
i don't think you mean to be using the .csv file suffix in the pickle dump. also, shouldn't you be opening it in binary mode (i.e., 'wb')?
You're right about the first point (though the extension doesn't really matter, but pickle.dump() doesn't need a file opened in binary mode.
|
1

Rather than using pickle, I would recommend using cPickle:

import cPickle as pickle
pickle.dump(array, open('file.p', 'wb'))

Note that all of your calls remain the same as when you are using pickle -- only the import line changes.

cPickle is much faster than pickle and with large arrays can save you a lot of time.

That said, when you're dealing with numpy arrays, using numpy's save functions -- e.g., np.savetxt -- is your best bet.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.