@@ -395,12 +395,12 @@ def createForeCast(tree, testData, modelEval=regTreeEval):
395
395
396
396
397
397
if __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
404
404
405
405
# # 回归树
406
406
# myDat = loadDataSet('input/9.RegTrees/data1.txt')
@@ -431,29 +431,30 @@ def createForeCast(tree, testData, modelEval=regTreeEval):
431
431
# myTree = createTree(myMat, modelLeaf, modelErr)
432
432
# print myTree
433
433
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
+ # # corrcoef 返回皮尔森乘积矩相关系数
445
+ # print "regTree:", corrcoef(yHat1, testMat[:, 1],rowvar=0)[0, 1]
446
+
447
+ # # 模型树
448
+ # myTree2 = createTree(trainMat, modelLeaf, modelErr, ops=(1, 20))
449
+ # yHat2 = createForeCast(myTree2, testMat[:, 0], modelTreeEval)
450
+ # print myTree2
451
+ # print "modelTree:", corrcoef(yHat2, testMat[:, 1],rowvar=0)[0, 1]
452
+
453
+ # # 线性回归
454
+ # ws, X, Y = linearSolve(trainMat)
455
+ # print ws
456
+ # m = len(testMat[:, 0])
457
+ # yHat3 = mat(zeros((m, 1)))
458
+ # for i in range(shape(testMat)[0]):
459
+ # yHat3[i] = testMat[i, 0]*ws[1, 0] + ws[0, 0]
460
+ # print "lr:", corrcoef(yHat3, testMat[:, 1],rowvar=0)[0, 1]
0 commit comments