def reDraw(tolN, tolS):
    reDraw.f.clf()  #清空图像
    reDraw.a = reDraw.f.add_subplot(111)

    trainData = regTrees.loadDataSet(
        ".//machinelearninginaction//ch09//sine.txt")
    if chkBtnVar.get():  #复选框被选中,求模型树
        modelTree = regTrees.createTree(trainData,
                                        leafType=regTrees.modelLeaf,
                                        errType=regTrees.modelErr,
                                        ops=(tolS, tolN))
        y_hat = regTrees.createForeCast(modelTree,
                                        trainData[:, 0],
                                        modelEval=regTrees.modelTreeEval)
    else:  #回归树
        modelTree = regTrees.createTree(trainData, ops=(tolS, tolN))
        y_hat = regTrees.createForeCast(modelTree, trainData[:, 0])
    data_hat = np.hstack((trainData[:, 0], y_hat))
    sort_hat = sorted(data_hat.tolist(), key=lambda x: x[0])
    sort_x = [x[0] for x in sort_hat]
    sort_y = [x[1] for x in sort_hat]
    reDraw.a.scatter(trainData[:, 0].T.tolist()[0],
                     trainData[:, 1].T.tolist()[0],
                     s=50)
    reDraw.a.plot(sort_x, sort_y)
    reDraw.canvas.show()
示例#2
0
def reDraw(tolS,tolN):
    # 调用 Figure 对象的 clf() 方法清理画布
    reDraw.f.clf()
    
    # 添加 Axes 对象
    reDraw.a = reDraw.f.add_subplot(111)
    
    # 检查复选框是否被选中
    if chkBtnVar.get():
        # 绘制模型树
        # tolN至少为2
        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)
    
    # 调用 scatter() 方法绘制真实数据图
    reDraw.a.scatter(reDraw.rawDat[:,0], reDraw.rawDat[:,1], s=5)
    
    # 预测值采用 plot() 方法绘制
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    
    # 在画布上显示绘制的图形
    reDraw.canvas.show()
示例#3
0
def reDraw(tolS, tolN):
    '''
    画出数据点和模型拟合线
    :param tolS: 用户输入的最小误差值
    :param tolN: 用户输入的最少样本数
    :return:
    '''
    # pass # 空语句, 是为了保持程序结构的完整性
    reDraw.f.clf()  # 清空之前的图像
    reDraw.a = reDraw.f.add_subplot(111)  # 添加一个新图
    if chkBtnVar.get():  # 复选框被选中, 构建模型树
        if tolN < 2:  # 最少样本数不能少于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)  # 画出数据点
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  # 画出模型拟合线
    reDraw.canvas.show()
示例#4
0
def reDraw(tolS, tolN):
    """
    绘制原始数据的散点图以及拟合数据的曲线图

    Parameters
    -----------
    tolS : 允许的误差下降值
    tolN : 诶分的最小样本值

    Returns
    ------------
    None
    """
    reDraw.f.clf()
    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].tolist(), reDraw.rawDat[:, 1].tolist(),
                     s=5)
    reDraw.a.plot(reDraw.testDat, yHat, 'b', linewidth=2.0)

    reDraw.canvas.show()
示例#5
0
def reDraw(tolS, tolN):
	reDraw.f.clf()
	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], reDraw.rawDat[:,1], s=5)
	reDraw.a.plot(reDraw.testDat, yHat, linewidth = 2.0)
	reDraw.canvas.show()
示例#6
0
def reDraw(tolS, tolN):
    reDraw.f.clf()  #清空之前的图像
    reDraw.a = reDraw.f.add_subplot(111)#重新添加新图
    if chkBtnVar.get():#检查选框model tree是否被选中
        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], reDraw.rawDat[:, 1], s=5)  # 绘制真实值
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  # 绘制预测值
    reDraw.canvas.show()
示例#7
0
def reDraw(tolS,tolN):
    reDraw.f.clf()        # clear the figure
    reDraw.a = reDraw.f.add_subplot(111)
    if chkBtnVar.get():   #see if check box has been selected
        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], reDraw.rawDat[:,1], s=5) #use scatter for data set
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0) #use plot for yHat
    reDraw.canvas.show()
示例#8
0
def reDraw(tolS, tolN):
    reDraw.f.clf()
    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(array(reDraw.rawDat[:, 0]), array(reDraw.rawDat[:, 1]), s=5)
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    reDraw.canvas.draw()
示例#9
0
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], reDraw.rawDat[:, 1], s=5)  # use scatter for data set
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  # use plot for yHat
    reDraw.canvas.show()
示例#10
0
def reDraw(tolS, tolN):
    reDraw.f.clf()
    reDraw.a = reDraw.f.add_subplot(111)
    if chkBtnVar.get():     # 复选框选中,则为模型树,否则会回归树
        if tolN < 2:
            tolN = 2
        myTree = regTrees.createTree(reDraw.rawData, regTrees.modelLeaf, regTrees.modelError, (tolS, tolN))
        yHat = regTrees.createForeCast(myTree, reDraw.testData, regTrees.modelTreeEval)
    else:
        myTree = regTrees.createTree(reDraw.rawData, ops = (tolS, tolN))
        yHat = regTrees.createForeCast(myTree, reDraw.testData)
    reDraw.a.scatter(reDraw.rawData[:, 0], reDraw.rawData[:, 1], s=5)
    reDraw.a.plot(reDraw.testData, yHat, linewidth=2.0)
    reDraw.canvas.show()
示例#11
0
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], reDraw.rawDat[:,1], s=20, alpha=.7) #use scatter for data set
    reDraw.a.plot(reDraw.testDat, yHat, 'or', markersize=5.0, linewidth=2.0, alpha=.6) #use plot for yHat
    reDraw.canvas.show()
示例#12
0
def reDraw(tolS, tolN):
    #清空之前的图像
    reDraw.f.clf()
    reDraw.a = reDraw.f.add_subplot(111)
    if chkBtnVar.get():
        if tolN < 2: tolN = 2
        myTree = rt.createTree(reDraw.rawDat, rt.modelleaf, rt.modelErr,
                               (tolS, tolN))
        yHat = rt.createForeCast(myTree, reDraw.testDat, rt.modelTreeEval)
    else:
        myTree = rt.createTree(reDraw.rawDat, ops=(tolS, tolN))
        yHat = rt.createForeCast(myTree, reDraw.testDat)
    reDraw.a.scatter(reDraw.rawDat[:, 0], reDraw.rawDat[:, 1], s=5)
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    reDraw.canvas.show()
示例#13
0
def reDraw(tolS, tolN):
    reDraw.f.clf()  # 清空之前的图像
    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(array(reDraw.rawDat[:, 0]), array(reDraw.rawDat[:, 1]), s=5)  # 画真实值的散点图
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  # 画预测值的直线图
    reDraw.canvas.draw()
示例#14
0
def reDraw(tolS,tolN):
    reDraw.rawDat=mat(regTrees.loadDataSet("C:\Users\YAN\Desktop\Cart\sine.txt"))
    reDraw.testDat=arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)
    reDraw.f.clf()
    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],reDraw.rawDat[:,1],s=5)
    reDraw.a.plot(reDraw.testDat,yHat,linewidth=2.0)
    reDraw.canvas.show()
def reDraw(tolS,tolN):
	#清空之前的图像,使得前后两个图像不会重叠
	reDraw.f.clf()
	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(list(reDraw.rawDat[:,0]),list(reDraw.rawDat[:,1]),s=5)
	reDraw.a.plot(reDraw.testDat,yHat,linewidth=2.0)
	reDraw.canvas.show()
