Skip to content

Instantly share code, notes, and snippets.

@masanobuimai
Created January 16, 2020 12:22
Show Gist options
  • Save masanobuimai/3ae46af0b135da565d7723ee4d073766 to your computer and use it in GitHub Desktop.
Save masanobuimai/3ae46af0b135da565d7723ee4d073766 to your computer and use it in GitHub Desktop.
def my_round(source_list, threshold=0.1, ratio_threshold=0.8):
"""
リストの最大値を基準に、最大値との比率が閾値の範囲かどうかで値を1,0で二値化します。
:param source_list: 対象のリスト
:type source_list: List[float]
:param threshold: 最大値の閾値(これより小さい値の場合、すべて0になる)
:param ratio_threshold: 最大値との比率の閾値(この範囲内の値は1になる))
:return: 二値化したリスト
"""
max_value = max(source_list)
return [(1 if max_value >= threshold and x / max_value >= ratio_threshold else 0) for x in source_list]
if __name__ == '__main__':
a = [0.1, 0.2, 0.45, 0.42, 0.37, 0.3, 0.4, 0.5]
print(a)
print(my_round(a))
print(my_round([0.2, 0.28, 0.3]))
print(my_round([0.2, 0.28, 0.3], threshold=0.4, ratio_threshold=0.6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment