01背包 决策树模型 Python

来源:互联网 发布:阿里云 机房地址 编辑:程序博客网 时间:2024/05/29 14:09
def DecisionTree( weight_arr, value_arr, index, left_space ):if index == 0:if left_space >= weight_arr[index]:return value_arr[index]else:return 0without_index = DecisionTree( weight_arr, value_arr, index - 1, left_space )if weight_arr[index] > left_space:return without_indexelse:with_index = values[index] + DecisionTree( weight_arr, value_arr, index - 1, left_space -weight_arr[index] )return max( with_index, without_index )weights = [ 1, 5, 3, 4 ]values = [ 15, 10, 9, 5 ]res = DecisionTree( weights, values, len( values ) - 1, 8 )

0 0
原创粉丝点击