示例#16
0
def reDraw(tolS, tolN):  #后边会编写函数
    reDraw.f.clf()
    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], reDraw.rawDat[:, 1], s=5)  #绘制样本集的散点图
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  #绘制测试集的拟合曲线
    reDraw.canvas.show()
def reDraw(tolS, tolN):
    reDraw.f.clf()
    reDraw.a = reDraw.f.add_subplot(111)
    # Check which is selected
    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].A, reDraw.rawDat[:, 1].A, s = 5)
    reDraw.a.plot(reDraw.testDat, yHat, linewidth = 2.0)
    reDraw.canvas.show()
def reDraw(tolS, tolN):
    reDraw.f.clf()
    reDraw.a = reDraw.f.add_subplot(111)
    # See if check box has been selected
    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], reDraw.rawDat[:, 1], c="black", s=5)
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    reDraw.canvas.draw()
示例#19
0
def reDraw(tolS, tolN):
    reDraw.f.clf()  # 清空之前的图像
    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.rawDat[:, 0].A
    reDraw.a.scatter(reDraw.rawDat[:, 0], reDraw.rawDat[:, 1],
                     s=5)  #真实值用scatter()方法绘制,因为scatter构建的是离散型散点图
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  #预测值用plot()方法构建,连续曲线
    reDraw.canvas.show()
def reDraw(tolS, tolN):
    reDraw.f.clf()  # clear画布
    reDraw.a = reDraw.f.add_subplot(111)
    if chkBtnVar.get():  # 判断 模型树 or 回归树
        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].A, reDraw.rawDat[:, 1].A,
                     s=5)  # 使用散点图,描绘真实数据
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  # 使用折线图,描绘预测数据
    reDraw.canvas.show()
示例#21
0
def reDraw(tolS, tolN):
    reDraw.f.clf()
    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].A,
                     reDraw.rawDat[:, 1].A,
                     s=20,
                     c='g',
                     alpha=.4)
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0, c='r')
    reDraw.canvas.show()
示例#22
0
def reDraw(tolS, tolN):
    # 清空
    reDraw.f = Figure(figsize=(5,4), dpi=100)
    reDraw.f.clf()
    # 建新图
    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], reDraw.rawDat[:, 1], s=5)
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
    reDraw.canvas.show()
def reDraw(tolS, tolN):
    reDraw.f.clf()
    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)
    #print shape(reDraw.rawDat[:,0])
    reDraw.a.scatter(reDraw.rawDat[:, 0].flatten().A[0],
                     reDraw.rawDat[:, 1].flatten().A[0],
                     s=5)
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    reDraw.canvas.show()
def reDraw(tols, tolN):
    # 清空图像,重新绘图
    reDraw.f.clf()
    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].A, reDraw.rawDat[:, 1].A, s=5)
    # 会出预测值
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    reDraw.canvas.show()
示例#25
0
def reDraw(tolS,tolN):
	reDraw.f.clf()
	reDraw.a = reDraw.f.add_subplit(111)
	if chkBtnVar.get():
		if tolN < 2:
			tolN = 2
		myTree = regTrees.createTree(reDraw.reDat,regTrees.modelLeaf,regTrees.modelErr(tolS,tolN))
		yHat = regTrees.createForeCast(myTree,reDraw.testDat,regTrees.modelTreeEval)
	else:
		myTree = regTrees.createTree(reDraw.rawDat[:,0],reDraw.rawDat[:,1],s=5)
		reDraw.canvas.show()
示例#26
0
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))
        print 'myTree=', myTree
        yHat = regTrees.createForeCast(myTree, reDraw.testDat, \
                                       regTrees.modelTreeEval)
    else:
        myTree=regTrees.createTree(reDraw.rawDat, ops=(tolS,tolN))
        yHat = regTrees.createForeCast(myTree, reDraw.testDat)
    # print 'min=', min(reDraw.rawDat[:,0])
    # print 'min=', min(reDraw.rawDat[:,1])
    print 'srcdata=',reDraw.rawDat
    # reDraw.a.scatter(reDraw.rawDat[:,0], reDraw.rawDat[:,1], s=5) #use scatter for data set
    reDraw.a.plot(reDraw.rawDat[:,0], reDraw.rawDat[:,1], '.') #use plot for yHat
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0) #use plot for yHat
    reDraw.canvas.show()
