从 Python 中的列表中遍历一棵树来进行计算?



我有这个列表:

new_tree = {'cues': 'glucose_tol',
'directions': '<=',
'thresholds': '122.5',
'exits': 1.0,
'children': [{'cues': True},
{'cues': 'mass_index',
'directions': '<=',
'thresholds': '30.8',
'exits': 1.0,
'children': [{'cues': 'pedigree',
'directions': '<=',
'thresholds': '0.305',
'exits': 1.0,
'children': [{'cues': True},
{'cues': 'diastolic_pb',
'directions': '<=',
'thresholds': '77.0',
'exits': 1,
'children': [{'cues': True}, 
{'cues': 'insulin',
'directions': '<=',
'thresholds': '480',
'exits': '0.5',
'children': [{'cues': True}, {'cues': False}]}]}]}]}]}

我想获取这些数据点在此树列表中的路径,以便我可以知道它们的去向,然后进行一些计算。

我在df中有数据点(2个数据点仅用于说明(:

print(df)
times_pregnant,glucose_tol,diastolic_pb,triceps,insulin,mass_index,pedigree,age,label
6,148,72,35,0,33.6,0.627,50,1
1,85,66,29,0,26.6,0.351,31,0

第一个将转到glucose_tol,mass_index,谱系,diastolic_pb,并将归类为True。如何从该数据点经过的列表中获取这 4 个线索并保存它们以供将来计算?任何帮助将不胜感激。

这看起来像一个决策树。

它的工作方式是,在每一步中,您要么处于最终决策状态("cues":真,或"cues":假(,要么需要做出决定。

要做出决定,您需要从数据帧中获取"提示"中命名的字段,然后使用方向和阈值形成条件。第一个基本上是if glucose_tol <= 122.5.每个节点应该有 2 个孩子,我认为第一个如果是真的情况,第二个是假的(如果你知道域,这对你来说应该是显而易见的(。然后,您根据自己的决定选择孩子并继续。

可能最简单的方法是实现递归函数。一旦你有了根据一行数据评估树的功能,你就可以添加功能来存储你想要的东西或你认为有趣的东西。

最新更新