python 中的 MATCH 函数?
MATCH function in python?
有没有办法在 Python 中执行 Excel match() 函数,这样:
在这样的图表中
...
...其中我在 y = 90 处截止,我想打印哪个对应的 x 值最接近。
根据我的调查,value/answer 应该是 4,但是我怎么可能将它打印或存储在变量中呢?
In: print(bss/tss*100)
Out: [ 1.21976032e-14 7.42743185e+01 8.51440985e+01 9.21584826e+01
9.59771981e+01 9.74117561e+01 9.82980987e+01 9.90505760e+01
9.92982678e+01 9.94756800e+01 9.96396123e+01 9.97126077e+01
9.97593424e+01 9.98030600e+01 9.98344280e+01 9.98692896e+01
9.98840717e+01 9.99020097e+01 9.99142963e+01]
你也可以用这个简单的功能把它打死。
它找到至少与目标值一样大的第一个值,检查前一个值,并 returns 更接近值的位置(基于 1,而不是基于 0)。
def match (table, target):
for over in range(len(table)):
if table[over] >= target:
break
# over is the index of the first vale at least as large as the target
# return the position (index+1) of the nearer value
return over+1 if 2*target > table[over-1] + table[over] \
else over
ss_table = [
1.21976032e-14, 7.42743185e+01, 8.51440985e+01, 9.21584826e+01,
9.59771981e+01, 9.74117561e+01, 9.82980987e+01, 9.90505760e+01,
9.92982678e+01, 9.94756800e+01, 9.96396123e+01, 9.97126077e+01,
9.97593424e+01, 9.98030600e+01, 9.98344280e+01, 9.98692896e+01,
9.98840717e+01, 9.99020097e+01, 9.99142963e+01
]
print match(ss_table, 87)
print match(ss_table, 90)
对于您的示例,这个 returns 3 和 4,根据需要。
您正在寻找 argmin 函数。
from numpy import *
print argmin(abs(data - threshold))
会找到与阈值偏差最小的index。指定数学关系 (argmin(abs(...))
) 比将其隐藏在模糊的 "match" 函数中要精确得多。这样,很清楚如何使用例如两个阈值(上阈值和下阈值),或非常数阈值函数。我们可以用同样的方法找到两个函数的最近点。
有没有办法在 Python 中执行 Excel match() 函数,这样:
在这样的图表中
...其中我在 y = 90 处截止,我想打印哪个对应的 x 值最接近。
根据我的调查,value/answer 应该是 4,但是我怎么可能将它打印或存储在变量中呢?
In: print(bss/tss*100)
Out: [ 1.21976032e-14 7.42743185e+01 8.51440985e+01 9.21584826e+01
9.59771981e+01 9.74117561e+01 9.82980987e+01 9.90505760e+01
9.92982678e+01 9.94756800e+01 9.96396123e+01 9.97126077e+01
9.97593424e+01 9.98030600e+01 9.98344280e+01 9.98692896e+01
9.98840717e+01 9.99020097e+01 9.99142963e+01]
你也可以用这个简单的功能把它打死。 它找到至少与目标值一样大的第一个值,检查前一个值,并 returns 更接近值的位置(基于 1,而不是基于 0)。
def match (table, target):
for over in range(len(table)):
if table[over] >= target:
break
# over is the index of the first vale at least as large as the target
# return the position (index+1) of the nearer value
return over+1 if 2*target > table[over-1] + table[over] \
else over
ss_table = [
1.21976032e-14, 7.42743185e+01, 8.51440985e+01, 9.21584826e+01,
9.59771981e+01, 9.74117561e+01, 9.82980987e+01, 9.90505760e+01,
9.92982678e+01, 9.94756800e+01, 9.96396123e+01, 9.97126077e+01,
9.97593424e+01, 9.98030600e+01, 9.98344280e+01, 9.98692896e+01,
9.98840717e+01, 9.99020097e+01, 9.99142963e+01
]
print match(ss_table, 87)
print match(ss_table, 90)
对于您的示例,这个 returns 3 和 4,根据需要。
您正在寻找 argmin 函数。
from numpy import *
print argmin(abs(data - threshold))
会找到与阈值偏差最小的index。指定数学关系 (argmin(abs(...))
) 比将其隐藏在模糊的 "match" 函数中要精确得多。这样,很清楚如何使用例如两个阈值(上阈值和下阈值),或非常数阈值函数。我们可以用同样的方法找到两个函数的最近点。