示例#27
0
def reDraw(tolS, tolN):
    # clear the figure
    reDraw.f.clf()
    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)

    # use scatter for data set
    reDraw.a.scatter(reDraw.rawDat[:, 0].A, reDraw.rawDat[:, 1].A, s=5)
    # use plot for yHat
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0, c='red')
    reDraw.canvas.show()
示例#28
0
def reDraw(tolS, tolN):  #图形是回归树还是模型树在程序内部判断
    reDraw.f.clf()  # 清空图像
    reDraw.a = reDraw.f.add_subplot(111)
    if chkBtnVar.get():  #检查复选框(Model Tree)是否选中,选中就是模型树(叶子节点是线性模型)
        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)
    print "\n××××××××××××reDraw.rawDat的值是:\n", type(
        numpy.mat(reDraw.rawDat[:, 0]))
    reDraw.a.scatter(list(reDraw.rawDat[:, 0]),
                     list(reDraw.rawDat[:, 1]),
                     c='r')  # 绘制原始数据的散点图(真实值)
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  # 使用预测数据yHat绘制折线图(预测值)
    # (如果叶子节点是常数绘制出的图形就是方正的,如果叶子节点的模型是线性的绘制出的图形拟合程度就比较好)
    reDraw.canvas.show()  #绘制图像
示例#29
0
def reDraw(tolS, tolN):  # tolS是容许的误差下降值,tolN是切分的最少样本数
    # tolN=200,则不切分树,用一条直线来拟合;tolN=50,5直线拟合;10,仅需要8直线便可拟合,
    # 为构建尽量大的树,将tolN设置为1,tolN设为0.此时构建模型树,过拟合严重
    reDraw.f.clf()  # 清空之前的图像
    reDraw.a = reDraw.f.add_subplot(111)
    if chkBtnVar.get():  # 检查复选框是否选中,选中则构建模型树
        if tolN < 2: 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))  # 中间两个参数采用默认参数regLeaf
        yHat = regTrees.createForeCast(myTree, reDraw.testDat)
    reDraw.a.scatter(reDraw.rawDat[:, 0].tolist(),
                     reDraw.rawDat[:, 1].tolist(),
                     s=5)
    #   注意加上 .tolist() 将matrix或array转换为list,才能使用scatter函数,否则会报错
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  # 预测值采用plot()方法绘制
    reDraw.canvas.show()
示例#30
0
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)
    #plt.scatter(dataSet[0],dataSet[1])#2改成 plt.scatter(dataSet[0].tolist(),dataSet[1].tolist())

# 或plt.scatter(array(dataSet[0]),array(dataSet[1].tolist())) 、
# type(dataSet[0])==<class 'numpy.matrixlib.defmatrix.matrix'>是矩阵对象不是一维(1-D)的
    reDraw.a.scatter(array(reDraw.rawDat[:, 0]),
                     array(reDraw.rawDat[:, 1]).tolist(),
                     s=5)  #use scatter for data set
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  #use plot for yHat
    reDraw.canvas.show()
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(list(reDraw.rawDat[:,0]), list(reDraw.rawDat[:,1]), s=5) #use scatter for data set
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0) #use plot for yHat
    reDraw.canvas.draw()
示例#32
0
def reDraw(tolS, tolN):
    reDraw.f.clf()  # 清除图像显示
    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], reDraw.rawDat[:, 1], s=5)  #大小为5
    # 画出预测曲线
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)  #线宽为2
    # 显示画布
    reDraw.canvas.show()
