[SciPy-user] Unexpected behavior (bug?): scipy x Numeric.argmin

Robert Kern rkern at ucsd.edu
Sun Oct 10 15:19:00 CDT 2004


krivilli at unberwoot.net wrote:
> HI,
> 
> -I got myself confused by a strange (for me at least) 'problem' since I
> don’t  know much about Numpy and how, sometimes, some operations that
> seems to be the same, aren’t (like matrix[i,j] instead of matrix[i][j]).
> 
> -So let me show what my problem is:
> 
> 
> ####################
> import Numeric
> 
> 
> def MakeLC(matrix,vz):
>     tmp=matrix>=vz # boolean matrix
>     tmp=Numeric.argmin(tmp,0)
> 
>     print tmp
> 
> 
> if __name__=="__main__":
> 
>     Matrix = [
>     Numeric.zeros(20),
>     Numeric.array([-0.0123,-0.0156,-0.0199,-0.0254,-0.0267,-0.0285,-0.313,-0.376,-0.0475,-0.0534,-0.0578,-0.0634,-0.0679,-0.0735,-0.0879,-0.0981,-0.112,-0.115,-0.116,-0.118]),
>     Numeric.array([0.0023,
> 0.0056,0.0099,0.0054,0.0067,0.0085,0.013,0.076,0.0075,0.0034,0.0078,0.0034,0.0079,0.0035,0.0079,0.0081,0.012,0.015,0.016,0.018]),
>     Numeric.array([0.0103,0.0106,0.0109,0.0204,0.0207,0.0205,0.303,0.306,0.0405,0.0504,0.0508,0.0604,0.0609,0.0705,0.0809,0.0901,0.102,0.105,0.106,0.108]),
>     Numeric.array([0.1120,0.1006,0.1009,0.1004,0.1007,0.1200,0.100,0.206,0.2405,0.2504,0.2508,0.2604,0.2609,0.2705,0.2809,0.3081,0.302,0.335,0.306,0.318]),
>     Numeric.array([0.0983,0.0876,0.0854,0.0834,0.0813,0.0798,0.776,0.071,0.0678,0.0630,0.0634,0.0610,0.0588,0.0564,0.0580,0.0522,0.050,0.048,0.040,0.036])]
>     Matrix = Numeric.array(Matrix)
> 
>     LC=MakeLC(Matrix,-0.0564)
> 
> ####################
> 
> 
> -Output for this is:
> 
> [0 0 0 0 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1]
> 
> 
> -But when I have "import scipy" on the head of file like in:
> 
> 
> ####################
> import scipy
> import Numeric
> 
> .
> .
> .
> (rest is identical)
> 
> 
> 
> 
> -The output is now:
> 
> [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
> 
> 
> -The same occurs even if I don’t use 'import Numeric' and use
> 'scipy.argmin', 'scipy.zeros' and 'scipy.array' instead.
> 
> -The 'problem' seems to go away when we replace in Numeric.py, the
> following line (line:295, Numpy version 23.5)
> 
> 
> 295c295
> <     a = -array(a, copy=0)
> ---
> 
>>    a = -1*array(a, copy=0)

SciPy alters the comparison functions to return UInt8 arrays.

In [1]: tmp = array([0,0,1,1], UInt8)

In [2]: tmp
Out[2]: NumPy array, format: long
[0 0 1 1]

In [3]: -tmp
Out[3]: NumPy array, format: long
[  0   0 255 255]

You can work around this by casting tmp to some signed type before 
passing it to argmin().

However, I believe you should be using the functions 
logical_not(alltrue(tmp)) instead of argmin(tmp). Those functions 
accurately represent the semantics of the operation you want to perform.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the SciPy-user mailing list