决策树用做4D毫米波分类的实现方法

        使用4D毫米波雷达的一些特征,可以对其做准确的分类。这里使用点数、距离、方位角、长、宽作为特征,长宽通过估计目标朝向后将点云投影到朝向方向上计算得到。

        训练使用python的sklearn,直接导入即可

from sklearn.tree import DecisionTreeClassifier

        这里使用了三个类别(小车、大车、自行车)8万多组数据,80%用做训练集,20%用做测试集。       

 from sklearn.model_selection import train_test_split

 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

        然后调用函数进行训练

decision_tree = DecisionTreeClassifier()        
decision_tree.fit(X_train, y_train)

        训练完成后在测试集上验证

y_train_pred = decision_tree.predict(X_train)
y_test_pred = decision_tree.predict(X_test)

         验证准确率

from sklearn.metrics import accuracy_score
print('decision tree train accurary socre:', accuracy_score(y_train, y_train_pred), 'test accurary socre:', accuracy_score(y_test, y_test_pred))
train_error_score = 1 - accuracy_score(y_train, y_train_pred)
test_error_score = 1 - accuracy_score(y_test, y_test_pred)
print('decision tree train error socre:', train_error_score, 'test error socre:', test_error_score)

        如果使用默认参数,则训练集准确率为100%,测试集准确率为95%

        获取决策树深度,发现有33层,叶子数有2179个,实在过于庞大

tree_depth = decision_tree.get_depth()
tree_leaves = decision_tree.get_n_leaves()
print('depth',tree_depth,',leaves:',tree_leaves)

        调用决策树可视化工具,可以看到结果,由于叶子树太多,全屏显示只能看到一个个小点

         放大到100%才能看清细节

        这么多的层数和叶子树存在过拟合风险,而且实现的代码也非常复杂,因此考虑降低层数

decision_tree = DecisionTreeClassifier(max_depth=9)  

        在生成决策树模型的时候设置最大深度为9,则最后生成的决策树体量大幅减小,实测结果层数为9,叶子树为230。

         训练集准确度有所降低,但测试集反而提升,表明降低了过拟合,提升了泛化性能。

         模型生成之后,代码编写费事费力,这里参考这位博主的方法,用代码自动生成。不过python脚本有点小问题,需要自行发现修改才能得到最后的结果。

使用Python训练好的决策树模型生成C++代码_python sklearn的决策树模型如何在c++使用_大风起兮d的博客-CSDN博客

        使用m2cgen库也可以实现模型到c代码的转换,  详细信息可以查看m2cgen · PyPI

import m2cgen as m2c
code = m2c.export_to_c(decision_tree)
file = open('radar_tree_m2cgen_code.txt','w')
file.write(code)
file.close()

        如果对效果不满意,可以使用不同的特征组合进行对比实验,以获得期望的结果。

猜你喜欢

转载自blog.csdn.net/weixin_41691854/article/details/129830285