简体   繁体   中英

import numpy as np versus from numpy import

I have a module that heavily makes use of numpy :

from numpy import array, median, nan, percentile, roll, sqrt, sum, transpose, unique, where

Is it better practice to keep the namespace clean by using

import numpy as np

and then when I need to use array just use np.array , for eg?

This module also gets called repeatedly, say a few million times and keeping the namespace clean appears to add a bit of an overhead?

setup = '''import numpy as np'''
function = 'x = np.sum(np.array([1,2,3,4,5,6,7,8,9,10]))'
print(min(timeit.Timer(function, setup=setup).repeat(10, 300000)))

1.66832

setup = '''from numpy import arange, array, sum'''
function = 'x = sum(array([1,2,3,4,5,6,7,8,9,10]))'
print(min(timeit.Timer(function, setup=setup).repeat(10, 300000)))

1.65137

Why does this add more time when using np.sum vs sum ?

You are right, it is better to keep the namespace clean. So I would use

import numpy as np

It keeps your code more readable, when you see a call like np.sum(array) you are reminded that you should work with an numpy array. The second reason is that many of the numpy functions have identical names as functions in other modules like scipy... If you use both its always clear which one you are using.

As you you can see in the test you made, the performance difference is there and if you really need the performance you could do it the other way.

The difference in performance is that in the case of a specific function import, you are referencing the function in the numpy module at the beginning of the script. In the case of the general module import you import only the reference to the module and python needs to resolve/find the function that you are using in that module at every call.

You could have the best of both worlds (faster name resolution, and non-shadowing), if you're ok with defining your own aliasing (subject to your team conventions, of course):

import numpy as np
(np_sum, np_min, np_arange) = (np.sum, np.min, np.arange)

x = np_arange(24)
print (np_sum(x))

Alternative syntax to define your aliases:

from numpy import \
    arange as np_arange, \
    sum as np_sum, \
    min as np_min

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