x = datalong_hair forehead_width_cm forehead_height_cm nose_wide nose_long lips_thin distance_nose_to_lip_long y = datagender y = LabelEncoderfit_transformy X_trainX_testY_trainY_
可以通过设置决策树的参数来避免过拟合,例如设置最大深度(max_depth)、最小叶子节点数(min_samples_leaf)等。以下是一个示例代码:
from sklearn.tree import DecisionTreeClassifier
构建决策树模型,设置最大深度为5,最小叶子节点数为10
clf = DecisionTreeClassifier(max_depth=5, min_samples_leaf=10)
训练模型
clf.fit(X_train, Y_train)
在测试集上评估模型性能
score = clf.score(X_test, Y_test) print("Accuracy:", score)
原文地址: http://www.cveoy.top/t/topic/hnPG 著作权归作者所有。请勿转载和采集!