def test_createTreeEx2(self): myDat1 = regTrees.loadDataSet('ex2.txt') myDat1 = np.mat(myDat1) regTree = regTrees.createTree(myDat1) print(regTree) myDat1 = regTrees.loadDataSet('ex2.txt') myDat1 = np.mat(myDat1) regTree = regTrees.createTree(myDat1, ops=(10000, 4)) # 手动调参比较敏感 print(regTree)
def test_bike(self): trainMat = np.mat(regTrees.loadDataSet('bikeTrain.txt')) testMat = np.mat(regTrees.loadDataSet('bikeTest.txt')) #print(testMat, trainMat) myTree = regTrees.createTree(trainMat, ops=(1, 20)) # 回归树 yHat = regTrees.createForeCast(myTree, testMat[:, 0]) r1 = np.corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1] print(r1) # 回归树的相关系数 print("-" * 80) myTree = regTrees.createTree(trainMat, regTrees.modelLeaf, regTrees.modelErr, (1, 20)) # 模型树 yHat = regTrees.createForeCast(myTree, testMat[:, 0], regTrees.modelTreeEval) r2 = np.corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1] print(r2) # 模型树的相关系数
def reDraw(tolS, tolN): reDraw.f.clf() # clear the figure reDraw.a = reDraw.f.add_subplot(111) if chkBtnVar.get(): if tolN < 2: tolN = 2 myTree=regTrees.createTree(reDraw.rawDat, regTrees.modelLeaf,\ regTrees.modelErr, (tolS,tolN)) yHat = regTrees.createForeCast(myTree, reDraw.testDat, \ regTrees.modelTreeEval) else: myTree = regTrees.createTree(reDraw.rawDat, ops=(tolS, tolN)) yHat = regTrees.createForeCast(myTree, reDraw.testDat) reDraw.a.scatter(reDraw.rawDat[:, 0].flatten().A[0], reDraw.rawDat[:, 1].flatten().A[0], s=5) #use scatter for data set reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0) #use plot for yHat reDraw.canvas.draw()
def test_createTreeEx3(self): myDat1 = regTrees.loadDataSet('exp2.txt') myDat1 = np.mat(myDat1) regTree = regTrees.createTree(myDat1, regTrees.modelLeaf, regTrees.modelErr, (1, 10)) print(regTree)
def test_createTree0(self): myDat1 = regTrees.loadDataSet('ex0.txt') myDat1 = np.mat(myDat1) regTree = regTrees.createTree(myDat1) print(regTree)