python机器学习-鸢尾花决策树

决策树API

  • class sklearn.tree.DecisionTreeClassifier(criterion=’gini’, max_depth=None,random_state=None)
    • 决策树分类器
    • criterion:默认是’gini’系数,也可以选择信息增益的熵’entropy’
    • max_depth:树的深度大小
    • random_state:随机数种子
  • 其中会有些超参数:max_depth:树的深度大小
#鸢尾花决策树
def decision_iris():
    """
    用决策树对鸢尾花进行分类
    :return:
    """
    #获取数据集
    iris = load_iris()
    #划分数据集
    x_train,x_test,y_train,y_test=train_test_split(iris.data,iris.target,random_state=22)
    #决策树预估器
    estimator=DecisionTreeClassifier(criterion="entropy")#criterion默认为gini系数,此处选择的为信息增益的熵
    #max_depth:树深的大小,random_state:随机数种子
    estimator.fit(x_train,y_train)
    #模型评估
    y_predict=estimator.predict(x_test)
    print("y_predict:\n",y_predict)
    print("直接对比真实值和预测值:\n",y_test==y_predict)
    score=estimator.score(x_test,y_test)
    print("准确率为:\n",score)
    #决策树可视化
    export_graphviz(estimator,out_file="iris_tree.dot")

 

posted @ 2021-10-18 21:25  风吹过半夏  阅读(544)  评论(0编辑  收藏  举报