《机器学习实战》决策树的应用

 

 

课本中给出的是一个预测隐形眼镜的例子。

数据集样式如下:

young    myope    no    reduced    no lenses
young    myope    no    normal    soft
young    myope    yes    reduced    no lenses
young    myope    yes    normal    hard
young    hyper    no    reduced    no lenses
young    hyper    no    normal    soft
young    hyper    yes    reduced    no lenses
young    hyper    yes    normal    hard
pre    myope    no    reduced    no lenses

 

用sklearn实现的代码如下:


import numpy as np
import pandas as pd

#载入数据
labels=['age','prescript','astigmatic','tearRate','class']
feature=['age','prescript','astigmatic','tearRate']
lenses=pd.read_csv('C:/Users/lenovo/Desktop/lenses.txt',names=labels,sep='\t')
print(lenses)


#将数据转换为数值型
from sklearn import preprocessing

lenses_feature=lenses[feature]
le=preprocessing.LabelEncoder()
for col in lenses_feature.columns:
    lenses_feature[col]=le.fit_transform(lenses_feature[col])

print(lenses_feature)


from sklearn.model_selection import train_test_split

#分离出测试集和验证集
X=lenses_feature
y=lenses['class']
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3)

#训练决策树模型
from sklearn import tree

dtc=tree.DecisionTreeClassifier().fit(X_train,y_train)

y_pred=dtc.predict(X_test)
print(dtc.predict([[1,1,0,1]]))


#评估模型
from sklearn import metrics

value=list(set(y))
print(metrics.classification_report(y_test,y_pred))

print(metrics.confusion_matrix(y_test,y_pred,labels=value))

#画决策树
import graphviz

feature_name=['age','prescript','astigmatic','tearRate']

dot_data=tree.export_graphviz(dtc,out_file=None
                                ,feature_names= feature_name
                                ,class_names=["no lenses","soft","hard"]
                                ,filled=True
                                ,rounded=True
                               )
graph = graphviz.Source(dot_data)
graph

#探索决策树
print(dtc.feature_importances_)

print([*zip(feature,dtc.feature_importances_)])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值