4

Given the following code and assuming that x, y, and z are properly re-defined for interpolation:

import numpy as np
import scipy as sp

x = np.arange(5,-5.5,0.5)
f = sp.interpolate.interp2d(x, y, z, kind='linear')

Why does the last line give the following error?

'module' object has no attribute 'interpolate'

I solve my problem when I explicitly import interpolate as follows:

from scipy import interpolate

And then call interpolate like so

interpolate.interp2d(x, y, z, kind='linear')

But I don't understand why I have to explicitly import interpolate when I didn't have to explicitly import arange(). Based on other posts and comments below, scipy is organized differently than numpy, and interpolate is itself a submodule (whereas arange() is just a function). Is that it?

techSultan
  • 203
  • 2
  • 8
  • 5
    `arange` is a function while `interpolate` is a submodule. You'll need to import those explicitly. – cs95 Jun 05 '17 at 18:54
  • 4
    Also, `interpolate` is a submodule of **`scipy`**, not `numpy`. – user2357112 Jun 05 '17 at 18:55
  • @user2357112 whoops, thanks for the catch. I meant to write sp – techSultan Jun 05 '17 at 18:56
  • 1
    This is just how the `scipy` developers chose to organize the package. You import subpackages individually. `numpy` is a more compact unified package. But even it has a few modules that need to be imported individually. – hpaulj Jun 05 '17 at 19:04
  • 1
    What don't you understand or accept from this section: https://docs.scipy.org/doc/scipy/reference/api.html#guidelines-for-importing-functions-from-scipy – hpaulj Jun 05 '17 at 19:19
  • @hpaulj why does scipy ask me to explicitly import functions when numpy doesn't (most of the time)? Am I not providing clear enough "directions" to Python when I say sp.interpolate.interp2d() when I've already imported scipy as sp? – techSultan Jun 05 '17 at 19:32
  • @hpaulj They restructured though? I think there's old answers like [this](https://stackoverflow.com/questions/21661802/finding-the-distance-between-a-set-of-points-using-scipy-spatial-distance-cdist) that suggest it is all one package. I can't remember what question I saw specifically, but I'm pretty sure it imported `scipy.spatial.distance` and I had to change to `from scipy.spatial import distance`, a bit confused, but I can't find it :( – roganjosh Jun 05 '17 at 19:39
  • @hpaulj I should have paid more attention to the question in my link since that also imports it as a single package. There's multiple questions/answers that work this way on SO, so it's surprising when those imports no longer work. – roganjosh Jun 05 '17 at 19:51
  • The import details are spelled out in the `__init__.py` file at each directory level - `scipy`, `numpy`, `scipy/spatial`. I don't know when they last did a major restructuring. My impression is that `scipy` has always been a looser collection. Python 3 has also made changes in how modules are imported. – hpaulj Jun 05 '17 at 20:10
  • 2
    @ev-br The answers to that post are what should be done about it. The OP here already knows what should be done, but he wants to know why. The other post doe not supply that. – zondo Jun 05 '17 at 20:46
  • The *why* answer is in the comments to the top voted answer, as a link to the official docs which directly answer the question. – ev-br Jun 05 '17 at 20:48
  • 2
    I think I found the actual "source" question I'm duplicating. Feel free to close this thread. https://stackoverflow.com/questions/12229580/python-importing-a-sub-package-or-sub-module – techSultan Jun 05 '17 at 20:55

0 Answers0