def test_tree_base(fake_writer): data = [ { "children": [ {"name": "B"}, { "children": [ {"children": [{"name": "I"}], "name": "E"}, {"name": "F"}, ], "name": "C", }, { "children": [ {"children": [{"name": "J"}, {"name": "K"}], "name": "G"}, {"name": "H"}, ], "name": "D", }, ], "name": "A", } ] c = Tree().add("", data) c.render() _, content = fake_writer.call_args[0] eq_(c.theme, "white") eq_(c.renderer, "canvas")
def test_tree_base(): data = [ { "children": [ {"name": "B"}, { "children": [ {"children": [{"name": "I"}], "name": "E"}, {"name": "F"}, ], "name": "C", }, { "children": [ {"children": [{"name": "J"}, {"name": "K"}], "name": "G"}, {"name": "H"}, ], "name": "D", }, ], "name": "A", } ] c = Tree().add("", data) eq_(c.theme, "white") eq_(c.renderer, "canvas") c.render()
def test_tree_options(fake_writer): c = Tree().add( series_name="tree", data=TEST_DATA, orient="BT", initial_tree_depth=1, label_opts=opts.LabelOpts(), leaves_label_opts=opts.LabelOpts(), ) c.render() _, content = fake_writer.call_args[0] assert_in("orient", content) assert_in("initialTreeDepth", content) assert_in("label", content) assert_in("leaves", content)
def test_tree_base(): data = [{ "children": [ { "name": "B" }, { "children": [ { "children": [{ "name": "I" }], "name": "E" }, { "name": "F" }, ], "name": "C", }, { "children": [ { "children": [{ "name": "J" }, { "name": "K" }], "name": "G" }, { "name": "H" }, ], "name": "D", }, ], "name": "A", }] c = Tree().add("", data) assert c.theme == "white" assert c.renderer == "canvas" c.render("render.html")
def test_tree_base(fake_writer): c = Tree().add("", TEST_DATA) c.render() _, content = fake_writer.call_args[0] assert_equal(c.theme, "white") assert_equal(c.renderer, "canvas")
class MCT(list): #there are 12 weeks, 10 SKUs, and we set C to 2 def __init__(self, maxLayer=12, maxNum=10, ensemble=False, C=2): self.maxLayer = maxLayer self.maxNum = maxNum self.ensemble = ensemble self.root = StatusNode(0, status=[0, 0, 0, maxNum]) self.root.value = 0 self.nodeList = [] self.meanProfit = 0 self.stdProfit = 0 self.C = C ### def checkChild(self, checkedNode, checkedStatus): if len(checkedNode.childList) == 0: return False if type(checkedNode) == int: myNode = self.nodeList[checkedNode] else: myNode = checkedNode for childI in checkedNode.childList: if self.nodeList[childI].status == checkedStatus: return True return False #find the childnode whose status equals to the value we want def findChild(self, checkedNode, checkedStatus): if len(checkedNode.childList) == 0: return None if type(checkedNode) == int: myNode = self.nodeList[checkedNode] else: myNode = checkedNode for childI in checkedNode.childList: if self.nodeList[childI].status == checkedStatus: return self.nodeList[childI] return None #update the times we checked/passed/selected a node def updateChecked(self, myNode: DecisionNode): tmpTravelNode = myNode while tmpTravelNode is not None: tmpTravelNode.checked += 1 tmpTravelNode = tmpTravelNode.parent def training(self, myX, myY, maxIter=5): ''' myX:ValueB+ReturnB ''' decisionList = list(set(myY.tolist())) print("training ...") self.nodeList = [self.root] #start iteration for iter in range(maxIter): #-for every iteration print("第{}轮迭代开始".format(iter + 1)) newStatusList = [self.root] for layerI in range(self.maxLayer): print("第{}周开始".format(layerI + 1)) #--for every layer #--new data newData = myX[iter * self.maxLayer + layerI, :] #--next week #--decision nodes newDecisionList = [] if self.maxNum > 0: #--the SKU was sold out if len(newStatusList) == 0: print("the SKU was sold out in all the way") break #--whether to generalize the decision node for statusNodeItem in newStatusList: if len(statusNodeItem.childList) == 0: #-----if the status doesn't have decision child for decisionItem in decisionList: #------set the decision node tmpDecisionNode = DecisionNode( decisionItem, parent=statusNodeItem) #------update the checked on the path self.updateChecked(tmpDecisionNode) #------add the node to the node list self.nodeList.append(tmpDecisionNode) #------add the node to the child list statusNodeItem.childList.append( len(self.nodeList) - 1) #-----add the node to the newDecisionNodeList newDecisionList.append(tmpDecisionNode) else: for decisionI in statusNodeItem.childList: #---update checked of the node tmpTravelNode = statusNodeItem self.updateChecked(tmpTravelNode) newDecisionList.append( self.nodeList[decisionI]) #--set the newStatusList newStatusList = [] #--find the decision node with the largest ucb maxUCB = max([ newDecisionNodeItem.ucb for newDecisionNodeItem in newDecisionList ]) #--status nodes for decisionNodeItem in newDecisionList: if decisionNodeItem.ucb == maxUCB: self.updateChecked(decisionNodeItem) #---status in this situation if decisionNodeItem.parent.status[3] - np.sum( newData[:7] >= decisionNodeItem.decision ) >= 0: #----if there is enough deposit tmpStatus=[np.sum(newData[:7]>=decisionNodeItem.decision),\ np.sum(newData[:7]<decisionNodeItem.decision),\ np.sum(newData[7:]>0),\ decisionNodeItem.parent.status[3]-np.sum(newData[:7]>=decisionNodeItem.decision)] else: #----if there is not enough deposit tmpStatus=[decisionNodeItem.parent.status[3],\ 7-decisionNodeItem.parent.status[3],\ np.sum(newData[7:]>0),\ 0] if tmpStatus[0] + tmpStatus[1] != 7: print(newData, decisionNodeItem.decision) #---the decision doesn't have the status node if self.checkChild(decisionNodeItem, tmpStatus) == False: #----new status node tmpStatusNode=StatusNode(decisionNodeItem.decision,\ status=tmpStatus,\ parent=decisionNodeItem) #----add the status node into node list self.nodeList.append(tmpStatusNode) #----add the status node into the child list of the decision node decisionNodeItem.childList.append( len(self.nodeList) - 1) else: #-----if the status already exists, change tmpNode into it tmpStatusNode = self.findChild( decisionNodeItem, tmpStatus) #----update the parents' value self.updateChecked(tmpStatusNode) tmpTravelNode = tmpStatusNode tmpAggValue = 0 while tmpTravelNode.parent is not None: if type(tmpTravelNode) == StatusNode: tmpTravelNode.value = ( tmpTravelNode.value * tmpTravelNode.checked + decisionNodeItem.decision * tmpTravelNode.status[0] + tmpAggValue ) / (tmpTravelNode.checked + 1) tmpAggValue = tmpTravelNode.value else: tmpTravelNode.value = ( tmpTravelNode.value * tmpTravelNode.checked + np.max([ self.nodeList[childI].value * self.nodeList[childI].checked / tmpTravelNode.checked for childI in tmpTravelNode.childList ])) / (tmpTravelNode.checked + 1) # print(self.nodeList[tmpTravelNode.childList[0]].checked/tmpTravelNode.checked) tmpTravelNode = tmpTravelNode.parent #------add the node to newStatusList if tmpStatusNode not in newStatusList and tmpStatusNode.status[ 3] > 0: newStatusList.append(tmpStatusNode) #------update the ucb if find the tail node if tmpStatusNode.status[3] == 0: #--backpropagation for this iteration self.BPIter(tmpStatusNode) #only leave the status node with decision node with highest ucb maxUcb = 0 for statusNodeItem in newStatusList: if statusNodeItem.parent.ucb > maxUcb: maxUcb = statusNodeItem.parent.ucb newStatusI = 0 while newStatusI < len(newStatusList): if newStatusList[newStatusI].parent.ucb < maxUcb: newStatusList.pop(newStatusI) newStatusI -= 1 newStatusI += 1 print("第{}周结束".format(layerI + 1)) #calculate backwards the ucb of each node def BPIter(self, myNode): tmpNode = myNode while tmpNode.parent is not None: #-update the ucb while BP tmpNode.ucb = tmpNode.value + self.C * np.sqrt( np.log(tmpNode.parent.checked) / tmpNode.checked) #-update the usb of the cousins of the tmpNode for childItem in tmpNode.parent.childList: if tmpNode is not self.nodeList[childItem]: self.nodeList[childItem].ucb = self.nodeList[ childItem].value + self.C * np.sqrt( np.log(self.nodeList[childItem].parent.checked) / self.nodeList[childItem].checked) tmpNode = tmpNode.parent ### def integrateTree(self, myNode): myDict = {} if type(myNode) == DecisionNode: myDict[ "name"] = "D,ucb:{:.2f},price:{},value:{:.2f},checked:{}".format( myNode.ucb, myNode.decision, myNode.value, myNode.checked) else: if myNode.parent != None: myDict[ "name"] = "S,status:{},value:{:.2f},weight:{:.2f}".format( myNode.status, myNode.value, myNode.checked / myNode.parent.checked) else: myDict["name"] = "root" myDict["children"] = [] if len(myNode.childList) != 0: for childI in myNode.childList: myDict["children"].append( self.integrateTree(self.nodeList[childI])) return myDict def plotModel(self): self.tree = Tree() self.echartTree = [self.integrateTree(self.root)] self.tree.add("", self.echartTree) self.tree.render() def saveModel(self): with open("model/myModel.pkl", "wb+") as myModelFile: pkl.dump(self, myModelFile)
d2["name"] = name data2.append(d2) # 最外层树 dic2 = {} dic2["children"] = data2 dic2["name"] = "关键词" data.append(dic2) print(data) # 需要调整画布大小 tree = Tree(init_opts=opts.InitOpts(width='2000px', height='15000px')) # 两个树分支的距离增大 tree.add("", data, collapse_interval=100) tree.set_global_opts(title_opts=opts.TitleOpts(title="LDA主题模型")) tree.render(u'./tree.html') # 用如下程序也行 # C = Collector() # # # @C.funcs # def tree_base() -> Tree: # # c = ( # Tree() # .add("", data, collapse_interval=100) # # ._set_collapse_interval(10) # .set_global_opts(title_opts=opts.TitleOpts(title="LDA主题模型")) # ) # return c
def plotModel(myNode): myTree = Tree() echartTree = [integrateTree(myNode)] myTree.add("", echartTree) myTree.render()
class MCT(): def __init__(self, maxLayer=12, maxNum=10, ensemble=False, C=2, adaptC=False): self.maxLayer = maxLayer self.maxNum = maxNum self.ensemble = ensemble self.adaptC = adaptC self.root = StatusNode( 0, status=[0, 0, 0, 0, 0, 0, maxNum, maxNum, maxNum]) self.root.value = 0 self.nodeList = [] self.meanProfit = 0 self.stdProfit = 0 self.C = C self.maxR = 17000 def checkChild(self, checkedNode, checkedStatus): if len(checkedNode.childList) == 0: return False if type(checkedNode) == int: myNode = self.nodeList[checkedNode] else: myNode = checkedNode for childI in checkedNode.childList: if self.nodeList[childI].status == checkedStatus: return True return False def findChild(self, checkedNode, checkedStatus): if type(checkedStatus) == np.ndarray: checkedStatus = checkedStatus.tolist() if len(checkedNode.childList) == 0 or checkedNode is None: return None if type(checkedNode) == int: myNode = self.nodeList[checkedNode] else: myNode = checkedNode for childI in checkedNode.childList: if self.nodeList[childI].status[:3] == checkedStatus: return self.nodeList[childI] return None def updateChecked(self, myNode: DecisionNode): tmpTravelNode = myNode while tmpTravelNode is not None: tmpTravelNode.checked += 1 tmpTravelNode = tmpTravelNode.parent def BPIter(self, myNode, C=500): if self.adaptC == False: tmpNode = myNode while tmpNode.parent is not None: #-update the ucb while BP tmpNode.ucb = tmpNode.value + self.C * np.sqrt( np.log(tmpNode.parent.checked) / tmpNode.checked) #-update the usb of the cousins of the tmpNode for childItem in tmpNode.parent.childList: if tmpNode is not self.nodeList[childItem]: self.nodeList[childItem].ucb = self.nodeList[ childItem].value + self.C * np.sqrt( np.log(self.nodeList[childItem].parent.checked) / self.nodeList[childItem].checked) tmpNode = tmpNode.parent else: tmpNode = myNode while tmpNode.parent is not None: #-update the ucb while BP tmpNode.ucb = tmpNode.value + C * np.sqrt( np.log(tmpNode.parent.checked) / tmpNode.checked) #-update the usb of the cousins of the tmpNode for childItem in tmpNode.parent.childList: if tmpNode is not self.nodeList[childItem]: self.nodeList[childItem].ucb = self.nodeList[ childItem].value + C * np.sqrt( np.log(self.nodeList[childItem].parent.checked) / self.nodeList[childItem].checked) C = C * 10 tmpNode = tmpNode.parent def findReturn(self, myDecisionNode, statusI): tmpNode = myDecisionNode returnList = [] while tmpNode.parent != None: if type(tmpNode) == StatusNode: if tmpNode.status[statusI] != 0: returnList.append(tmpNode.status[statusI]) tmpNode = tmpNode.parent returnArr = np.array(returnList) return returnArr def training(self, myX, myY, startLayer=0, maxIter=5, incrementalNode=None): ''' myX:ValueB+ReturnB ''' decisionList = list(set(myY.tolist())) print("training ...") if incrementalNode == None: self.nodeList = [self.root] startNode = self.root else: startNode = incrementalNode newDecisionList = [] #start iteration for iter in tqdm.tqdm(range(maxIter)): iter = iter % 50 #-for every iteration # print("第{}轮迭代开始".format(iter+1)) if np.sum(startNode.status[-3:]) == 0: break newStatusList = [startNode] for layerI in range(startLayer, self.maxLayer): # print("第{}周开始".format(layerI+1)) #--for every layer #--new data newData = myX[iter * self.maxLayer + layerI, :] #--next week #--decision nodes newDecisionList = [] if self.maxNum > 0: #--the SKU was sold out if len(newStatusList) == 0: print("the SKU was sold out in all the path") break #--whether to generalize the decision node for statusNodeItem in newStatusList: if len(statusNodeItem.childList) == 0: #-----if the status doesn't have decision child for decisionItem in decisionList: #------set the decision node tmpDecisionNode = DecisionNode( decisionItem, parent=statusNodeItem) #------update the checked on the path self.updateChecked(tmpDecisionNode) #------add the node to the node list self.nodeList.append(tmpDecisionNode) #------add the node to the child list statusNodeItem.childList.append( len(self.nodeList) - 1) #-----add the node to the newDecisionNodeList newDecisionList.append(tmpDecisionNode) else: for decisionI in statusNodeItem.childList: #---update checked of the node tmpTravelNode = statusNodeItem self.updateChecked(self.nodeList[decisionI]) newDecisionList.append( self.nodeList[decisionI]) #--set the newStatusList newStatusList = [] #--find the decision node with the largest ucb maxUCB = max([ newDecisionNodeItem.ucb for newDecisionNodeItem in newDecisionList ]) #--status nodes for decisionNodeItem in newDecisionList: if decisionNodeItem.ucb == maxUCB: #---status in this situation #A,B,C,Ar,Br,Cr,Al,Bl,Cl if layerI == 11: #----update return status in the last week newDataApp = [ self.findReturn(decisionNodeItem, i) for i in range(3, 6) ] tmpStatus=[min(np.sum(np.append(newData[:7],newDataApp[0])>=decisionNodeItem.decision),decisionNodeItem.parent.status[6]),\ min(np.sum(np.append(newData[7:14],newDataApp[1])>=decisionNodeItem.decision),decisionNodeItem.parent.status[7]),\ min(np.sum(np.append(newData[14:21],newDataApp[2])>=decisionNodeItem.decision),decisionNodeItem.parent.status[8]),\ 0,\ 0,\ 0,\ max(decisionNodeItem.parent.status[6]-np.sum(newData[:7]>=decisionNodeItem.decision),0),\ max(decisionNodeItem.parent.status[7]-np.sum(newData[7:14]>=decisionNodeItem.decision),0),\ max(decisionNodeItem.parent.status[8]-np.sum(newData[14:21]>=decisionNodeItem.decision),0)] else: tmpStatus=[min(np.sum(newData[:7]>=decisionNodeItem.decision),decisionNodeItem.parent.status[6]),\ min(np.sum(newData[7:14]>=decisionNodeItem.decision),decisionNodeItem.parent.status[7]),\ min(np.sum(newData[14:21]>=decisionNodeItem.decision),decisionNodeItem.parent.status[8]),\ int(np.sum(np.sum((newData[21:28]>0)*(newData[:7]>=decisionNodeItem.decision))/7*newData[21:28])),\ int(np.sum(np.sum((newData[28:35]>0)*(newData[7:14]>=decisionNodeItem.decision))/7*newData[28:35])),\ int(np.sum(np.sum((newData[35:42]>0)*np.sum(newData[14:21]>=decisionNodeItem.decision))/7*newData[35:42])),\ max(decisionNodeItem.parent.status[6]-np.sum(newData[:7]>=decisionNodeItem.decision),0),\ max(decisionNodeItem.parent.status[7]-np.sum(newData[7:14]>=decisionNodeItem.decision),0),\ max(decisionNodeItem.parent.status[8]-np.sum(newData[14:21]>=decisionNodeItem.decision),0)] #---the decision doesn't have the status node tmpStatusNode = self.findChild( decisionNodeItem, tmpStatus) if tmpStatusNode == None: #----new status node tmpStatusNode=StatusNode(decisionNodeItem.decision,\ status=tmpStatus,\ parent=decisionNodeItem) #----add the status node into node list self.nodeList.append(tmpStatusNode) #----add the status node into the child list of the decision node decisionNodeItem.childList.append( len(self.nodeList) - 1) #----update the parents' value self.updateChecked(tmpStatusNode) tmpTravelNode = tmpStatusNode tmpAggValue = 0 while tmpTravelNode.parent is not None: if type(tmpTravelNode) == StatusNode: #-----status node tmpTravelNode.value = ( tmpTravelNode.value * tmpTravelNode.checked + tmpTravelNode.parent.decision * tmpTravelNode.status[0] + tmpTravelNode.parent.decision * tmpTravelNode.status[1] + tmpTravelNode.parent.decision * tmpTravelNode.status[2] + tmpAggValue ) / (tmpTravelNode.checked + 1) + np.sum([ self.nodeList[childI].value * self.nodeList[childI].checked / tmpTravelNode.checked for childI in tmpTravelNode.childList ]) else: #-----decision node tmpMean = np.sum([ self.nodeList[childI].value * self.nodeList[childI].checked / tmpTravelNode.checked for childI in tmpTravelNode.childList ]) tmpStd = np.sqrt( np.sum([(self.nodeList[childI].value - tmpMean)**2 * self.nodeList[childI].checked / tmpTravelNode.checked for childI in tmpTravelNode.childList])) tmpTravelNode.value = ( tmpTravelNode.value * tmpTravelNode.checked + tmpMean - tmpStd) / (tmpTravelNode.checked + 1) # print(self.nodeList[tmpTravelNode.childList[0]].checked/tmpTravelNode.checked) tmpTravelNode = tmpTravelNode.parent #------add the node to newStatusList if tmpStatusNode not in newStatusList\ and (tmpStatusNode.status[6]>0\ or tmpStatusNode.status[7]>0\ or tmpStatusNode.status[8]>0): newStatusList.append(tmpStatusNode) elif tmpStatusNode.status[6]==0\ and tmpStatusNode.status[7]==0\ and tmpStatusNode.status[8]==0: #------update the ucb if find the tail node self.BPIter(tmpStatusNode, C=self.C / (10**(layerI + 1))) #only keep the status node with decision node with highest ucb maxUcb = 0 for statusNodeItem in newStatusList: if statusNodeItem.parent.ucb > maxUcb: maxUcb = statusNodeItem.parent.ucb newStatusI = 0 while newStatusI < len(newStatusList): if newStatusList[newStatusI].parent.ucb < maxUcb: newStatusList.pop(newStatusI) newStatusI -= 1 newStatusI += 1 # print("第{}周结束".format(layerI+1)) # #------update the ucb if find the tail node # for tmpStatusNode in newStatusList: # if (tmpStatusNode.status[6]==0\ # and tmpStatusNode.status[7]==0\ # and tmpStatusNode.status[8]==0): # #--backpropagation for this iteration # self.BPIter(tmpStatusNode) #update the C def integrateTree(self, myNode): myDict = {} if type(myNode) == DecisionNode: myDict[ "name"] = "D,ucb:{:.2f},price:{},value:{:.2f},checked:{}".format( myNode.ucb, myNode.decision, myNode.value, myNode.checked) else: if myNode.parent != None: myDict[ "name"] = "S,status:{},value:{:.2f},weight:{:.2f}".format( myNode.status, myNode.value, myNode.checked / myNode.parent.checked) else: myDict["name"] = "root" myDict["children"] = [] if len(myNode.childList) != 0: for childI in myNode.childList: myDict["children"].append( self.integrateTree(self.nodeList[childI])) return myDict def plotModel(self): self.tree = Tree() self.echartTree = [self.integrateTree(self.root)] self.tree.add("", self.echartTree) self.tree.render() def saveModel(self): with open( "model/myModel" + time.strftime("%Y%m%d", time.localtime(time.time())) + ".pkl", "wb+") as myModelFile: pkl.dump(self, myModelFile) def predictItem(self, statusListStr, clayer, myNode=None, preIter=20): if len(statusListStr) == 0: #-no status, return the decision node with largest value in the first layer myNode = self.nodeList[0] valueList = [ self.nodeList[childI].value for childI in self.nodeList[0].childList ] maxValue = max(valueList) for childI in self.nodeList[0].childList: if self.nodeList[childI].value == maxValue: return self.nodeList[childI], self.nodeList[ childI].decision else: #-transform the statusListStr into a list if type(statusListStr) == str: statusList = [ int(statusItem) for statusItem in statusListStr.split(",") ] else: statusList = statusListStr #-find the status node myStatus = np.array(statusList)[:3] statusNode = self.findChild(myNode, myStatus) if statusNode is not None: #--the status is found if len(statusNode.childList) == 0: #---the status is tail print("卑微预测(真的还要卖吗?你不为祖国母亲庆生的吗?):", statusNode.parent.decision) return statusNode.parent, statusNode.parent.decision #--find the status node, give the new decision decisionList = [ self.nodeList[childI].value for childI in statusNode.childList ] maxValue = max(decisionList) for childI in statusNode.childList: if self.nodeList[childI].value == maxValue: decision = self.nodeList[childI].decision print("预测:", decision) return self.nodeList[childI], decision else: #--the status is not found trainDf = pd.read_csv("data/Simulated_Data1.csv") keyList1 = [ keyItem for keyItem in trainDf.keys() if keyItem.startswith("ValueB") ] keyList2 = [ keyItem for keyItem in trainDf.keys() if keyItem.startswith("ReturnB") ] keyList = keyList1 + keyList2 #--find the nearest status minDis = np.inf for xi1 in range(50): if np.sum( abs( np.array(trainDf.loc[:, keyList])[xi1 * 12 + clayer, :3] - myStatus)) < minDis: minDis = np.sum( abs( np.array(trainDf.loc[:, keyList])[xi1 * 12 + clayer, :3] - myStatus)) for xi1 in range(50): if np.sum( abs( np.array(trainDf.loc[:, keyList])[xi1 * 12 + clayer, :3] - myStatus)) == minDis: X1 = np.array( trainDf.loc[:, keyList])[xi1 * 12:xi1 * 12 + 12] y1 = np.array(trainDf["price"])[xi1 * 12:xi1 * 12 + 12] break #--a random status for i in range(preIter): xi2 = np.random.randint(0, high=50) if i == 0: #--concatenate the 2 kinds of statuses X=np.concatenate((X1,\ np.array(trainDf.loc[:,keyList])[xi2*12:xi2*12+12]),\ axis=0) y=np.concatenate((y1,\ np.array(trainDf["price"])[xi2*12:xi2*12+12]),\ axis=0) else: #--concatenate the 2 kinds of statuses X=np.concatenate((X,\ np.array(trainDf.loc[:,keyList])[xi2*12:xi2*12+12]),\ axis=0) y=np.concatenate((y,\ np.array(trainDf["price"])[xi2*12:xi2*12+12]),\ axis=0) #--new status node restA = myNode.parent.status[6] - myStatus[0] restB = myNode.parent.status[7] - myStatus[1] restC = myNode.parent.status[8] - myStatus[2] tmpStatus = [ myStatus[0], myStatus[1], myStatus[2], 0, 0, 0, restA, restB, restC ] statusNode=StatusNode(myNode.decision,\ status=tmpStatus,\ parent=myNode) self.nodeList.append(statusNode) myNode.childList.append(len(self.nodeList) - 1) statusIndex = len(self.nodeList) - 1 #--expand the tree self.training(X, y, startLayer=clayer + 1, incrementalNode=statusNode, maxIter=preIter) statusNode = self.nodeList[statusIndex] if len(statusNode.childList) == 0: print("卑微预测(真的还要卖吗?你不为祖国母亲庆生的吗?):", statusNode.parent.decision) return statusNode.parent, statusNode.parent.decision #----status > 0 decisionList = [ self.nodeList[childI].value for childI in statusNode.childList ] if len(decisionList) == 0: tmpDecisionNode = DecisionNode(99, parent=statusNode) tmpDecisionNode.checked = 1 self.nodeList.append(tmpDecisionNode) statusNode.childList.append(len(self.nodeList) - 1) print("卑微预测: 99") return tmpDecisionNode, 99 else: maxValue = max(decisionList) for childI in statusNode.childList: #---find reasonal virtual decision node tmpDecisionNode = self.nodeList[childI] if np.sum(statusNode.status[:3]) > 0: if tmpDecisionNode.value == maxValue: decision = tmpDecisionNode.decision print("卑微预测:", decision) return tmpDecisionNode, decision else: #----status==[0,0,0] try: maxValue = max([ self.nodeList[childI].value for childI in statusNode.childList ]) if tmpDecisionNode.value == maxValue: decision = tmpDecisionNode.decision print("卑微预测:", decision) return tmpDecisionNode, decision except ValueError: tmpDecisionNode = DecisionNode(99, parent=statusNode) tmpDecisionNode.checked = 1 self.nodeList.append(tmpDecisionNode) statusNode.childList.append(len(self.nodeList) - 1) print("卑微预测:99") return tmpDecisionNode, 99
import json from pyecharts.charts import Tree import pyecharts.options as opts with open('GDP_data.json', 'r', encoding='utf-8') as f: j = json.load(f) data = [j] tree = Tree(init_opts=opts.InitOpts(width='1500px', height='1200px')) tree.add('', data) tree.render('tree.html')