AttributeError:exp 在使用 scipy.io.loadmat 加载的数据上使用 numpy 时



我从下面的单元测试中得到以下输出:

[[array([[-1.57079633]])]]
[[array([[0.+1.57079633j]])]]
<module 'numpy' from '/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'>
E
======================================================================
ERROR: test_TestWECTrain_BasicEnv_SetupAndStepping (__main__.Test_exp)
----------------------------------------------------------------------
Traceback (most recent call last):
File "Test_exp.py", line 34, in test_TestWECTrain_BasicEnv_SetupAndStepping
expsigmatphase = np.exp(tmp)
AttributeError: exp
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)

这是单元测试

import unittest
import os
import scipy.io as sio
import numpy as np
from pprint import pprint
class Test_exp (unittest.TestCase):
def test_exp (self):
data_file = "test_buoysimoptions.mat"
buoysimoptions = sio.loadmat (data_file)
t = 0.0
phase = buoysimoptions['SeaParameters']['phase']
sigma = buoysimoptions['SeaParameters']['sigma']
sigmatminusphase = sigma * t - phase; print (sigmatminusphase)
tmp = -1.0j * sigmatminusphase; print (tmp)
print (np)
tmp = np.asarray(tmp)
expsigmatphase = np.exp(tmp)

if __name__ == '__main__':
unittest.main()

输入文件(2.9kB(可以在这里下载:https://www.dropbox.com/s/psq1gq8xpjivrim/test_buoysimoptions.mat?dl=0

为什么我会收到错误AttributeError: exp

请注意,这与在看似普通的数组上使用numpy.exp((时的"AttributeError:exp"相同,但这个问题从未得到解答,也没有像我一样提供最小的例子。

这是在Python 2.7中,在Python 3.5中,我得到:

[[array([[-1.57079633]])]]
[[array([[0.+1.57079633j]])]]
E
======================================================================
ERROR: test_exp (__main__.Test_exp)
----------------------------------------------------------------------
Traceback (most recent call last):
File "Test_exp.py", line 25, in test_exp
expsigmatphase = np.exp(tmp)
AttributeError: 'numpy.ndarray' object has no attribute 'exp'
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (errors=1)

编辑:有关加载数据的一些进一步信息

我希望buoysimoptions['SeaParameters']['phase']只是一个 numpy 数组,但似乎不是,见下文,最终导致错误

>>> phase = buoysimoptions['SeaParameters']['phase']
>>> phase
array([[array([[1.57079633]])]], dtype=object)
>>> phase = buoysimoptions['SeaParameters']['phase'][0]
>>> phase
array([array([[1.57079633]])], dtype=object)
>>> phase = buoysimoptions['SeaParameters']['phase'][0][0]
>>> phase
array([[1.57079633]])

我是否需要始终索引 [0][0] 才能获取实际数组?在这里做什么是正确的?如果我使用最后一个,exp 错误就会消失。

事实证明答案很简单,这些加载的变量本身就是 matlab 结构,我在检索它们时省略了索引,正确的做法如下(注意检索相位和西格玛时额外的 [0,0](:

import unittest
import os
import scipy.io as sio
import numpy as np
from pprint import pprint
class Test_exp (unittest.TestCase):
def test_exp (self):
data_file = "test_buoysimoptions.mat"
buoysimoptions = sio.loadmat (data_file)
t = 0.0
phase = buoysimoptions['SeaParameters'][0,0]['phase']
sigma = buoysimoptions['SeaParameters'][0,0]['sigma']
sigmatminusphase = sigma * t - phase; print (sigmatminusphase)
tmp = -1.0j * sigmatminusphase; print (tmp)
print (np)
tmp = np.asarray(tmp)
expsigmatphase = np.exp(tmp)

if __name__ == '__main__':
unittest.main()

最新更新