numpy/scipy/ipython:无法将文件解释为 pickle

发布于 2025-01-01 00:05:58 字数 1190 浏览 2 评论 0原文

我有以下格式的文件:

0,0.104553357966
1,0.213014562052
2,0.280656379048
3,0.0654249076288
4,0.312223429689
5,0.0959008911106
6,0.114207780917
7,0.105294501195
8,0.0900673766572
9,0.23941317105
10,0.0598239513149
11,0.541701803956
12,0.093929580526

我想使用 ipython 绘图函数来绘制这些点,执行以下操作:

   In [40]: mean_data = load("/Users/daydreamer/data/mean")

但它无法说明以下内容:

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
/Users/daydreamer/<ipython-input-40-8f1329559411> in <module>()
----> 1 mean_data = load("/Users/daydreamer/data/mean")

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy-1.6.1-py2.7-macosx-10.5-fat3.egg/numpy/lib/npyio.pyc in load(file, mmap_mode)
    354             except:
    355                 raise IOError, \
--> 356                     "Failed to interpret file %s as a pickle" % repr(file)
    357     finally:
    358         if own_fid:

IOError: Failed to interpret file '/Users/daydreamer/data/mean' as a pickle

如何修复此错误?

I have the file in following format:

0,0.104553357966
1,0.213014562052
2,0.280656379048
3,0.0654249076288
4,0.312223429689
5,0.0959008911106
6,0.114207780917
7,0.105294501195
8,0.0900673766572
9,0.23941317105
10,0.0598239513149
11,0.541701803956
12,0.093929580526

I want to plot these point using ipython plot function doing the following:

   In [40]: mean_data = load("/Users/daydreamer/data/mean")

But it fails saying the following:

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
/Users/daydreamer/<ipython-input-40-8f1329559411> in <module>()
----> 1 mean_data = load("/Users/daydreamer/data/mean")

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy-1.6.1-py2.7-macosx-10.5-fat3.egg/numpy/lib/npyio.pyc in load(file, mmap_mode)
    354             except:
    355                 raise IOError, \
--> 356                     "Failed to interpret file %s as a pickle" % repr(file)
    357     finally:
    358         if own_fid:

IOError: Failed to interpret file '/Users/daydreamer/data/mean' as a pickle

How do I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2025-01-08 00:05:58

numpy.load例程用于加载腌制的 .npy.npz 二进制文件,可以使用 numpy.savenumpy.savez 分别。由于您有文本数据,这些不是您想要的例程。

您可以使用 numpy.loadtxt 加载逗号分隔值

import numpy as np
mean_data = np.loadtxt("/Users/daydreamer/data/mean", delimiter=',')

完整示例

这是一个完整的示例(使用 StringIO 来模拟文件 I/O)。

import numpy as np
import StringIO

s = """0,0.104553357966
1,0.213014562052
2,0.280656379048
3,0.0654249076288
4,0.312223429689
5,0.0959008911106
6,0.114207780917
7,0.105294501195
8,0.0900673766572
9,0.23941317105
10,0.0598239513149
11,0.541701803956
12,0.093929580526"""

st = StringIO.StringIO(s)
a = np.loadtxt(st, delimiter=',')

现在我们有:

>>> a
array([[  0.        ,   0.10455336],
       [  1.        ,   0.21301456],
       [  2.        ,   0.28065638],
       [  3.        ,   0.06542491],
       [  4.        ,   0.31222343],
       [  5.        ,   0.09590089],
       [  6.        ,   0.11420778],
       [  7.        ,   0.1052945 ],
       [  8.        ,   0.09006738],
       [  9.        ,   0.23941317],
       [ 10.        ,   0.05982395],
       [ 11.        ,   0.5417018 ],
       [ 12.        ,   0.09392958]])

The numpy.load routine is for loading pickled .npy or .npz binary files, which can be created using numpy.save and numpy.savez, respectively. Since you have text data, these are not the routines you want.

You can load your comma-separated values with numpy.loadtxt.

import numpy as np
mean_data = np.loadtxt("/Users/daydreamer/data/mean", delimiter=',')

Full Example

Here's a complete example (using StringIO to simulate the file I/O).

import numpy as np
import StringIO

s = """0,0.104553357966
1,0.213014562052
2,0.280656379048
3,0.0654249076288
4,0.312223429689
5,0.0959008911106
6,0.114207780917
7,0.105294501195
8,0.0900673766572
9,0.23941317105
10,0.0598239513149
11,0.541701803956
12,0.093929580526"""

st = StringIO.StringIO(s)
a = np.loadtxt(st, delimiter=',')

Now we have:

>>> a
array([[  0.        ,   0.10455336],
       [  1.        ,   0.21301456],
       [  2.        ,   0.28065638],
       [  3.        ,   0.06542491],
       [  4.        ,   0.31222343],
       [  5.        ,   0.09590089],
       [  6.        ,   0.11420778],
       [  7.        ,   0.1052945 ],
       [  8.        ,   0.09006738],
       [  9.        ,   0.23941317],
       [ 10.        ,   0.05982395],
       [ 11.        ,   0.5417018 ],
       [ 12.        ,   0.09392958]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文