首页 > 编程语言 > 详细

01背包 决策树模型 Python

时间:2014-01-21 01:17:15      阅读:389      评论:0      收藏:0      [点我收藏+]
def DecisionTree( weight_arr, value_arr, index, left_space ):
	if index == 0:
		if left_space >= weight_arr[index]:
			return value_arr[index]
		else:
			return 0
	without_index = DecisionTree( weight_arr, value_arr, index - 1, left_space )
	if weight_arr[index] > left_space:
		return without_index
	else:
		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 )

01背包 决策树模型 Python

原文:http://blog.csdn.net/pandora_madara/article/details/18319169

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!