7

I am using scipy 0.17.1 and numpy 1.11.1. I am getting an error when trying to use interpolate, even if the subpackage interpolate should be included in my version (docs)

import numpy as np
import scipy
x = np.linspace(0, 2*np.pi, 1000)
y = np.sin(x) + 0.01*np.random.randn(1, 1000)
y = scipy.interpolate.PchipInterpolator(x, y)

Results in error:

Traceback (most recent call last):
  File "C:\Users\flabriol\AppData\Local\Continuum\Anaconda2\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-110-7dfbae0fdab5>", line 5, in <module>
    y = scipy.interpolate.PchipInterpolator(x, y)
AttributeError: 'module' object has no attribute 'interpolate'

Can I use the interpolate module without upgrading scipy?

FLab
  • 7,136
  • 5
  • 36
  • 69

1 Answers1

21

As per the scipy source - you need to explicitly import the subpackage:

Subpackages

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

So changing (or adding)

import scipy.interpolate

should fix it for you

Gavin
  • 1,070
  • 18
  • 24
  • 2
    Just wondering if you or anyone else happens to know why scipy is like this. Numpy is not: I can call numpy.random.rand without the explicit import of numpy.random. – saintsfan342000 Aug 11 '17 at 11:56
  • 3
    @saintsfan342000 - This [question](https://stackoverflow.com/questions/27744767/differences-in-importing-modules-subpackages-of-numpy-and-scipy-packages) and answer may shed some light: In particular this [comment](https://stackoverflow.com/questions/27744767/differences-in-importing-modules-subpackages-of-numpy-and-scipy-packages#comment43908729_27744767) probably has the key point (ie design decision) – Gavin Aug 11 '17 at 12:03