决策树

import matplotlib.pyplot as plt

decisionNode = dict(boxstyle = 'sawtooth',fc= '0.8')
leafNone = dict(boxstyle='round4',fc = '0.8')
arrow_args = dict(arrowstyle ='<-')
def plotNode(noedext,centerPt,parentPt,nodeType):
    plt.annotate(noedext,xy=parentPt,xycoords='axes fraction',xytext = centerPt,textcoords ='axes fraction',\
                        va='center',ha='center',bbox= nodeType,arrowprops = arrow_args)

def createPlot():
    plotNode("Plicy desicion",(0.5,0.1),(0.1,0.5),decisionNode)
    plotNode("leaf ",(0.8,0.1),(0.3,0.8),decisionNode)
    plt.show()
def gettreeDepth(tree):
    numDepth =0
    firstkey = list(tree.keys())[0]
    second = tree[firstkey]
    for key in list(second.keys()):
        if type(key).__name__=='dict':
            thisDepth = 1 + gettreeDepth(tree[key])
        else:
            thisDepth =1
        if numDepth<thisDepth:
             numDepth = thisDepth

    return  numDepth
def gettreeleafs(tree):
    print(tree)
    numDepth =0
    firstkey = list(tree.keys())[0]
    print('first key ',firstkey)
    second = tree[firstkey]
    print('first value ', tree[firstkey])
    print(second,second.keys())
    for key in list(second.keys()):
        if type(second[key]).__name__=='dict':
            print("True")
            print(second[key])
            numDepth +=  gettreeleafs(second[key])
        else:
            print("False")
            numDepth += 1
    return  numDepth
def gettreeleafs1(tree):
    num =0
    keylist = list(tree.keys())
    # print("key",keylist)
    for key in keylist:
        if type(tree[key]).__name__ =='dict':
            num +=gettreeleafs1(tree[key])
        else:
            # print(tree[key])
            num +=1
    return num
def gettreeDepth_1(tree):
    print(tree)
    num =0
    keylist = list(tree.keys())
    for key in keylist:
        if type(tree[key]).__name__ =='dict':
            num =1+gettreeDepth_1(tree[key])

        else:
            num =1
    return num
def plotMidText(cenPrt,parentPtr,txtString):
    xMind = (parentPtr[0]-cenPrt[0])/2 + cenPrt[0]
    yMind = (parentPtr[0] - cenPrt[0]) / 2 + cenPrt[0]

    plt.scatter(cenPrt,parentPtr)
    # plt.text(xMind, yMind, txtString)
    # plt.show()
dic = {3:{'a':3,
          'b':{2:'c',
               5:{3:'a',2:'b',5:{1:"c"},
                  4:{2:'d',
                     3:{2:'e'}}}}}}

#
print(gettreeleafs1(dic))
print(gettreeDepth_1(dic))
# print(type((dic.keys())))
# key = list(dic.keys())
# print(key[0])
# print("11")
# createPlot()
# plotMidText([0.5,0.6],[0.3,0.8],"text123")

# 2. plt.annotate标注文字
# (1)annotate语法说明 :annotate(s='str' ,xy=(x,y) ,xytext=(l1,l2) ,..)
#
#
# s 为注释文本内容
# xy 为被注释的坐标点
# xytext 为注释文字的坐标位置
# xycoords 参数如下:
#
# figure points          points from the lower left of the figure 点在图左下方
# figure pixels          pixels from the lower left of the figure 图左下角的像素
# figure fraction       fraction of figure from lower left 左下角数字部分
# axes points           points from lower left corner of axes 从左下角点的坐标
# axes pixels           pixels from lower left corner of axes 从左下角的像素坐标
# axes fraction        fraction of axes from lower left 左下角部分
# data                     use the coordinate system of the object being annotated(default) 使用的坐标系统被注释的对象(默认)
# polar(theta,r)       if not native ‘data’ coordinates t
# extcoords 设置注释文字偏移量
#
#          | 参数 | 坐标系 |
#          | 'figure points' | 距离图形左下角的点数量 |
#          | 'figure pixels' | 距离图形左下角的像素数量 |
#          | 'figure fraction' | 0,0 是图形左下角,1,1 是右上角 |
#          | 'axes points' | 距离轴域左下角的点数量 |
#          | 'axes pixels' | 距离轴域左下角的像素数量 |
#          | 'axes fraction' | 0,0 是轴域左下角,1,1 是右上角 |
#          | 'data' | 使用轴域数据坐标系 |
#
# arrowprops  #箭头参数,参数类型为字典dict
#
# width           the width of the arrow in points                              点箭头的宽度
# headwidth   the width of the base of the arrow head in points  在点的箭头底座的宽度
# headlength  the length of the arrow head in points                   点箭头的长度
# shrink          fraction of total length to ‘shrink’ from both ends  总长度为分数“缩水”从两端
# facecolor     箭头颜色
# bbox给标题增加外框 ,常用参数如下:
#
#   boxstyle方框外形
#   facecolor(简写fc)背景颜色
#   edgecolor(简写ec)边框线条颜色
#   edgewidth边框线条大小
#  bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k',lw=1 ,alpha=0.5)  #fc为facecolor,ec为edgecolor,lw为lineweight

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值