Posts: 4
Threads: 1
Joined: Apr 2026
Hi all,
I'm new to python. However I've stumbled upon a problem I find difficult to grasp.
The code below search for a an index number where the "index_y1" is below 15000 (value of G_SP).
print(seB10)
print(type(seB10)) gives the output:
Output: [5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02
3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05
2.76727749e+06 1.41841611e+07]
<class 'numpy.ndarray'>
print(G_SP)
print(type(G_SP)) gives the output
Output: 15000
<class 'int'>
I've browsed the net and tried the max() and min() function without success. I stumbled upon this solution which did the trick...
index_y1 = np.where(seB10<abs(G_SP))
index_y1 = sorted(index_y1) #Closest below 15
index_y1 = index_y1[-1]
print(index_y1[-1])
print(index_y1)
print(type(index_y1[-1]))
print(type(index_y1)) this leads to the output:
Output: 5
[0 1 2 3 4 5]
<class 'numpy.int64'>
<class 'numpy.ndarray'>
however... I don't seem to be able to assign the index value obtained to a variable.
Why does not this work:
Quote:index_y1 = index_y1[-1]
?
and why does this code
index_y1=max(index_y1)
print(index_y1)
print(type(index_y1)) give this output
Output: [0 1 2 3 4 5]
<class 'numpy.ndarray'>
Why does it not just give me the answer "5"?
[0 1 2 3 4 5]
<class 'numpy.ndarray'>
Posts: 6,981
Threads: 22
Joined: Feb 2020
Apr-13-2026, 09:22 PM
(This post was last modified: Apr-13-2026, 09:22 PM by deanhystad.)
Maybe this helps.
import numpy as np
seB10 = np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
2.76727749e+06, 1.41841611e+07])
G_SP = 1500
index_y1 = np.where(seB10 < abs(G_SP))
print(index_y1, type(index_y1), len(index_y1)) Output: (array([0, 1, 2, 3]),) <class 'tuple'> 1
np.where() returned a tuple. array[0, 1, 2, 3] is the one and only value in the tuple. np.where returns a tuple because it may return multiple results based on the shape of the arguments.
When you call max(index_y1) it compares the values in the tuple and returns the one that is greater than all the rest. array([0, 1, 2, 3] is greater than any other value in index_y1.
So this actually did something, just not what you expected.
index_y1 = index_y1[-1] index_y1 was (array([0, 1, 2, 3]),), and after the assignment index_y1 = array([0, 1, 2, 3]) (an array, not a tuple containing an array).
You want to do this:
import numpy as np
seB10 = np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
2.76727749e+06, 1.41841611e+07])
G_SP = 1500
index_y1 = max(np.where(seB10 < abs(G_SP))[0])
print(index_y1, type(index_y1)) Output: 3 <class 'numpy.int64'>
[0] returns the first element in the tuple, array[0, 1, 2, 3], and we use max on that to get the maximum index.
Now you probably know that the sorted in your example did not do what you thought. Instead of sorting the values in the array, it sorted the arrays (one array) in the tuple.
import numpy as np
seB10 = np.array(
[
5.07730000e01, 1.19360515e02, .17627965e02, 9.71699674e02, 3.47047540e03,
1.46524911e04, 7.31078184e04, 4.21952327e05, 2.76727749e06, 1.41841611e07,
]
)
G_SP = 1500
a = np.where(seB10 < abs(G_SP))
b = sorted(a)
print(a, b)Output: (array([0, 1, 2, 3]),) [array([0, 1, 2, 3])]
Sorted did do one thing. Notice that a is a tuple (enclosed in parenthesis) and b is a list (enclosed in square brackets). a[0] and b[0] are both the same numpy array.
Posts: 4
Threads: 1
Joined: Apr 2026
(Apr-13-2026, 09:22 PM)deanhystad Wrote: Maybe this helps.
import numpy as np
seB10 = np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
2.76727749e+06, 1.41841611e+07])
G_SP = 1500
index_y1 = np.where(seB10 < abs(G_SP))
print(index_y1, type(index_y1), len(index_y1)) Output: (array([0, 1, 2, 3]),) <class 'tuple'> 1 np.where() returned a tuple. array[0, 1, 2, 3] is the one and only value in the tuple. np.where returns a tuple because it may return multiple results based on the shape of the arguments.
When you call max(index_y1) it compares the values in the tuple and returns the one that is greater than all the rest. array([0, 1, 2, 3] is greater than any other value in index_y1.
So this actually did something, just not what you expected.
index_y1 = index_y1[-1] index_y1 was (array([0, 1, 2, 3]),), and after the assignment index_y1 = array([0, 1, 2, 3]) (an array, not a tuple containing an array).
You want to do this:
import numpy as np
seB10 = np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
2.76727749e+06, 1.41841611e+07])
G_SP = 1500
index_y1 = max(np.where(seB10 < abs(G_SP))[0])
print(index_y1, type(index_y1)) Output: 3 <class 'numpy.int64'> [0] returns the first element in the tuple, array[0, 1, 2, 3], and we use max on that to get the maximum index.
Now you probably know that the sorted in your example did not do what you thought. Instead of sorting the values in the array, it sorted the arrays (one array) in the tuple.
import numpy as np
seB10 = np.array(
[
5.07730000e01, 1.19360515e02, .17627965e02, 9.71699674e02, 3.47047540e03,
1.46524911e04, 7.31078184e04, 4.21952327e05, 2.76727749e06, 1.41841611e07,
]
)
G_SP = 1500
a = np.where(seB10 < abs(G_SP))
b = sorted(a)
print(a, b)Output: (array([0, 1, 2, 3]),) [array([0, 1, 2, 3])] Sorted did do one thing. Notice that a is a tuple (enclosed in parenthesis) and b is a list (enclosed in square brackets). a[0] and b[0] are both the same numpy array.
Thank you for that extensive and good clarification. I'll dig in to this.
Much appreciated!
Posts: 1,300
Threads: 151
Joined: Jul 2017
Apr-14-2026, 12:26 PM
(This post was last modified: Apr-14-2026, 12:26 PM by Pedroski55.)
Not so difficult I think:
import numpy as np
array =np.array([5.07730000e+01, 1.19360515e+02, 3.17627965e+02, 9.71699674e+02,
3.47047540e+03, 1.46524911e+04, 7.31078184e+04, 4.21952327e+05,
2.76727749e+06, 1.41841611e+07])
# convert to Python list for easy handling
nums = array.tolist()
wanted = 15000
max_num = 0
for num in nums:
if num <= wanted:
print(num)
if num > max_num:
max_num = num
print(max_num) # 14652.4911
max_num_index = nums.index(max_num) # 5
Posts: 6,981
Threads: 22
Joined: Feb 2020
I was wrong in my post. I think you might want to do this:
]index_y1 = np.where(seB10 < abs(G_SP))[0][-1] There's no reason to use max since np.where()[0] returns an array that only contains indices to values that match the criteria, and the index values are in increasing order.
Looking back at your post, you are depending a lot on the values in seB10 being in order. If the values are in random order, picking the last index doesn't work. Solving for the random order case is tricky, especially if you want the index of the value closest to G_SP in the original list.
What do you want to accomplish?
Posts: 4
Threads: 1
Joined: Apr 2026
Quote:I was wrong in my post. I think you might want to do this:
1]index_y1 = np.where(seB10 < abs(G_SP))[0][-1]
There's no reason to use max since np.where()[0] returns an array that only contains indices to values that match the criteria, and the index values are in increasing order.
Looking back at your post, you are depending a lot on the values in seB10 being in order. If the values are in random order, picking the last index doesn't work. Solving for the random order case is tricky, especially if you want the index of the value closest to G_SP in the original list.
What do you want to accomplish?
What I really want is to find the value hidden in G_SP (value is: 15000) by interpolating between data setpoints in "seB10". And being able to plot this in a graph.
I did actually play around with this and was successful.
The code looks like this:
print(G_SP) gives
Output: 15000
print(seB10, np.flip(saB10)) gives
Output: [5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02
3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05
2.76727749e+06 1.41841611e+07] [100. 90. 80. 70. 60. 50. 40. 30. 20. 10.]
res_sp=cal_interp_poly(G_SP, np.flip(saB10), seB10) print(res_sp) gives
Output: 49.854168650477924
The function called to determine it all using interpolation:
def cal_interp_poly(ltf, arr_x, arr_y): #works
arr_y = np.array(arr_y)
arr_x = np.array(arr_x)
index_y1 = max(np.where(arr_y < abs(ltf))[0]) #index where value is closest under ltf (limit to find)
index_y2 = min(np.where(arr_y > abs(ltf))[0]) #index where value is closest over ltf (limit to find)
y1=arr_y[index_y1]
y2=arr_y[index_y2]
x1=arr_x[index_y1] # grabs the temperature where the value was found
x2=arr_x[index_y2] # same as above
x_known=[x1, x2]
y_known=[np.log10(y1), np.log10(y2)] # correct values
x_interpolate=ltf
y_interpolate = np.interp(x_interpolate, x_known, y_known)
coeff=np.polyfit(x_known,y_known,1)
sLope=coeff[0]
y_int=coeff[1]
result_cal_interp_poly=(np.log10(abs(ltf))-y_int)/sLope # this will find the value where limit (ltf) is
return result_cal_interp_poly The graphs:
If I zoom in you can see that it actually interpolate properly
I guess it takes time to get skilled and see obvious flaws in python but I hope I get there eventually.
Posts: 1,300
Threads: 151
Joined: Jul 2017
Dunno what all that is doing. Seems complicated! If you want to iterpolate between 2 points P1 and P2:
Let dx be the difference between P1 and P2 along the x axis, let dy be the difference between P1 and P2 on the y axis.
The Point Pi is then: (x + dx / 2, y + dy / 2) and the slope is dy / dx.
# make some points for a graph
x = [n for n in range(10)]
y = [n**2 for n in range(10)]
points = list(zip(x, y))
def get_inter(p1, p2):
dx =p2[0] - p1[0]
dy = p2[1] - p1[1]
Pi = (p1[0] + dx / 2, p1[1] + dy / 2)
return Pi
for i in range(len(points) - 1):
print(get_inter(points[i], points[i+1]))Of course, as dx goes to zero, well, hope you can get rid of it before that happens!
Posts: 6,981
Threads: 22
Joined: Feb 2020
Apr-16-2026, 05:50 PM
(This post was last modified: Apr-16-2026, 05:50 PM by deanhystad.)
Quote:print(seB10, np.flip(saB10)) gives
Output:
Output: [5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02
3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05
2.76727749e+06 1.41841611e+07] [100. 90. 80. 70. 60. 50. 40. 30. 20. 10.]
No, it doesn't. The output is:
[5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02
3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05
2.76727749e+06 1.41841611e+07] [1.41841611e+07 2.76727749e+06 4.21952327e+05 7.31078184e+04
1.46524911e+04 3.47047540e+03 9.71699674e+02 3.17627965e+02
1.19360515e+02 5.07730000e+01] numpy flip creates a new array with the values in reverse order. I don't know where [100. 90....] came from, but not from np.flip.
I don't understand the purpose of this:
index_y1 = max(np.where(arr_y < abs(ltf))[0]) #index where value is closest under ltf (limit to find)
index_y2 = min(np.where(arr_y > abs(ltf))[0]) #index where value is closest over ltf (limit to find)If arr_y is monotonic, there's no reason for using max (use last index), and index_y2 = index_y1 + 1. If arr_y is not monotonic the code does not detect which value(s) in arr_y are closest to ltf.
Posts: 4
Threads: 1
Joined: Apr 2026
[quote="deanhystad" pid='189807' dateline='1776361856']
Quote:print(seB10, np.flip(saB10)) gives
Output:
Output: [5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02
3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05
2.76727749e+06 1.41841611e+07] [100. 90. 80. 70. 60. 50. 40. 30. 20. 10.]
No, it doesn't. The output is:
[5.07730000e+01 1.19360515e+02 3.17627965e+02 9.71699674e+02
3.47047540e+03 1.46524911e+04 7.31078184e+04 4.21952327e+05
2.76727749e+06 1.41841611e+07] [1.41841611e+07 2.76727749e+06 4.21952327e+05 7.31078184e+04
1.46524911e+04 3.47047540e+03 9.71699674e+02 3.17627965e+02
1.19360515e+02 5.07730000e+01] Well I think you need to look again.
print(seB10, np.flip(saB10)) "s eB10" is not the same as "s aB10"
Posts: 6,981
Threads: 22
Joined: Feb 2020
Quote:"seB10" is not the same as "saB10"
Having to point that out may indicate you need a different naming convention.
Now I have a different question. Why does s aB10 need to be flipped? If you're interpolating, seB10 and saB10 must be related, why is saB10 in reverse order?
|