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

时间:2018-09-20 09:07:02

标签: python numpy

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

_messageSentTask

这是单元测试

[HttpPost]
    public ActionResult CreateP3(Process process)
    {
        var tr = process.StageID + process.PIPENO + process.Status;
        var checkme = (from x in db.Processes
                       where x.StageID + x.PIPENO + x.Status == tr && x.Status == "OK"
                       select x).ToList();
        if (checkme.Count > 0)
        {
            ModelState.AddModelError("PIPENO", "You are trying Duplicate Pipe Entry");
        }
        if (ModelState.IsValid)
        {
            if (process.PipeAl.Order.THK > 1)
            {
                process.MPP = 20;
            }
            else
            {
                process.MPP = 1;
            }
            process.CuttingSpeed = (process.ActualMeter*1000)/process.CuttingTime;
            db.Processes.Add(process);
            db.SaveChanges();
            return RedirectToAction("IndexP3");
        }

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

为什么会出现错误[[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)

请注意,这与"AttributeError: exp" while using numpy.exp() on an apparently ordinary array相同,但是这个问题从未得到回答,也没有像我一样提供任何最小的例子。

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

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()

编辑:有关已加载数据的更多信息

我期望AttributeError: exp只是一个numpy数组,但似乎并没有,请参见下文,最终会导致错误

[[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)

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

1 个答案:

答案 0 :(得分:1)

事实证明答案很简单,这些加载的变量本身就是最初的matlab结构,而在检索它们时我省略了索引,正确的做法是以下操作(注意在检索时要多出[0,0] s相位和sigma):

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()