当前位置: 首页>>代码示例>>Python>>正文


Python Tree.keys方法代码示例

本文整理汇总了Python中nltk.tree.Tree.keys方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.keys方法的具体用法?Python Tree.keys怎么用?Python Tree.keys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在nltk.tree.Tree的用法示例。


在下文中一共展示了Tree.keys方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: pruning

# 需要导入模块: from nltk.tree import Tree [as 别名]
# 或者: from nltk.tree.Tree import keys [as 别名]
def pruning(Tree,x,y,t):
    if not isinstance(Tree, dict):
        error = 0
        right = 0
        for tag in y:
            if tag == Tree:
                right += 1
            else:
                error += 1
        return Tree,right,error
    
    nodefN = Tree.keys()[0]
    nodef = int(nodefN[7:])
    canpruning = True
    total1 = 0
    total2 = 0
    totalerror = 0
    
    newTree = {nodefN:{}}
    
    if (t[nodef] == 1): #discrete
        fV = [data[nodef] for data in x]
        fVType = set(fV)
        for Vtype in fVType:
            [subSetx,subSety,subSett] = splitSet_discrete(x,y,t,nodef,Vtype)
            try:
                [pTree, right, error ]= pruning(Tree[nodefN][Vtype],subSetx, subSety, subSett)
                newTree[nodefN][Vtype] = pTree
                if not isinstance(pTree,dict):
                    if pTree == name1:
                        total1 += right
                        total2 += error
                    else:
                        total1 += error
                        total2 += right
                    totalerror += error
                else:
                    canpruning = False
            except:
                totalerror += len(subSetx)
                if total1 < total2:
                    total1 += len(subSetx)
                else:
                    total2 += len(subSetx)    
    else: #numerical
        minv = 0.0
        maxv = 1.0
        delta = (maxv - minv) / NforNumerical + 0.0001
        for i in range(NforNumerical):
            [subSetx,subSety,subSett] = splitSet_numerical(x,y,t,nodef,minv+delta*i,minv+delta*(i+1))
            if (len(subSetx) > 0):
                try:
                    [pTree, right, error ]= pruning(Tree[nodefN][i],subSetx, subSety, subSett)
                    newTree[nodefN][i] = pTree
                    if not isinstance(pTree,dict):
                        if pTree == name1:
                            total1 += right
                            total2 += error
                        else:
                            total1 += error
                            total2 += right
                        totalerror += error
                    else:
                        canpruning = False
                except:
                    totalerror += len(subSetx)
                    if total1 < total2:
                        total1 += len(subSetx)
                    else:
                        total2 += len(subSetx) 

    if canpruning and totalerror > min(total1,total2):
        #print name1,name2,totalerror,total1,total2
        if total1 < total2:
            #print Tree,totalerror,total1,total2,name1
            return name2,total2,total1
        else:
            #print Tree,totalerror,total1,total2,name2
            return name1,total1,total2
        
    return newTree,len(x)-totalerror,totalerror
开发者ID:DC-Swind,项目名称:DataMining,代码行数:83,代码来源:DTwithPruning.py


注:本文中的nltk.tree.Tree.keys方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。