【问题标题】:Error importing scipy as sp when I use sp.integrate.quad()使用 sp.integrate.quad() 时将 scipy 作为 sp 导入时出错
【发布时间】:2023-03-31 17:57:01
【问题描述】:

scipy 导入 Python 时出现错误。当我写的时候:

import scipy as sp
x2 = lambda x: x**2
print sp.integrate.quad(x2, 0, 4)

我得到错误:

sp.integrate.quad: "NameError: name 'integrate' is not defined".

为什么会出现这个错误?

【问题讨论】:

    标签: python scipy


    【解决方案1】:

    导入scipy 不会自动加载integrate 子包。使用:

    from scipy.integrate import quad
    

    import scipy.integrate as spi
    

    并使用spi.quad

    来自文档(或者,更确切地说,是 SciPy 的 __init__.py 文件):

    ...
    Subpackages
    -----------
    Using any of these subpackages requires an explicit import.  For example,
    ``import scipy.cluster``.
    
    ::
    
     cluster                      --- Vector Quantization / Kmeans
     fftpack                      --- Discrete Fourier Transform algorithms
     ...
     integrate                    --- Integration routines [*]
     ...
    

    【讨论】:

    • 另见this关于为什么会这样的问题
    • 感谢你们两位的解决方案和其他问题中的附加信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多