阅读背景:

如何为Picloud输出一个Numpy数组

来源:互联网 

I have a matrix-factorization process that I'm running on picloud. The output is a set of numpy arrays (ndarray).

我在picloud上运行一个矩阵分解过程。输出是一组numpy数组(ndarray)。

Now, I want to save it to my bucket, but I'm not able to zero in on the right way to do it. Let's assume that the array to be saved is P.

现在,我想把它存到我的桶里,但是我不能用正确的方法来做。假设要保存的数组是P。

I tried:

我试着:

cloud.bucket.putf(P,'p.csv')

but that returned an error: "IOError: File object is not seekable. Cannot transmit".

但是,这返回了一个错误:“IOError: File对象是不可见的。不能传输”。

I tried

我试着

numpy.ndarray.tofile(P,f, sep=",", format="%s") #outputing the array to a file object f
cloud.bucket.putf(f,'p.csv') #saving the file object f in the bucket.

I tried a couple of other things, including using using numpy.savetext (as I would if I ran it locally) but I'm not able to solve this between the picloud documentation and stackexchange questions. I haven't tried pickle yet, though. I felt this was something straightforward, but I'm feeling quite silly after spending a few hours on this.

我尝试了一些其他的东西,包括使用numpy。savetext(就像我在本地运行时那样),但是我无法在picloud文档和stackexchange问题之间解决这个问题。我还没试过泡菜。我觉得这很简单,但我在这上面花了几个小时后觉得很傻。

1 个解决方案

#1


2  

As you guessed, you want to pickle the array as follows:

正如您所猜测的,您希望按照以下方式对数组进行pickle:

import cloud
import cPickle as pickle

# to write
cloud.bucket.putf(pickle.dumps(P), 'p.csv')

# to read
obj = pickle.loads(cloud.bucket.getf('p.csv').read())

This is a general way to serialize and store any Python object in your PiCloud Bucket. I also recommend that you store your csv files under a prefix to keep it organized [1].

这是在您的PiCloud Bucket中序列化和存储任何Python对象的通用方法。我还建议您将csv文件存储在前缀下,以保持其组织[1]。

[1] https://docs.picloud.com/bucket.html#namespacing-with-prefix

[1]https://docs.picloud.com/bucket.html namespacing-with-prefix


分享到: