Why should I import this from numpy if I just reference it from the numpy module

Aloha!

I have two blocks of code: one that will work, and one that won't. The only difference is the commented out line of code for the numpy module, which I do not use. Why do I need to import this model when I never refer to "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 does not 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 error:

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

Thanks in advance!

+5
source share
3 answers

Short answer

This is due to the fact that numpy.matlib is an additional numpy subpacker that must be imported separately.

The reason for this function may be:

  • In particular, for numpy numpy.matlib submodule overrides numpy functions to return matrices instead of ndarrays, an optional function that many might not need
  • In the general case, to load the parent module without loading the module with slow loading, which many users may not need often
  • Maybe split namespace

When you import only numpy without the matlib , then Python will look for .matlib as an attribute of the numpy package. This attribute was not assigned to numpy without importing numpy.matlib (see discussion below)

Submodules and Binding

If you're wondering why np.matlib.identity works without using the npm keyword, this is because when you import the matlib submodule, the matlib parent module (named np in your case) will be assigned the matlib attribute attached to the submodule. This only works if you first define numpy .

From reference :

When a submodule is loaded using any mechanism (for example, importlib APIs, import or import statements, or built-in import ()), the binding is placed in the namespace of the parent modules on the submodule object.

Import and __init __. py

The choice of what you need to import is determined in the corresponding __init__.py module files in the module directory. You can use the dir() function to find out which names define the respective modules.

 >> 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 will not see the import for matlib .

Namespace for submodules

If you're curious how the namespace is copied seamlessly,

matlib source code runs this command to copy through 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 * especially important. Because of this, you will notice that if you just import numpy.matlib , you can still use all numpy modules without importing numpy !

Without line (2), a copy of the namespace in line (3) will be bound only to the submodule. Interestingly, you can still make such a fun team because of the line (3).

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

This is because np.__all__ bound to np numpy.matlib (which was imported on line (1)).

+6
source

You never use npm , but you use np.matlib , so you can change your second import line only:

 import numpy.matlib 

Or you can save your second import line as is, but use:

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

Is there a reason you are not using np.identity ?

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

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

If you are not tied to the 2d np.matrix subclass, you'd better stick with the regular versions of ndarray .

(Others pointed out that import is based on __init__ specifications for numpy . numpy imports most, but not all, of its submodules. Those that it does not automatically import are less commonly used. This is a polite way of saying: You don't really need this module )

+2
source

Source: https://habr.com/ru/post/1263224/


All Articles