示例#33
0
def test4():
    trainMat = mat(regTrees.loadDataSet('bikeSpeedVsIq_train.txt'))
    testMat = mat(regTrees.loadDataSet('bikeSpeedVsIq_test.txt'))
    #regTrees.plot1(testMat)
    myTree = regTrees.createTree(trainMat, ops=(1, 20))
    yHat = regTrees.createForeCast(myTree, testMat[:, 0])
    print(corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1])
    #regTrees.plot1withTree(trainMat, myTree)

    myTree = regTrees.createTree(trainMat, regTrees.modelLeaf,
                                 regTrees.modelErr, (1, 20))
    yHat = regTrees.createForeCast(myTree, testMat[:, 0],
                                   regTrees.modelTreeEval)
    print(corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1])
    print(myTree)
    regTrees.plot1withTree_Linear(trainMat, myTree)

    ws, X, Y = regTrees.linearSolve(trainMat)
    print(ws)
    for i in range(shape(testMat)[0]):
        yHat[i] = testMat[i, 0] * ws[1, 0] + ws[0, 0]
    print(corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1])
示例#34
0
def reDraw(tolS, tolN):
    # 清空之前的图像,避免重叠
    reDraw.f.clf()
    reDraw.a = reDraw.f.add_subplot(111)
    # 检查复选框是否被选中
    # 确定基于tolS和tolN参数构建模型树还是回归树
    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.rawDat[:,0].A,需要将矩阵转换成数组, 原书错误
    # 真实值散点图绘制
    reDraw.a.scatter(reDraw.rawDat[:, 0].A, reDraw.rawDat[:, 1].A, s=5)
    # 预测值曲线绘制
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    reDraw.canvas.show()
示例#35
0
def reDraw(tolS, tolN):
    fig.clf()
    a = fig.add_subplot(111)
    # if chkBtnVar.get():#勾选 check button,就画模型树
    #     if tolN<2:
    #         tolN=2
    #     myTree=regTrees.createTree(loadData,regTrees.modelLeaf,regTrees.modelErr,ops=(tolS,tolN))
    #     yHat=regTrees.createForeCast(myTree,testData,regTrees.regTreeEval)
    # else:#默认,就画回归树
    myTree = regTrees.createTree(loadData, ops=(tolS, tolN))
    yHat = regTrees.createForeCast(myTree, testData)
    a.scatter(loadData[:, 0], loadData[:, 1], marker="s", s=5)
    a.plot(testData, yHat, linewidth=2.0)
    canvas.show()
示例#36
0
def reDraw(tolS, tolN):
    reDraw.f.clf()
    reDraw.a = reDraw.f.add_subplot(111)
    if chkBtnVar.get():
        #print('模型树')
        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:
        #print('普通树')
        myTree = regTrees.createTree(reDraw.rawDat, ops=(tolS, tolN))
        yHat = regTrees.createForeCast(myTree, reDraw.testDat)
    print('myTree: \n%s' % myTree)
    print('\nyHat: \n%s' % yHat)
    reDraw.a.scatter(reDraw.rawDat[:, 0].tolist(),
                     reDraw.rawDat[:, 1].tolist(),
                     s=5)  #这里使用tolist方法,否则出现1-D的异常
    reDraw.a.plot(reDraw.testDat, yHat, linewidth=2.0)
    reDraw.canvas.show(
    )  #20190107发现新版的python中不使用这个show方法,使用的话,会出错,通过在jyputer中测试发现,这句话不能注释,注释后,图形不能更新
示例#37
0
文件: 9.py 项目: niumeng07/ML
print(regTrees.createTree(myMat2))
myTree=regTrees.createTree(myMat2,ops=(0,1))
myDatTest=regTrees.loadDataSet('ex2test.txt')
myMat2Test=mat(myDatTest)
regTrees.prune(myTree,myMat2Test)
print(myTree)

