[AstroPy] How can I take the index of "True" value and minimum "True" in list with python ?

Trey Wenger tvw2pu at virginia.edu
Tue Apr 8 18:12:14 EDT 2014


Hi again,

Third time is the charm!

You can do this to mask out the array where the difference is greater than
0.02:

IPy> diff = abs(E - gE0)*(abs(E - gE0) < 0.02)
IPy> diff
Out>
array([[ 0.        ,  0.00178256,  0.00674264,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.00499484,  0.00321228,  0.00174779,  0.0155495 ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.01439379,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.00162705,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.00335728,
         0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])

Now, to get the minima that fulfill this criterion, get the minimum from
the non-zero values in this diff array:

IPy> min_values = [(-1 if len(d.nonzero()[0]) == 0 else
min(d[d.nonzero()[0]])) for d in diff]
IPy> min_values
Out>
[0.0017825594000000001,
 0.00174779433,
 0.014393789699999996,
 -1,
 -1,
 -1,
 -1,
 -1,
 0.0016270488999999971,
 0.0033572820000000031]

So, the min_values list is filled with -1 if there is difference below 0.2,
otherwise it is filled with the minimum difference.

Hope this helps!
Trey




On Tue, Apr 8, 2014 at 5:31 PM, THƯỜNG Hoàng Đức <hoangthuong.hust at gmail.com
> wrote:

> HI Trey,
>
> I wish you may understand. "You are tying to find the minimum of abs(E -
> gE0) for every sub-array in gE0 ". It is not for every sub_array(for
> diferent location in array).
>  Firstly it have condition : abs(E - gE0) < 0.02. And after that I am
> typing to take the closest value E-gE0 == 0 (It also is minimum) therefor
> maybe some sub_arrays have not value. I am sorry, It is complicate to
> understand. Hopefully you can help me.
>
> I will show you a simple example:(The main ideal of my problem)
>
> I have array ar = [1,8,9,10,100,101,102,1000,1001,1002,1003,5000]
>
> I want to take I = [1,8,100,1000,5000] because 1 is alone and non value
> follow. 8 is minimum of [8,9,10] and 8,9,10 is follow together.....
>
> Thank you - Merci
> Thuong
>
>
> 2014-04-08 18:45 GMT+02:00 Trey Wenger <tvw2pu at virginia.edu>:
>
>> Hi again, Thuong.
>>
>> I think I understand what you are tying to do. You are tying to find the
>> minimum of abs(E - gE0) for every sub-array in gE0. This is what I would do:
>>
>> min_indicies = [abs(E - my_gE0).argmin() for my_gE0 in gE0]
>>
>> So, min_indicies[0] is the index of the minimum of abs(E-gE0[0]),
>> min_indicies[1] is the index of the minimum of abs(E - gE0[1]), etc.
>>
>> Hope this helps,
>> Trey
>>
>>
>>
>> On Tue, Apr 8, 2014 at 12:20 PM, THƯỜNG Hoàng Đức <
>> hoangthuong.hust at gmail.com> wrote:
>>
>>> thank you Trey,
>>>
>>> How can i find the minimum value for different location in the list. Let
>>> me show you more detail: (I am sorry, it is long )
>>>
>>> I have:
>>> In [15]: E
>>> Out[15]:
>>> array([  1.00000000e-03,   2.78255940e-03,   7.74263683e-03,
>>>          2.15443469e-02,   5.99484250e-02,   1.66810054e-01,
>>>          4.64158883e-01,   1.29154967e+00,   3.59381366e+00,
>>>          1.00000000e+01])
>>>
>>> In [16]: gE0
>>> Out[16]:
>>> array([[  1.00000000e-03,   1.00000000e-03,   1.00000000e-03,
>>>           1.00000000e-03,   1.00000000e-03,   1.00000000e-03,
>>>           1.00000000e-03,   1.00000000e-03,   1.00000000e-03,
>>>           1.00000000e-03],
>>>        [  5.99484250e-03,   5.99484250e-03,   5.99484250e-03,
>>>           5.99484250e-03,   5.99484250e-03,   5.99484250e-03,
>>>           5.99484250e-03,   5.99484250e-03,   5.99484250e-03,
>>>           5.99484250e-03],
>>>        [  3.59381366e-02,   3.59381366e-02,   3.59381366e-02,
>>>           3.59381366e-02,   3.59381366e-02,   3.59381366e-02,
>>>           3.59381366e-02,   3.59381366e-02,   3.59381366e-02,
>>>           3.59381366e-02],
>>>        [  2.15443469e-01,   2.15443469e-01,   2.15443469e-01,
>>>           2.15443469e-01,   2.15443469e-01,   2.15443469e-01,
>>>           2.15443469e-01,   2.15443469e-01,   2.15443469e-01,
>>>           2.15443469e-01],
>>>        [  1.29154967e+00,   1.29154967e+00,   1.29154967e+00,
>>>           1.29154967e+00,   1.29154967e+00,   1.29154967e+00,
>>>           1.29154967e+00,   1.29154967e+00,   1.29154967e+00,
>>>           1.29154967e+00],
>>>        [  1.18292276e+00,   1.20239338e+00,   1.26419499e+00,
>>>           1.38016526e+00,   1.57688572e+00,   1.91417885e+00,
>>>           2.55149610e+00,   4.10977947e+00,   7.74263683e+00,
>>>           7.74263683e+00],
>>>        [  2.77831168e-01,   2.82126806e-01,   2.95703802e-01,
>>>           3.20921794e-01,   3.62928377e-01,   4.32761236e-01,
>>>           5.56895376e-01,   8.15796501e-01,   1.61701932e+00,
>>>           4.64158883e+01],
>>>        [  7.88832931e-02,   8.01002871e-02,   8.39462486e-02,
>>>           9.10876310e-02,   1.02977169e-01,   1.22725700e-01,
>>>           1.57778342e-01,   2.30672383e-01,   4.54431889e-01,
>>>           2.78255940e+02],
>>>        [  4.46767553e-02,   4.53659668e-02,   4.75440123e-02,
>>>           5.15882797e-02,   5.83213761e-02,   6.95047231e-02,
>>>           8.93535809e-02,   1.30626292e-01,   2.57284143e-01,
>>>           1.66810054e+03],
>>>        [  4.84949885e-02,   4.92431017e-02,   5.16072881e-02,
>>>           5.59971873e-02,   6.33057070e-02,   7.54447993e-02,
>>>           9.69899542e-02,   1.41789797e-01,   2.79271195e-01,
>>>           1.00000000e+04]])
>>>
>>> In [25]: (abs(E - gE0) < 0.02)
>>> Out[25]:
>>> array([[ True,  True,  True, False, False, False, False, False, False,
>>>         False],
>>>        [ True,  True,  True,  True, False, False, False, False, False,
>>>         False],
>>>        [False, False, False,  True, False, False, False, False, False,
>>>         False],
>>>        [False, False, False, False, False, False, False, False, False,
>>>         False],
>>>        [False, False, False, False, False, False, False,  True, False,
>>>         False],
>>>        [False, False, False, False, False, False, False, False, False,
>>>         False],
>>>        [False, False, False, False, False, False, False, False, False,
>>>         False],
>>>        [False, False, False, False, False, False, False, False, False,
>>>         False],
>>>        [False, False, False, False,  True, False, False, False, False,
>>>         False],
>>>        [False, False, False, False,  True, False, False, False, False,
>>>         False]], dtype=bool)
>>>
>>> In [26]: abs(E - gE0)
>>> Out[26]:
>>> array([[  0.00000000e+00,   1.78255940e-03,   6.74263683e-03,
>>>           2.05443469e-02,   5.89484250e-02,   1.65810054e-01,
>>>           4.63158883e-01,   1.29054967e+00,   3.59281366e+00,
>>>           9.99900000e+00],
>>>        [  4.99484250e-03,   3.21228310e-03,   1.74779432e-03,
>>>           1.55495044e-02,   5.39535825e-02,   1.60815211e-01,
>>>           4.58164041e-01,   1.28555482e+00,   3.58781882e+00,
>>>           9.99400516e+00],
>>>        [  3.49381366e-02,   3.31555772e-02,   2.81954998e-02,
>>>           1.43937897e-02,   2.40102884e-02,   1.30871917e-01,
>>>           4.28220747e-01,   1.25561153e+00,   3.55787553e+00,
>>>           9.96406186e+00],
>>>        [  2.14443469e-01,   2.12660910e-01,   2.07700832e-01,
>>>           1.93899122e-01,   1.55495044e-01,   4.86334153e-02,
>>>           2.48715414e-01,   1.07610620e+00,   3.37837019e+00,
>>>           9.78455653e+00],
>>>        [  1.29054967e+00,   1.28876711e+00,   1.28380703e+00,
>>>           1.27000532e+00,   1.23160124e+00,   1.12473961e+00,
>>>           8.27390782e-01,   1.33226763e-15,   2.30226400e+00,
>>>           8.70845033e+00],
>>>        [  1.18192276e+00,   1.19961082e+00,   1.25645235e+00,
>>>           1.35862091e+00,   1.51693729e+00,   1.74736880e+00,
>>>           2.08733721e+00,   2.81822980e+00,   4.14882316e+00,
>>>           2.25736317e+00],
>>>        [  2.76831168e-01,   2.79344246e-01,   2.87961166e-01,
>>>           2.99377447e-01,   3.02979952e-01,   2.65951182e-01,
>>>           9.27364927e-02,   4.75753164e-01,   1.97679434e+00,
>>>           3.64158883e+01],
>>>        [  7.78832931e-02,   7.73177277e-02,   7.62036117e-02,
>>>           6.95432841e-02,   4.30287441e-02,   4.40843535e-02,
>>>           3.06380541e-01,   1.06087728e+00,   3.13938177e+00,
>>>           2.68255940e+02],
>>>        [  4.36767553e-02,   4.25834074e-02,   3.98013755e-02,
>>>           3.00439328e-02,   1.62704897e-03,   9.73053306e-02,
>>>           3.74805302e-01,   1.16092337e+00,   3.33652952e+00,
>>>           1.65810054e+03],
>>>        [  4.74949885e-02,   4.64605423e-02,   4.38646513e-02,
>>>           3.44528404e-02,   3.35728195e-03,   9.13652544e-02,
>>>           3.67168929e-01,   1.14975987e+00,   3.31454247e+00,
>>>           9.99000000e+03]])
>>>
>>> and Finaly:
>>> At the first red line. I would to take the index[2] of value
>>> 1.78255940e-03 (minimum)
>>> At the second line. I would to take the index of value  1.74779432e-03
>>> (minimum)
>>> .
>>> .
>>> .
>>>
>>> May you try to help me
>>>
>>> Thuong,
>>>
>>>
>>> 2014-04-08 16:09 GMT+02:00 Trey Wenger <tvw2pu at virginia.edu>:
>>>
>>>> Hi Thuong,
>>>>
>>>> I would do this
>>>>
>>>> index = abs(gE0[:,0] - ek).argmin()
>>>>
>>>> .argmin() returns the index at which the previous argument is smallest.
>>>> Now, gE0[;,0][index] will be the value of gE0[;,0] closest to ek.
>>>>
>>>> For example,
>>>>
>>>> IPy> x = np.array([1.1, 2.3, 4.4])
>>>> IPy> y = 2.2
>>>> IPy> index = np.abs(x-y).argmin()
>>>> IPy> index
>>>> Out> 1
>>>> IPy> x[index]
>>>> Out> 2.3
>>>> IPy> # this is the value in x closest to y
>>>>
>>>> Cheers,
>>>> Trey
>>>>
>>>>
>>>> On Tue, Apr 8, 2014 at 9:57 AM, THƯỜNG Hoàng Đức <
>>>> hoangthuong.hust at gmail.com> wrote:
>>>>
>>>>> Dear AstroPyer,
>>>>>
>>>>> I have problem with ideals !
>>>>> Firstly, I have two array. "ek" (one dimension - value in logspace)
>>>>> and "gE0[]" (two dimension - value in logspace). After that I scan value ek
>>>>> in array gE0 to find the equal value, It mean that abs(ek-gE0[:,0]) = 0.
>>>>> Due to the numerical that in logspace, it can not equal zeros(There are
>>>>> some point near the 0), and then I put the error for example err < 0.08
>>>>> >>>> abs(ek-gE0[:,0]) < 0.08. You can see the value below. The result I
>>>>> would to take is True but it has several value < 0.08 also true, then how
>>>>> can i take smallest if have several True.
>>>>>
>>>>>
>>>>> In [53]: abs(ek-gE0[:,0]) < 0.08
>>>>> Out[53]: array([False,  True, False, False, False, False,  True,
>>>>> True,  True,  True], dtype=bool)
>>>>>
>>>>> ek = 0.1
>>>>> In [52]: gE0[:,0]
>>>>> Out[52]:
>>>>> array([ 0.01      ,  0.04641589,  0.21544347,  1.        ,  1.93593016,
>>>>>         0.51306049,  0.15479754,  0.06105322,  0.04410808,
>>>>> 0.04849499])
>>>>>
>>>>> How can i take only the value: i = 1, value = 0.04641589 and i = 9,
>>>>> value = 0.04410808
>>>>>
>>>>>
>>>>> Thankyou very muck
>>>>>
>>>>>  Merci beaucoup,
>>>>>
>>>>> Thuong
>>>>>
>>>>> ================================================
>>>>> THUONG Hoang Duc
>>>>>
>>>>> Université des Sciences et des Technologies de Hanoi(USTH)
>>>>> University of Science and Technology of Hanoi(USTH)
>>>>> Hanoi University of Science and Technology (HUST)
>>>>>
>>>>> Email: hoangthuong.hust at gmail.com
>>>>> Tel: +33 06 52 92 00 96
>>>>> Tel: +84 01692887738
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> AstroPy mailing list
>>>>> AstroPy at scipy.org
>>>>> http://mail.scipy.org/mailman/listinfo/astropy
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Trey V. Wenger
>>>> Department of Astronomy
>>>> University of Virginia
>>>> http://www.astro.virginia.edu/~tvw2pu/
>>>>
>>>
>>>
>>>
>>> --
>>>
>>> ================================================
>>> THUONG Hoang Duc
>>>
>>> Université des Sciences et des Technologies de Hanoi(USTH)
>>> University of Science and Technology of Hanoi(USTH)
>>> Hanoi University of Science and Technology (HUST)
>>>
>>> Email: hoangthuong.hust at gmail.com
>>> Tel: +33 06 52 92 00 96
>>> Tel: +84 01692887738
>>>
>>>
>>
>>
>> --
>> Trey V. Wenger
>> Department of Astronomy
>> University of Virginia
>> http://www.astro.virginia.edu/~tvw2pu/
>>
>
>
>
> --
>
> ================================================
> THUONG Hoang Duc
>
> Université des Sciences et des Technologies de Hanoi(USTH)
> University of Science and Technology of Hanoi(USTH)
> Hanoi University of Science and Technology (HUST)
>
> Email: hoangthuong.hust at gmail.com
> Tel: +33 06 52 92 00 96
> Tel: +84 01692887738
>
>


-- 
Trey V. Wenger
Department of Astronomy
University of Virginia
http://www.astro.virginia.edu/~tvw2pu/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20140408/74473d7a/attachment.html>


More information about the AstroPy mailing list