Я пытаюсь автоматически найти как функцию минимального значения и максимума с выбранным допуском индексы массива, который находится в этих 2 интервалах. Для этого я сделал (если я хочу, чтобы значения массива были расположены между 7e-4 и 3e-3.
## Want to plot roughly between 7e-4 and 3e-3
minStep = 7e-4
maxStep = 3e-3
# Tolerance : could be calculated in theory
minTol = 1.5e-1
maxTol = 1.5e-1
for j in range(len(stepNewArray)):
if (np.isclose(stepNewArray[j], minStep, atol=minTol)):
# Find final index : don't forget to cast into integer
indexFirst = np.where((stepNewArray >= (minStep-minTol)) & (stepNewArray <= (minStep+minTol)))
else:
# Value not found
print('Minimum value not found : change tolerance')
for j in range(len(stepNewArray)):
if (np.isclose(stepNewArray[j], maxStep, atol=maxTol)):
# Find final index : don't forget to cast into integer
indexLast = np.where((stepNewArray >= (maxStep-maxTol)) & (stepNewArray <= (maxStep+maxTol)))
else:
# Value not found
print('Maximum value not found : change tolerance')
print('indexFirst ', np.array(indexFirst)[0])
print('indexLast ', np.array(indexLast)[0])
Но для вывода печати я получаю:
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
etc ...
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Minimum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
etc ...
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
Maximum value not found : change tolerance
('indexFirst ', array([95, 96, 97]))
('indexLast ', array([103, 104, 105, 106, 107, 108, 109, 110, 111, 112]))
###################################
Current parameter : Omega_m
###################################
idMultipole = 0
Traceback (most recent call last):
File "plot_Derivatives_INTERVAL_between_7e-4_AND_3e-3.py", line 253, in <module>
for idStep in range(indexFirst,indexLast+1):
TypeError: can only concatenate tuple (not "int") to tuple
Я не знаю, как преобразовать первый найденный индекс, соблюдая условия np.where
), в целое число или просто напечатать его.
Я пробовал также с:
print('indexFirst = %d ' % int(np.array(indexFirst[0])))
print('indexLast = %d ' % int(np.array(indexLast[0])))
Но я также получаю сообщение об ошибке:
print('indexFirst = %d ' % int(np.array(indexFirst[0])))
TypeError: only size-1 arrays can be converted to Python scalars
Я не знаю, что делать, чтобы получить первые индексы, соответствующие множественным условиям np.where
, распечатать их и в основном получить для них целочисленный тип (indexFirst и indexLast).
Я использую python2 .7 (знаю, извините, это устарело)
С уважением