9

我对 python 很陌生,我正在编写一个从与我一起工作的人那里继承的 python 脚本,我收到了这个错误:

expected a readable buffer object

造成这种情况的代码是:

self.y_NoShock_data = np.zeros((self.a_count+1,1,self.numberOfTags+1,lookback+forward,),dtype=enums.    
self.y_data = np.zeros((self.a_count+1,len(self.SCL)+1,self.numberOfTags+1,lookback+forward,),dtype=enums.DataPoints)

self.y_NoShock_cum_data = np.zeros_like(self.y_NoShock_data)
self.y_cum_data = np.zeros_like(self.y_data)

enums.DataPoints 看起来像这样:

enums.DataPoints = dtype([
        ('Amount','float32'),
        ])

堆栈跟踪如下:

Internal Server 

Error: /smCore/entity/1/runScenarioManager/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/views/createScenario.py", line 445, in runScenarioManager
    a = ScenarioExecutionController(sEA)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecutionController.py", line 176, in __init__
    shockEventDataSet=[], lookback=self.lookback, forward=self.forward, period=self.period) #,
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 307, in buildSeedScenarioObject
    cls.updateScenarioParameters(shockContainerList,shockEventDataSet, shockEventDateList)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 130, in updateScenarioParameters
    self.initialiseResultArrays()
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 154, in initialiseResultArrays
    self.y_NoShock_cum_data = np.zeros_like(self.y_NoShock_data,dtype=enums.DataPoints)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/numeric.py", line 116, in zeros_like
    res.fill(0)
TypeError: expected a readable buffer object

他在电脑上工作,而我在 Mac 上工作。我一直在环顾四周,但找不到解决方案,谁能指出我正确的方向?

4

2 回答 2

5

如果不知道enums.DataPointsdtype 对象是什么样子,就很难做出响应,但我会尝试解释您在哪里看到该错误消息。

当您尝试将数组(的元素)设置为与其 dtype 不正确对齐的某个值时,您会看到这一点。这是一个例子:

In [133]: data = np.zeros((3,2), dtype="int, int")

In [134]: data
Out[134]: 
array([[(0, 0), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

In [135]: data[0, 0]
Out[135]: (0, 0)

In [136]: data[0, 0] = [1,2]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-136-399e78675be4> in <module>()
----> 1 data[0, 0] = [1,2]

TypeError: expected a readable buffer object

这给出了一个错误,因为它无法处理分配给数组一个元素的两个值。您的 dtype 中有两个值,因此可以预期,但该数组需要一个由 dtype 给出的类型的对象:

In [137]: data[0,0] = (1,2)

In [138]: data
Out[138]: 
array([[(1, 2), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

数组相同形状的一组零很可能不会与 dtype 对齐。

于 2013-11-12T23:23:38.113 回答
1

对此的答案是,正如@askewchan 在对他的回答的评论中所指出的那样,我没有在我的同事使用的同一版本的 Numpy 下工作。

  • 失败:numpy 1.6.1
  • 作品:numpy 1.7
  • 作品:numpy 1.8.0
于 2013-11-15T16:11:51.843 回答