confusion on importing numpy

Hello list. I am confused about importing numpy. When I do from numpy import * and I try the min function, I get the default Python min function. On the other hand, when I do import numpy as np and use the np.min function, I get the numpy min function (which is obviously what I want). I know, the latter is preferred, but the former is so dang easy to type. I always thought both imports work the same, but I am obviously wrong. What's the difference? Thanks, Mark

On Wed, Aug 6, 2008 at 05:00, mark <markbak@gmail.com> wrote:
Hello list. I am confused about importing numpy.
When I do
from numpy import *
and I try the min function, I get the default Python min function.
On the other hand, when I do
import numpy as np
and use the np.min function, I get the numpy min function (which is obviously what I want).
I know, the latter is preferred, but the former is so dang easy to type. I always thought both imports work the same, but I am obviously wrong. What's the difference?
We specifically exclude those names which would override builtins. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

I guess that makes sense on a certain level, but boy it is cumbersome to explain to a class. It pretty much defeats the whole purpose of doing from numpy import *. Mark On Aug 6, 12:03 pm, "Robert Kern" <robert.k...@gmail.com> wrote:
On Wed, Aug 6, 2008 at 05:00, mark <mark...@gmail.com> wrote:
Hello list. I am confused about importing numpy.
When I do
from numpy import *
and I try the min function, I get the default Python min function.
On the other hand, when I do
import numpy as np
and use the np.min function, I get the numpy min function (which is obviously what I want).
I know, the latter is preferred, but the former is so dang easy to type. I always thought both imports work the same, but I am obviously wrong. What's the difference?
We specifically exclude those names which would override builtins.
-- Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ Numpy-discussion mailing list Numpy-discuss...@scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion

Anything that defeats the purpose of doing * imports is good in my book. :-) Seriously, willy nilly import of any package into the base namespace is just asking for trouble. Tell your class to import numpy as np, then there will be no chance of confusion. Then later tell them about "from numpy import min,max,array". Or just have them do "min = np.min" etc. --bb On Wed, Aug 6, 2008 at 7:44 PM, mark <markbak@gmail.com> wrote:
I guess that makes sense on a certain level, but boy it is cumbersome to explain to a class. It pretty much defeats the whole purpose of doing from numpy import *.
Mark
On Aug 6, 12:03 pm, "Robert Kern" <robert.k...@gmail.com> wrote:
On Wed, Aug 6, 2008 at 05:00, mark <mark...@gmail.com> wrote:
Hello list. I am confused about importing numpy.
When I do
from numpy import *
and I try the min function, I get the default Python min function.
On the other hand, when I do
import numpy as np
and use the np.min function, I get the numpy min function (which is obviously what I want).
I know, the latter is preferred, but the former is so dang easy to type. I always thought both imports work the same, but I am obviously wrong. What's the difference?
We specifically exclude those names which would override builtins.
-- Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

mark wrote:
I guess that makes sense on a certain level, but boy it is cumbersome to explain to a class. It pretty much defeats the whole purpose of doing from numpy import *.
which is fine by me: "Namespaces are one honking great idea -- let's do more of those!" explain namespaces to your class -- that will teach them something truly useful. -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

mark wrote:
Hello list. I am confused about importing numpy.
When I do
from numpy import *
and I try the min function, I get the default Python min function.
On the other hand, when I do
import numpy as np
and use the np.min function, I get the numpy min function (which is obviously what I want).
I know, the latter is preferred, but the former is so dang easy to type. I always thought both imports work the same, but I am obviously wrong. What's the difference?
While I agree with the other posters that "import *" is not preferred, if you want to use it, the solution is to use amin and amax, which are provided precisely to avoid the conflict. Just as arange is a numpy analog of range, amin and amax are numpy analogs of min and max. Eric
Thanks, Mark _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion

Thanks for the encouragement to try to dive deeper into namespaces, but also thanks for the amin, amax suggestions. Mark On Aug 6, 8:21 pm, Eric Firing <efir...@hawaii.edu> wrote:
mark wrote:
Hello list. I am confused about importing numpy.
When I do
from numpy import *
and I try the min function, I get the default Python min function.
On the other hand, when I do
import numpy as np
and use the np.min function, I get the numpy min function (which is obviously what I want).
I know, the latter is preferred, but the former is so dang easy to type. I always thought both imports work the same, but I am obviously wrong. What's the difference?
While I agree with the other posters that "import *" is not preferred, if you want to use it, the solution is to use amin and amax, which are provided precisely to avoid the conflict. Just as arange is a numpy analog of range, amin and amax are numpy analogs of min and max.
Eric
Thanks, Mark _______________________________________________ Numpy-discussion mailing list Numpy-discuss...@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discuss...@scipy.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion

2008/8/6 Eric Firing <efiring@hawaii.edu>:
While I agree with the other posters that "import *" is not preferred, if you want to use it, the solution is to use amin and amax, which are provided precisely to avoid the conflict. Just as arange is a numpy analog of range, amin and amax are numpy analogs of min and max.
There are actually several analogs of min and max, depending on what you want. np.minimum(a,b) takes the elementwise minimum, that is, np.minimum([1,2,3],[3,2,1]) is [1,2,1]. np.amin(a) finds the minimum value in the array a. (It's equivalent to np.minimum.reduce(a).) One reason automatically overriding min is a Bad Idea is that you will certainly shoot yourself in the foot: In [4]: numpy.min(2,1) Out[4]: 2 In [5]: min(2,1) Out[5]: 1 Note that this does not raise any kind of error, and it sometimes returns the right value. It's especially bad if you do, say, numpy.max(a,0) and expect the result to be always positive: now you get a bug in your program that only turns up with exceptional values. numpy.min is not the same as python's min, and using it as a substitute will get you in trouble. Anne
participants (6)
-
Anne Archibald
-
Bill Baxter
-
Christopher Barker
-
Eric Firing
-
mark
-
Robert Kern