print("分段函数表示:")
myMat2=mat(regTrees.loadDataSet('exp2.txt'))
print(regTrees.createTree(myMat2,regTrees.modelLeaf,regTrees.modelErr,(1,10)))


trainMat=mat(regTrees.loadDataSet('bikeSpeedVsIq_train.txt'))
testMat=mat(regTrees.loadDataSet('bikeSpeedVsIq_test.txt'))
myTree=regTrees.createTree(trainMat,ops=(1,20))
yHat=regTrees.createForeCast(myTree,testMat[:,0])
print(corrcoef(yHat,testMat[:,1],rowvar=0)[0,1])

ws,X,Y=regTrees.linearSolve(trainMat)
print(ws)
for i in range(shape(testMat)[0]):
    yHat[i]=testMat[i,0]*ws[1,0]+ws[0,0]
print(corrcoef(yHat,testMat[:,1],rowvar=0)[0,1])

from Tkinter import *
root=Tk()
myLabel=Label(root,text="Hello World")
myLabel.grid()
root.mainloop()

示例#38
0
#print myDat2
#print regTrees.createTree(myMat2, ops=(10000,4))



#model tree
#myMat2 = mat(regTrees.loadDataSet('exp2.txt'))
#print regTrees.createTree(myMat2, regTrees.modelLeaf, regTrees.modelErr, (1,10))


trainMat = mat(regTrees.loadDataSet('bikeSpeedVsIq_train.txt'))
testMat = mat(regTrees.loadDataSet('bikeSpeedVsIq_test.txt'))
myTree = regTrees.createTree(trainMat, ops=(1,20))
#print myTree
#___REGRES TREE___
yHat = regTrees.createForeCast(myTree, testMat[:,0])
#print yHat
print corrcoef(yHat, testMat[:,1], rowvar=0)[0,1]
#___MODEL TREE___
myTree = regTrees.createTree(trainMat, regTrees.modelLeaf, regTrees.modelErr, (1,20))
yHat = regTrees.createForeCast(myTree, testMat[:,0], regTrees.modelTreeEval)
print corrcoef(yHat, testMat[:,1], rowvar=0)[0,1]
#___STAND REGRES___
ws,X,Y = regTrees.linearSolve(trainMat)
print "ws=",ws
for i in range(shape(testMat)[0]):
    yHat[i] = testMat[i,0]*ws[1,0]+ws[0,0]
print corrcoef(yHat, testMat[:,1], rowvar=0)[0,1]


myMat2 = mat(myDat2)
#print regTrees.createTree(myMat2)
print regTrees.createTree(myMat2, ops=(10000, 4))

myTree = regTrees.createTree(myMat2, ops=(0, 1))
myDatTest = regTrees.loadDataSet('ex2test.txt')
myMat2Test = mat(myDatTest)
print regTrees.prune(myTree, myMat2Test)

myMat2 = mat(regTrees.loadDataSet('exp2.txt'))
print regTrees.createTree(myMat2, regTrees.modelLeaf, regTrees.modelErr,
                          (1, 10))

trainMat = mat(regTrees.loadDataSet('bikeSpeedVsIq_train.txt'))
testMat = mat(regTrees.loadDataSet('bikeSpeedVsIq_test.txt'))
myTree = regTrees.createTree(trainMat, ops=(1, 20))
yHat = regTrees.createForeCast(myTree, testMat[:, 0])
print corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1]

myTree = regTrees.createTree(trainMat,
                             regTrees.modelLeaf,
                             regTrees.modelErr,
                             ops=(1, 20))
yHat = regTrees.createForeCast(myTree, testMat[:, 0], regTrees.modelTreeEval)
print corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1]

ws, X, Y = regTrees.linearSolve(trainMat)
print ws
for i in range(shape(testMat)[0]):
    yHat[i] = testMat[i, 0] * ws[1, 0] + ws[0, 0]
print corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1]