why cannot I use sp.signal by import scipy as sp? [duplicate]

谁说胖子不能爱 提交于 2020-01-14 14:39:50

问题


I would like to use scipy.signal.lti and scipy.signal.impulse function to calculate the transfer function. I import the scipy module in the following way.

import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
from math import *

However, when I type the following scripts,

tf = sp.signal.lti(numH, denH)

The Kernel gives an error:

---> 10 tf = sp.signal.lti(numH, denH)
AttributeError: 'module' object has no attribute 'signal' 

I tried another way to import the signal module,

from scipy.signal import lti, step, impulse

Then, the script works,

tf = lti(numH, denH)

So, my questions is, must we import every subpackage in the script? Then what's the point of importing the scipy package?

Thanks.


回答1:


The default behavior for which modules are imported in a python package is defined in the __init__.py file in the package directory. You can locate the scipy directory or find this using ipython, import scipy and call scipy? with questionmark to get the path. By default it seems scipy only imports numpy. The signal module is a directory of scripts, which includes its own __init__.py so you need the scipy.signal.lti syntax.

You could edit __init__.py as outlined in this question which would reduce the boilerplate imports, although probably not recommended as it would yield code which is not portable and may result in name clashes.




回答2:


From the scipy doc:

Using any of these subpackages requires an explicit import. For example, import scipy.cluster.

or from scipy import cluster.

There isn't much point to doing a simple

import scipy

Look at the site-packages/scipy/__init__.py file for more details. Compare it with the numpy init.

numpy is an integrated package, scipy is a collection of loosely integrated packages. numpy is the basic numeric package that everyone uses. The scipy subpackages are relatively independent of each other. I can load and use sparse without knowning anything about the signal or integrate packages.



来源:https://stackoverflow.com/questions/33379576/why-cannot-i-use-sp-signal-by-import-scipy-as-sp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!