Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

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!

like image 695
TankNorton Avatar asked Jan 24 '17 01:01

TankNorton


People also ask

What is the difference between import Numpy and from Numpy import *?

Closed 3 years ago. using from numpy import array , you only import the array module. you cannot use other functions of numpy. with import numpy as np , you import all the numpy modules and you can use them as np.

Why do we use import Numpy?

The import numpy portion of the code tells Python to bring the NumPy library into your current environment. The as np portion of the code then tells Python to give NumPy the alias of np. This allows you to use NumPy functions by simply typing np. function_name rather than numpy.

Do you have to import Numpy as NP?

The numpy should be imported in order to use it. It can be imported by using the import statement and module name like below. But typing the numpy every time we use one of the elements of numpy is not a practical way.

What is import Numpy in Python?

NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.


Video Answer


2 Answers

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 (e.g. 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)).

like image 76
Jamie Phan Avatar answered Oct 23 '22 22:10

Jamie Phan


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)
like image 22
Sash Sinha Avatar answered Oct 23 '22 23:10

Sash Sinha