简体   繁体   中英

Why do I have to import this from numpy if I am just referencing it from the numpy module

Aloha!

I have two blocks of code, one that will work and one that will not. The only difference is a commented line of code for a numpy module I don't use. Why am I required to import that model when I never reference "npm"?

This command works:

import numpy as np
import numpy.matlib as npm

V  = np.array([[1,2,3],[4,5,6],[7,8,9]])
P1 = np.matlib.identity(V.shape[1], dtype=int)
P1

This command doesn't work:

import numpy as np
#import numpy.matlib as npm

V  = np.array([[1,2,3],[4,5,6],[7,8,9]])
P1 = np.matlib.identity(V.shape[1], dtype=int)
P1

The above gets this error:

AttributeError: 'module' object has no attribute 'matlib'

Thanks in advance!

Short Answer

This is because numpy.matlib is an optional sub-package of numpy that must be imported separately.

The reason for this feature may be:

  • In particular for numpy , the numpy.matlib sub-module redefines numpy 's functions to return matrices instead of ndarrays, an optional feature that many may not want
  • More generally, to load the parent module without loading a potentially slow-to-load module which many users may not often need
  • Possibly, namespace separation

When you import just numpy without the sub-package matlib , then Python will be looking for .matlib as an attribute of the numpy package. This attribute has not been assigned to numpy without importing numpy.matlib (see discussion below)

Sub-Modules and Binding

If you're wondering why np.matlib.identity works without having to use the keyword npm , that's because when you import the sub-module matlib , the parent module numpy (named np in your case) will be given an attribute matlib which is bound to the sub-module. This only works if you first define numpy .

From the reference :

When a submodule is loaded using any mechanism (eg importlib APIs, the import or import-from statements, or built-in import ()) a binding is placed in the parent module's namespace to the submodule object.

Importing and __init__.py

The choice of what to import is determined in the modules' respective __init__.py files in the module directory. You can use the dir() function to see what names the respective modules define.

>> import numpy

>> 'matlib' in dir(numpy)
# False

>> import numpy.matlib

>> 'matlib' in dir(numpy)
# True

Alternatively, if you look directly at the __init__.py file for numpy you'll see there's no import for matlib .

Namespace across Sub-Modules

If you're wondering how the namespace is copied over smoothly ;

The matlib source code runs this command to copy over the numpy namespace:

import numpy as np                                    # (1)
...
# need * as we're copying the numpy namespace
from numpy import *                                   # (2)
...
__all__ = np.__all__[:] # copy numpy namespace        # (3)

Line (2), from numpy import * is particularly important. Because of this, you'll notice that if you just import numpy.matlib you can still use all of numpy modules without having to import numpy !

Without line (2), the namespace copy in line (3) would only be attached to the sub-module. Interestingly, you can still do a funny command like this because of line (3).

import numpy.matlib               
numpy.matlib.np.matlib.np.array([1,1])

This is because the np.__all__ is attached to the np of numpy.matlib (which was imported via line (1)).

You never use npm but you do use np.matlib , so you could change your 2nd import line to just:

import numpy.matlib

Or you could keep your 2nd import line as is but instead use:

P1 = npm.identity(V.shape[1], dtype=int)

Is there are reason you don't use np.identity ?

P1 = np.identity(V.shape[1], dtype=int)

This module contains all functions in the numpy namespace, with the following replacement functions that return matrices instead of ndarrays.

Unless you are wedded to 2d np.matrix subclass, you are better off sticking with the regular ndarray versions.

(Others have pointed out that the import why is based on the __init__ specs for numpy . numpy imports most, but not all of its submodules. The ones it does not automatically import are used less often. It's a polite way of saying, You don't really need this module )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM