首页 > 解决方案 > 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 错误就会消失。

标签: pythonnumpy

解决方案


事实证明答案很简单,这些加载的变量本身就是 matlab 结构,我在检索它们时省略了索引,正确的做法如下(注意检索相位和 sigma 时额外的 [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()

推荐阅读