Algorithm 如何编写这个简单的阈值算法?
问题在于:Algorithm 如何编写这个简单的阈值算法?,algorithm,Algorithm,问题在于: spread = ThresholdMax - ThresholdMin; diff = score - ThresholdMin; return Math.round(diff / spread); 当score介于两个阈值之间时,diff/spread将是一个介于0和1之间的数字,这意味着对其进行四舍五入将始终只能得到0或1,而不是您想要的分数 如果在最小值和最大值之间(如您的问题所示),只需使用: spread = maxThreshold - minThreshold; d
spread = ThresholdMax - ThresholdMin;
diff = score - ThresholdMin;
return Math.round(diff / spread);
当score
介于两个阈值之间时,diff/spread
将是一个介于0和1之间的数字,这意味着对其进行四舍五入将始终只能得到0或1,而不是您想要的分数
如果在最小值和最大值之间(如您的问题所示),只需使用:
spread = maxThreshold - minThreshold;
diff = score - minThreshold;
return Math.round(diff / spread);
这方面的样本分数为:
fraction = (score - thresholdMin) / (thresholdMax- thresholdMin);
最简单的else语句应该如下所示:
score fraction
----- --------
31 0.025
35 0.125
40 0.250
45 0.375
50 0.500
69 0.975
if(score>thresholdMax)返回1;
如果(分数
您也可以尝试使用min
和max
功能:
if (score > thresholdMax) return 1;
if (score < thresholdMin) return 0;
else{
return (score-thresholdMin)/(thresholdMax-thresholdMin);
}
你能举个例子吗。例如,分数=40、50、60:你想要什么?你说的“我认为它不起作用”是什么意思?实现并测试它。看起来不错。@Smith42先生,也许我弄错了。Francis,你可以通过缩进四个空格而不使用反勾号来获得代码块。已修改以显示如何。
if (score > thresholdMax) return 1;
if (score < thresholdMin) return 0;
else{
return (score-thresholdMin)/(thresholdMax-thresholdMin);
}
return min(1, max(0, (score-thresholdMin)/(thresholdMax-thresholdMin)));