尝试将 Excel 模糊逻辑转换为 Python 函数
Trying to convert Excel Fuzzy logic to Python function
我目前在 Excel 中使用以下模糊逻辑命令 select 来自参考 table 的值:
=IF(E49>0,VLOOKUP(E49,'Ref Table'!$D:$E,2,FALSE),"--")
我正在尝试编写一个 Django/Python 函数来给出最接近所提供数字的值。 (示例:分数 = 14.5 - 返回值为 0.021)
我安装了 fuzzywuzzy,但我不确定这是实现它的最佳方式。
下面是到目前为止没有模糊逻辑的函数示例。
@register.simple_tag
def get_perc(score):
if score is None:
return '--'
else:
pct_dict = {
14: 0.016,
14.7: 0.021,
15.3: 0.026,
16: 0.034,
16.7: 0.04,
17.3: 0.05,
18: 0.07,
18.7: 0.09,
19.3: 0.11,
20: 0.13,
20.7: 0.17,
21.3: 0.21,
22: 0.26,
22.7: 0.31,
23.3: 0.38,
24: 0.47,
24.7: 0.56,
25.3: 0.68,
26: 0.82,
26.7: 0.98,
27.3: 1.17,
28: 1.39,
29.3: 1.94,
30: 2.28
}
if score in pct_dict.keys():
return pct_dict[score]
else:
return '--'
(示例:分数 = 14.5 - 返回值为 0.021)
如果您只是想使用 fuzzywuzzy 将输入与您的其中一个键进行模糊匹配,您可以尝试这样的操作:
from fuzzywuzzy import process
def get_perc(score):
# I put your dictionary up here so that it's always defined.
pct_dict = {
14: 0.016,
14.7: 0.021,
15.3: 0.026,
16: 0.034,
16.7: 0.04,
17.3: 0.05,
18: 0.07,
18.7: 0.09,
19.3: 0.11,
20: 0.13,
20.7: 0.17,
21.3: 0.21,
22: 0.26,
22.7: 0.31,
23.3: 0.38,
24: 0.47,
24.7: 0.56,
25.3: 0.68,
26: 0.82,
26.7: 0.98,
27.3: 1.17,
28: 1.39,
29.3: 1.94,
30: 2.28
}
MATCH_THRESHOLD = 80 # This is the minimum score needed to "match" a value
# you can change it as you like.
if not score: # I changed this, so that any "falsey" value will return '--'
# this includes values like '', None, 0, and False
return '--'
match, match_score = process.extractOne(score, pct_dict.keys())
if match_score >= MATCH_THRESHOLD:
return pct_dict[match]
else:
return '--'
我对您的原始代码进行了一些更改,并在注释中进行了说明。
我从未使用过 fuzzywuzzy,但基于 fuzzywuzzy 自述文件的 "Usage" 部分:https://github.com/seatgeek/fuzzywuzzy
谢谢,你帮了大忙。我还了解到 FuzzyWuzzy 使用的数据必须转换为文本才能正常工作。我能够将数据包含在 '' 中并使用 str() 转换传入的分数。我目前正在寻找一种在多维数组上使用 str() 命令的方法,以消除在数据中使用 '' 的需要。
这是我的工作代码:
@register.simple_tag
def get_perc(score):
MATCH_THRESHOLD = 80
pct_dict = {'14': '0.016', '14.7': '0.021', '15.3': '0.026', '16': '0.034', '16.7': '0.04', '17.3': '0.05', '18': '0.07', '18.7': '0.09', '19.3': '0.11', '20': '0.13', '20.7': '0.17', '21.3': '0.21', '22': '0.26', '22.7': '0.31', '23.3': '0.38', '24': '0.47'}
if not score:
return '--'
elif score < 24:
return '<1'
match, match_score = process.extractOne(str(score), pct_dict.keys())
if match_score >= MATCH_THRESHOLD:
return pct_dict[match]
else:
return '--'
我目前在 Excel 中使用以下模糊逻辑命令 select 来自参考 table 的值:
=IF(E49>0,VLOOKUP(E49,'Ref Table'!$D:$E,2,FALSE),"--")
我正在尝试编写一个 Django/Python 函数来给出最接近所提供数字的值。 (示例:分数 = 14.5 - 返回值为 0.021)
我安装了 fuzzywuzzy,但我不确定这是实现它的最佳方式。
下面是到目前为止没有模糊逻辑的函数示例。
@register.simple_tag
def get_perc(score):
if score is None:
return '--'
else:
pct_dict = {
14: 0.016,
14.7: 0.021,
15.3: 0.026,
16: 0.034,
16.7: 0.04,
17.3: 0.05,
18: 0.07,
18.7: 0.09,
19.3: 0.11,
20: 0.13,
20.7: 0.17,
21.3: 0.21,
22: 0.26,
22.7: 0.31,
23.3: 0.38,
24: 0.47,
24.7: 0.56,
25.3: 0.68,
26: 0.82,
26.7: 0.98,
27.3: 1.17,
28: 1.39,
29.3: 1.94,
30: 2.28
}
if score in pct_dict.keys():
return pct_dict[score]
else:
return '--'
(示例:分数 = 14.5 - 返回值为 0.021)
如果您只是想使用 fuzzywuzzy 将输入与您的其中一个键进行模糊匹配,您可以尝试这样的操作:
from fuzzywuzzy import process
def get_perc(score):
# I put your dictionary up here so that it's always defined.
pct_dict = {
14: 0.016,
14.7: 0.021,
15.3: 0.026,
16: 0.034,
16.7: 0.04,
17.3: 0.05,
18: 0.07,
18.7: 0.09,
19.3: 0.11,
20: 0.13,
20.7: 0.17,
21.3: 0.21,
22: 0.26,
22.7: 0.31,
23.3: 0.38,
24: 0.47,
24.7: 0.56,
25.3: 0.68,
26: 0.82,
26.7: 0.98,
27.3: 1.17,
28: 1.39,
29.3: 1.94,
30: 2.28
}
MATCH_THRESHOLD = 80 # This is the minimum score needed to "match" a value
# you can change it as you like.
if not score: # I changed this, so that any "falsey" value will return '--'
# this includes values like '', None, 0, and False
return '--'
match, match_score = process.extractOne(score, pct_dict.keys())
if match_score >= MATCH_THRESHOLD:
return pct_dict[match]
else:
return '--'
我对您的原始代码进行了一些更改,并在注释中进行了说明。
我从未使用过 fuzzywuzzy,但基于 fuzzywuzzy 自述文件的 "Usage" 部分:https://github.com/seatgeek/fuzzywuzzy
谢谢,你帮了大忙。我还了解到 FuzzyWuzzy 使用的数据必须转换为文本才能正常工作。我能够将数据包含在 '' 中并使用 str() 转换传入的分数。我目前正在寻找一种在多维数组上使用 str() 命令的方法,以消除在数据中使用 '' 的需要。
这是我的工作代码:
@register.simple_tag
def get_perc(score):
MATCH_THRESHOLD = 80
pct_dict = {'14': '0.016', '14.7': '0.021', '15.3': '0.026', '16': '0.034', '16.7': '0.04', '17.3': '0.05', '18': '0.07', '18.7': '0.09', '19.3': '0.11', '20': '0.13', '20.7': '0.17', '21.3': '0.21', '22': '0.26', '22.7': '0.31', '23.3': '0.38', '24': '0.47'}
if not score:
return '--'
elif score < 24:
return '<1'
match, match_score = process.extractOne(str(score), pct_dict.keys())
if match_score >= MATCH_THRESHOLD:
return pct_dict[match]
else:
return '--'