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

2024/9/16 23:23:07

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.

Answer

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://en.xdnf.cn/q/72825.html

Related Q&A

How to speed up nested cross validation in python?

From what Ive found there is 1 other question like this (Speed-up nested cross-validation) however installing MPI does not work for me after trying several fixes also suggested on this site and microso…

Streaming video from camera in FastAPI results in frozen image after first frame

I am trying to stream video from a camera using FastAPI, similar to an example I found for Flask. In Flask, the example works correctly, and the video is streamed without any issues. However, when I tr…

Fastest way to concatenate multiple files column wise - Python

What is the fastest method to concatenate multiple files column wise (within Python)?Assume that I have two files with 1,000,000,000 lines and ~200 UTF8 characters per line.Method 1: Cheating with pas…

Can autograd in pytorch handle a repeated use of a layer within the same module?

I have a layer layer in an nn.Module and use it two or more times during a single forward step. The output of this layer is later inputted to the same layer. Can pytorchs autograd compute the grad of t…

Altering numpy function output array in place

Im trying to write a function that performs a mathematical operation on an array and returns the result. A simplified example could be:def original_func(A):return A[1:] + A[:-1]For speed-up and to avoi…

Does the E-factory of lxml support dynamically generated data?

Is there a way of creating the tags dynamically with the E-factory of lxml? For instance I get a syntax error for the following code:E.BODY(E.TABLE(for row_num in range(len(ws.rows)):row = ws.rows[row…

Check if datetime object in pandas has a timezone?

Im importing data into pandas and want to remove any timezones – if theyre present in the data. If the data has a time zone, the following code works successfully: col = "my_date_column" df[…

Extract translator comments with xgettext from JavaScript (in Python mode)

I have a pretty well-working command that extracts strings from all my .js and .html files (which are just Underscore templates). However, it doesnt seem to work for Translator comments.For example, I …

Embedding python + numpy code into C++ dll callback

I am new of python embedding. I am trying to embed python + numpy code inside a C++ callback function (inside a dll)the problem i am facing is the following. if i have:Py_Initialize(); // some python g…

How to parse single file using Python bindings to Clang?

I am writing a simple tool to help with refactoring the source code of our application. I would like to parse C++ code based on wxWidgets library, which defines GUI and produce XML .ui file to use with…