@@ -395,12 +395,12 @@ def createForeCast(tree, testData, modelEval=regTreeEval):
395395
396396
397397if __name__ == "__main__" :
398- # 测试数据集
399- testMat = mat (eye (4 ))
400- print testMat
401- print type (testMat )
402- mat0 , mat1 = binSplitDataSet (testMat , 1 , 0.5 )
403- print mat0 , '\n -----------\n ' , mat1
398+ # # 测试数据集
399+ # testMat = mat(eye(4))
400+ # print testMat
401+ # print type(testMat)
402+ # mat0, mat1 = binSplitDataSet(testMat, 1, 0.5)
403+ # print mat0, '\n-----------\n', mat1
404404
405405 # # 回归树
406406 # myDat = loadDataSet('input/9.RegTrees/data1.txt')
@@ -431,29 +431,29 @@ def createForeCast(tree, testData, modelEval=regTreeEval):
431431 # myTree = createTree(myMat, modelLeaf, modelErr)
432432 # print myTree
433433
434- # # # 回归树 VS 模型树 VS 线性回归
435- # trainMat = mat(loadDataSet('input/9.RegTrees/bikeSpeedVsIq_train.txt'))
436- # testMat = mat(loadDataSet('input/9.RegTrees/bikeSpeedVsIq_test.txt'))
437- # # # 回归树
438- # myTree1 = createTree(trainMat, ops=(1, 20))
439- # print myTree1
440- # yHat1 = createForeCast(myTree1, testMat[:, 0])
441- # print "--------------\n"
442- # # print yHat1
443- # # print "ssss==>", testMat[:, 1]
444- # print "回归树:", corrcoef(yHat1, testMat[:, 1],rowvar=0)[0, 1]
445-
446- # # 模型树
447- # myTree2 = createTree(trainMat, modelLeaf, modelErr, ops=(1, 20))
448- # yHat2 = createForeCast(myTree2, testMat[:, 0], modelTreeEval)
449- # print myTree2
450- # print "模型树:", corrcoef(yHat2, testMat[:, 1],rowvar=0)[0, 1]
451-
452- # # 线性回归
453- # ws, X, Y = linearSolve(trainMat)
454- # print ws
455- # m = len(testMat[:, 0])
456- # yHat3 = mat(zeros((m, 1)))
457- # for i in range(shape(testMat)[0]):
458- # yHat3[i] = testMat[i, 0]*ws[1, 0] + ws[0, 0]
459- # print "线性回归:", corrcoef(yHat3, testMat[:, 1],rowvar=0)[0, 1]
434+ # # 回归树 VS 模型树 VS 线性回归
435+ trainMat = mat (loadDataSet ('input/9.RegTrees/bikeSpeedVsIq_train.txt' ))
436+ testMat = mat (loadDataSet ('input/9.RegTrees/bikeSpeedVsIq_test.txt' ))
437+ # # 回归树
438+ myTree1 = createTree (trainMat , ops = (1 , 20 ))
439+ print myTree1
440+ yHat1 = createForeCast (myTree1 , testMat [:, 0 ])
441+ print "--------------\n "
442+ # print yHat1
443+ # print "ssss==>", testMat[:, 1]
444+ print "回归树:" , corrcoef (yHat1 , testMat [:, 1 ],rowvar = 0 )[0 , 1 ]
445+
446+ # 模型树
447+ myTree2 = createTree (trainMat , modelLeaf , modelErr , ops = (1 , 20 ))
448+ yHat2 = createForeCast (myTree2 , testMat [:, 0 ], modelTreeEval )
449+ print myTree2
450+ print "模型树:" , corrcoef (yHat2 , testMat [:, 1 ],rowvar = 0 )[0 , 1 ]
451+
452+ # 线性回归
453+ ws , X , Y = linearSolve (trainMat )
454+ print ws
455+ m = len (testMat [:, 0 ])
456+ yHat3 = mat (zeros ((m , 1 )))
457+ for i in range (shape (testMat )[0 ]):
458+ yHat3 [i ] = testMat [i , 0 ]* ws [1 , 0 ] + ws [0 , 0 ]
459+ print "线性回归:" , corrcoef (yHat3 , testMat [:, 1 ],rowvar = 0 )[0 , 1 ]
0 commit comments