【问题标题】:SciPy import error when try to import spline尝试导入样条曲线时的 SciPy 导入错误
【发布时间】:2020-06-19 16:48:41
【问题描述】:

我得到的错误行:

    from scipy.interpolate import spline
ImportError: cannot import name 'spline'

导致问题的源代码是:

from scipy.interpolate import spline

我尝试了几种安装scipy的方法:

  • conda 使用conda install scipy
  • 从 PyCharm 安装 但他们都没有工作。有谁知道发生了什么?

【问题讨论】:

    标签: python-3.x scipy python-3.6


    【解决方案1】:

    spline 不在scipy.interpolate 模块中。您可以改用splrepUnivariateSpline。看看documentation中的可用函数。

    【讨论】:

    • 感谢您的回复,我做了研究,发现spline 已从版本1.3.1 中删除,我回滚到1.2.1 并且它有效!
    【解决方案2】:
    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.interpolate import interp1d
    
    x = np.linspace(0, 10, num=11, endpoint=True)
    y = np.cos(-x**2/9.0)
    f1 = interp1d(x, y, kind='nearest')
    f2 = interp1d(x, y, kind='zero')
    f3 = interp1d(x, y, kind='quadratic')
    
    xnew = np.linspace(0, 10, num=1001, endpoint=True)
    plt.plot(x, y, 'o')
    plt.plot(xnew, f1(xnew), '-', xnew, f2(xnew), '--', xnew, f3(xnew), ':')
    plt.legend(['data', 'nearest', 'zero', 'quadratic'], loc='best')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      相关资源
      最近更新 更多