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

Python AttributeError:exp在使用scipy.io.loadmat加载的数据上使用numpy时,python,numpy,Python,Numpy,我从下面的单元测试中获得以下输出: [[array([[-1.57079633]])]] [[array([[0.+1.57079633j]])]] <module 'numpy' from '/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'> E ====================================================================== ERROR: test_Te

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

[[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)
可在此处下载输入文件(2.9kB):

为什么我会得到错误
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)
编辑:有关加载数据的更多信息

我期望
booksimoptions['seaparters']['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结构,我在检索它们时省略了索引,正确的做法如下(在检索相位和sigma时注意额外的[0,0]s):


print(type(tmp),dir(tmp))
的输出是什么?问题似乎不是关于
np
,而是
tmp
。嗯。在命令行上,我可以复制它。但是我看到
tmp.dtype
O
。。。如果我尝试创建一个数组作为
a=np.array([[0.+1.57079633j]],dtype=object)
,那么
np.exp(tmp)
会失败,并出现
AttributeError
,但是如果我指定
dtype=complex
,那么我会得到正确的结果。。。也许您只需要在某个地方指定
dtype
?Re为什么在命令行上工作而不是从脚本:您可能使用不同的python,因此使用不同的库版本。也许在某些情况下,它会以
dtype=O
vs
dtype=complex
结束,你会得到这个错误。我认为问题在于加载结构的字段如下:
array([[array([[1.57079633]]),dtype=object)
@NilsWerner,不,肯定没有,你看到打印输出(np)了吗声明?这似乎更像是你的文件的问题。。。如果有人向您发送了一个以奇怪方式创建的文件,您将获得一个奇怪的结果。您可以更改文件的创建方式吗?也许就是在那里,你必须修复一些东西。
>>> 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]])
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()