决策树变量的面向对象实现
oo_trees的Python项目详细描述
这个存储库将包含决策树的几个变体/ 集成分类算法,以面向对象的方式编写。 我的直接目标是试图重现一些关于典型相关的this paper的结果。 森林,我正在测试它。
如果可能,外部参数名将与scikit-learn匹配 实现decision trees 和random forests。
用法
与scikit-learn的一个主要区别是数据集和 属性被视为一级对象。此外,所有 分类器必须使用其训练数据集初始化(相反 调用fit。
fromoo_trees.datasetimportDatasetfromoo_trees.decision_treeimportDecisionTreefromoo_trees.random_forestimportRandomForestX=examples# numpy 2D numeric arrayy=outcomes# numpy 1D arraydataset=Dataset(X,y)training_dataset,test_dataset=dataset.random_split(0.75)d_tree=DecisionTree(training_dataset)forest=RandomForest(training_dataset)print(d_tree.classify(test_dataset.X[0]))print(forest.classify(test_dataset.X[0]))d_tree_confusion_matrix=d_tree.performance_on(test_dataset)forest_confusion_matrix=forest.performance_on(test_dataset)print(d_tree_confusion_matrix.accuracy)print(forest_confusion_matrix.accuracy)
当初始化数据集时,我们假设训练的所有属性 例子是绝对的。如果不是这样,你可以把 初始化时附加的attribute_types变量:
fromoo_trees.datasetimportDatasetfromoo_trees.attributeimportNumericAttribute,CategoricalAttributeX=examplesy=outcomesattributes=[NumericAttribute(index=0,name='age'),CategoricalAttribute(index=1,name='sex'),NumericAttribute(index=2,name='income')]dataset=Dataset(X,y,attributes)
对于每种属性类型,找到最佳分割的逻辑是不同的, 在将来,可能会有额外的特定于类型的参数(例如 作为重要性或编号到名称的映射)对分类或 显示。