# 导入必要的库 from sklearn.datasets import load_diabetes from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeRegressor, plot_tree from sklearn.metrics import mean_squared_error import matplotlib.pyplot as plt # 加载糖尿病数据集 diabetes_data = load_diabetes(...
totalW, plotTree.yOff) #中心位置 plotMidText(cntrPt, parentPt, nodeTxt) #标注有向边属性值 plotNode(firstStr, cntrPt, parentPt, decisionNode) #绘制结点 secondDict = myTree[firstStr] #下一个字典,也就是继续绘制子结点 plotTree.yOff = plotTree.yOff - 1.0/plotTree.totalD #y偏移 for ...
plt.figure(figsize=(20,10)) plot_tree(regressor, filled=True, feature_names=diabetes_data.feature_names) plt.draw() plt.show()
return majorityCnt(classList) bestFeat = chooseBestFeatureToSplit(dataSet) # 返回最佳的属性划分方式 bestFeatLabel = labels[bestFeat] # 并得到该属性的字符串名 # 由于{}设置为空,即键的值为空,所以下层字典树的创建在该{}里创建 myTree = {bestFeatLabel: {}} # 开始创建字典树,bestFeatLabel为根...
plotNode('test1',(0.5,0.1),(0.1,0.5),decisionNode) plotNode('test2',(0.8,0.1),(0.3,0.8),leafNode) plt.show() 测试过这个小例子之后我们就要开始构建注解树了。虽然有xy坐标,但在如何放置树节点的时候我们会遇到一些麻烦。所以我们需要知道有多少个叶节点,树的深度有多少层。下面的两个函数就是为了得...
plotMidText(cntrPt, parentPt,nodeTxt) plotNode(firstStr, cntrPt, parentPt, decisionNode) secondDict = myTree[firstStr] plotTree.yoff = plotTree.yoff -1.0/plotTree.totalD for key in secondDict.keys(): if type(secondDict[key]) == 'dict': # 判断子节点是否是字典类型 ...
dataTrain<-read.csv("xiguadata.csv",header=TRUE)trainDecisionTree<-function(dataTrain){calEntropy<-function(y){# 计算熵 values<-table(unlist(y));# 频次汇总 得到各个特征对应的概率 valuesRate<-values/sum(values);logVal=log2(valuesRate);#log2(0)==infinite ...
plt.plot(X[:,0][y==0],X[:,1][y==0],'ro',alpha=0.6)plt.plot(X[:,0][y==0],X[:,1][y==1],'bs',alpha=0.6)plt.axis(axes)plt.xlabel('x1')plt.xlabel('x2')plt.figure(figsize=(12,5))plt.subplot(121)plot_decision_boundary(tree_clf,X,y)plt.title('Decision Tree')plt...
Run code Powered By In the decision tree chart, each internal node has a decision rule that splits the data. Gini, referred to as Gini ratio, measures the impurity of the node. You can say a node is pure when all of its records belong to the same class, such nodes known as the ...
Run code Powered By In the decision tree chart, each internal node has a decision rule that splits the data. Gini, referred to as Gini ratio, measures the impurity of the node. You can say a node is pure when all of its records belong to the same class, such nodes known as the ...