根据另一个数组的值提取数组的值 PYTHON

Extracting value of an array based on the value of another array PYTHON

第一次在这里发帖。我需要帮助解决我面临的问题,尽管我已经在网上搜索过,但我找不到答案。所以:

我有两个数组,flux 和 flux_err。

 flux = np.array(folded2_lc_binned.flux.value, dtype = 'float')

 flux_err = np.array(folded2_lc_binned.flux_err.value, dtype = 'float')

这给了我以下结果:

flux = [0.99996269 1.0000602  1.00017059 1.00002182 1.00007594 0.9999696
 1.00011313 1.00012934 1.00005817 0.99997538 0.99956554 0.99896783
 0.99861592 0.99828523 0.99859077 0.9990297  0.9994933  0.99997085
 1.00000501 0.999919   1.00013363 1.00007749 1.00005972 1.00003064
 0.99999666 1.00003886 1.00002563 1.00004816]

flux_err = [5.34750658e-05 5.23316427e-05 5.29300918e-05 5.33181581e-05
 5.30598314e-05 5.34477987e-05 5.35483605e-05 5.35784012e-05
 5.35678339e-05 5.33819678e-05 5.28453506e-05 5.28792032e-05
 5.28458148e-05 5.31577965e-05 5.36710824e-05 5.40498860e-05
 5.38263520e-05 5.38960178e-05 5.38837160e-05 5.41886097e-05
 5.34210296e-05 5.31687131e-05 5.30733841e-05 5.29602811e-05
 5.38159642e-05 5.35264592e-05 5.34654396e-05 6.24225799e-05]

问题:我计算数组的最小值'flux':

Flux min value: 0.9982852339744568

如何计算数组'flux_err'中对应的值(位于同一个index/position中的值)?我试过 astype(bool) 但它给出了所需数组的最小值(这是不正确的)而不是我需要的相应值。

非常感谢。

因为你已经在使用 numpy 作为依赖,你可以使用 np.argmin 函数,它 returns 的最小值的索引数组。然后你可以使用索引 returned 来索引 flux_err.

在你的情况下,应该是这样的:

min_idx = np.argmin(flux)
min_err = flux_err[min_idx]

注意 argmin() 可能 return 多个 索引以防最小值在数组中重复。

P.S.: 除非我没有理解你的意思,否则 astype(bool) 与此无关。