python - 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 结构,我在检索它们时省略了索引,正确的做法如下(注意检索相位和 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()
推荐阅读
- ios - 如何使用 sprite kit 进行资产管理
- graphql - 如何使用nexus-prisma做一个嵌套的变异解析器
- python - 如何从稀疏矩阵在networkx中创建图形?
- mysql - 如何执行一个非常庞大的 MySQL 查询?(花了几天然后没有成功就停止了)
- c++ - 是否有任何内置函数可以告诉编译器分支是否可预测?
- vb.net - 指定的打印机无效(有时可以工作,有时不能)
- sql-server - 用于从所述表的关系属性更新表属性的 SQL 查询
- javascript - 使用 JQuery 在表中显示 JSON
- node.js - 我如何在 Angular 中写博客组件?
- c++ - 为什么不是条件 *First1 == * First2 ?? (C++ STL 包含函数实现)