Example #1
0
def test2():
    ''' line = '0.530897\t0.893462'
    curLine = line.strip().split('\t')
    print(curLine)
    fltLine = map(float, curLine) #在python3里面变成map了。。。错了
    print(list(curLine))
    print(set(fltLine)) '''

    myDat = regTrees.loadDataSet('exp.txt')  #200*2 float, exp和ex2也差不多,y的方差更小
    #print(myDat[2])    #print(list(myDat[2]))一样的
    print(shape(myDat))
    #print(m1[:,-1])    #print(m1[5,:]) # 用mat()转成矩阵才能这样用
    myMat = mat(myDat)
    #retTree = regTrees.createTree(myMat, ops=(1000,10)) #(0,1)就是每个点都分了一个叉,典型的overfitting
    #retTree = regTrees.createTree(myMat, ops=(0.2,4)) #ex2比ex00分布差不多,y的取值大了100倍,因此用10000,4和原来的效果差不多
    #print(retTree)

    retTree = regTrees.createTree(myMat, ops=(10, 4))
    testDat = mat(
        regTrees.loadDataSet('ex2test.txt'))  #ex2test.txt的数据分布范围和ex2很接近,真实的测试集
    pruned_Tree = regTrees.prune(retTree, testDat)
    print(pruned_Tree)

    #regTrees.plot1(myMat)
    regTrees.plot1withTree(myMat, retTree)
    regTrees.plot1withTree(myMat, pruned_Tree)
    def test_compare(self):
        train_mat = mat(regTrees.loadDataSet("bikeSpeedVsIq_train.txt"))
        test_mat = mat(regTrees.loadDataSet("bikeSpeedVsIq_test.txt"))
        # 创建一个回归树
        myTree = regTrees.createTree(train_mat, ops=(1, 20))
        yHat = compareRegression.createForeCast(myTree, test_mat[:, 0])
        corrcoef = compareRegression.corrcoef(yHat, test_mat[:, 1],
                                              rowvar=0)[0, 1]
        print("\n corrcoef == %s" % (corrcoef))

        # 创建一颗模型树
        myTree = regTrees.createTree(train_mat,
                                     linearModelTrees.modelLeaf,
                                     linearModelTrees.modelErr,
                                     ops=(1, 20))
        yHat = compareRegression.createForeCast(
            myTree, test_mat[:, 0], compareRegression.modelTreeEval)
        corrcoef = compareRegression.corrcoef(yHat, test_mat[:, 1],
                                              rowvar=0)[0, 1]
        print("\n corrcoef == %s" % (corrcoef))

        # 标准回归
        ws, X, Y = linearModelTrees.linearSolve(train_mat)
        print("\n ws == %s" % (ws))
        for i in range(shape(test_mat)[0]):
            yHat[i] = test_mat[i, 0] * ws[1, 0] + ws[0, 0]
        corrcoef = compareRegression.corrcoef(yHat, test_mat[:, 1],
                                              rowvar=0)[0, 1]
        print("\n corrcoef == %s" % (corrcoef))
Example #3
0
    def test_prune(self):
        data_set = regTrees.loadDataSet("ex2.txt")
        data_set = mat(data_set)

        tree = regTrees.createTree(data_set, ops=(1, 4))
        print("\n tree == %s" % (tree))

        test_set = regTrees.loadDataSet("ex2test.txt")
        test_set = mat(test_set)
        # 后剪枝,可以看到没有像预期被剪成两部分,说明后剪枝可能不如先剪枝有效
        pruneTree = pruneTrees.prune(tree, test_set)
        print("\n pruneTree == %s" % (pruneTree))
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()
def tkinterTest():
    root = tk.Tk()

    tk.Label(root, text="Plot Place Holder").grid(row=0, columnspan=3)
    tk.Label(root, text="tolN").grid(row=1, column=0)

    tolNentry = tk.Entry(root)
    tolNentry.grid(row=1, column=1)
    tolNentry.insert(0, '10')
    tk.Label(root, text="tolS").grid(row=2, column=0)

    tolSentry = tk.Entry(root)
    tolSentry.grid(row=2, column=1)
    tolSentry.insert(0, '1.0')

    tk.Button(root, text="ReDraw", command=drawNewTree).grid(row=1,
                                                             column=2,
                                                             rowspan=3)

    # IntVar按钮整数值
    chkBtnVar = tk.IntVar()
    chkBtn = tk.Checkbutton(root, text="Model Tree", variable=chkBtnVar)
    chkBtn.grid(row=3, column=0, columnspan=2)
    reDraw.rawDat = mat(regTrees.loadDataSet('sine.txt'))
    reDraw.testDat = arange(min(reDraw.rawDat[:, 0]), max(reDraw.rawDat[:, 0]),
                            0.01)
    reDraw(1.0, 10)

    tk.Button(root, text='Quit', fg="black", command=root.quit).grid(row=1,
                                                                     column=2)

    root.mainloop()
Example #6
0
    def __init__(self, parent):
        self.parent = parent
        fig = Figure(figsize=(5, 4), dpi=100)  #create canvas
        self.f = fig
        self.canvas = FigureCanvasTkAgg(fig, master=root)
        self.canvas.show()
        self.canvas.get_tk_widget().grid(row=0, columnspan=3)

        #Label(root, text="Plot Place Holder").grid(row=0, columnspan=3)
        Label(root, text="tolN").grid(row=1, column=0)
        self.tolNentry = Entry(self.parent)
        self.tolNentry.grid(row=1, column=1)
        self.tolNentry.insert(0, '10')
        Label(root, text="tolS").grid(row=2, column=0)
        self.tolSentry = Entry(root)
        self.tolSentry.grid(row=2, column=1)
        self.tolSentry.insert(0, '1.0')
        Button(root, text="ReDraw", command=self.drawNewTree).grid(row=1,
                                                                   column=2,
                                                                   rowspan=3)
        Button(root, text='Quit', fg="black", command=root.quit).grid(row=1,
                                                                      column=2)
        self.chkBtnVar = IntVar()
        self.chkBtn = Checkbutton(root,
                                  text="Model Tree",
                                  variable=self.chkBtnVar)
        self.chkBtn.grid(row=3, column=0, columnspan=2)
        self.rawDat = mat(regTrees.loadDataSet('sine.txt'))
        self.testDat = arange(min(self.rawDat[:, 0]), max(self.rawDat[:, 0]),
                              0.01)
        self.reDraw(1.0, 10)
Example #7
0
def drawNewTree():
	datPath, tolS, tolN = getInputs()
	try:
		reDraw.rawDat = mat(regTrees.loadDataSet(datPath))
		reDraw.testDat = arange(min(reDraw.rawDat[:,0]), max(reDraw.rawDat[:,0]), 0.01)
		reDraw(tolS, tolN)
	except:
		print "Cannot find file %s" % datPath
Example #8
0
def test3():
    myDat = mat(regTrees.loadDataSet('exp2.txt'))  #200*2 float
    print(shape(myDat))
    retTree = regTrees.createTree(myDat, regTrees.modelLeaf, regTrees.modelErr,
                                  (1, 10))
    print(retTree)
    #regTrees.plot1(myDat)
    regTrees.plot1withTree_Linear(myDat, retTree)
Example #9
0
    def test_linear_model_tree(self):
        data_set = regTrees.loadDataSet("exp2.txt")
        data_set = mat(data_set)

        tree = regTrees.createTree(data_set,
                                   linearModelTrees.modelLeaf,
                                   linearModelTrees.modelErr,
                                   ops=(1, 10))
        print("\n tree == %s" % (tree))
Example #10
0
def SL_Model():
    test = re.loadDataSet('bikeSpeedVsIq_test.txt')
    train = re.loadDataSet('bikeSpeedVsIq_train.txt')
    testMat = mat(test)
    trainMat = mat(train)

    mytree = re.createTree(trainMat, ops=(1, 20))
    yHat = createForeCast(mytree, testMat[:, 0])
    print('回归树的相关性R^2:', corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1])

    myTree = re.createTree(trainMat, tr.modelLeaf, tr.modelErr, ops=(1, 20))
    yHat = createForeCast(myTree, testMat[:, 0], modelTreeEval)
    print('模型树的相关性R^2:', corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1])

    ws, X, Y = tr.linearSolve(trainMat)
    for i in range(shape(testMat)[0]):
        yHat[i] = testMat[i, 0] * ws[1, 0] + ws[0, 0]
    print('标准线性回归的相关性R^2:', corrcoef(yHat, testMat[:, 1], rowvar=0)[0, 1])
Example #11
0
    def test_create_reg_tree(self):
        data_set = regTrees.loadDataSet("ex0.txt")
        data_set = mat(data_set)
        print("\n data_set == %s" % (data_set))
        tree = regTrees.createTree(data_set)
        print("\n tree == %s" % (tree))

        # 测试过拟合
        tree = regTrees.createTree(data_set, ops=(0, 1))
        print("\n tree == %s" % (tree))
Example #12
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])
Example #13
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()
Example #14
0
def test10():
    myDat = regTrees.loadDataSet('ex00.txt')
    fy = mat(myDat)[0:5, 1]
    print(fy)
    afy = fy[0]
    print(afy)
    fy[1] = fy[0]
    print('fy', fy)
    print('fy.T', fy.T)
    print('fy.tolist()', fy.tolist())
    print('fy.T.tolist()', fy.T.tolist())
    print('fy.T.tolist()[0]', fy.T.tolist()[0])
    #print(set(fy)) #出错:unhashable type: 'matrix'
    #print(set(fy.T.tolist()))#出错:TypeError: unhashable type: 'list'
    print(set(fy.T.tolist()[0]))  #这样可以!!实现了对fy里元素的去重复!
    my = map(float, fy)
    print(set(my))  #这样也可以!!实现了对fy里元素的去重复!
Example #15
0
def main(root):
    # 标题
    Label(root, text="Plot Place Holder").grid(row=0, columnspan=3)
    # 输入栏1, 叶子的数量
    Label(root, text="tolN").grid(row=1, column=0)
    global tolNentry
    tolNentry = Entry(root)
    tolNentry.grid(row=1, column=1)
    tolNentry.insert(0, '10')
    # 输入栏2, 误差量
    Label(root, text="tolS").grid(row=2, column=0)
    global tolSentry
    tolSentry = Entry(root)
    tolSentry.grid(row=2, column=1)
    # 设置输出值
    tolSentry.insert(0, '1.0')

    # 设置提交的按钮
    Button(root, text="确定", command=drawNewTree).grid(row=1,
                                                      column=2,
                                                      rowspan=3)

    # 设置复选按钮
    global chkBtnVar
    chkBtnVar = IntVar()
    chkBtn = Checkbutton(root, text="Model Tree", variable=chkBtnVar)
    chkBtn.grid(row=3, column=0, columnspan=2)

    # 退出按钮
    Button(root, text="退出", fg="black", command=quit).grid(row=1, column=2)

    # 创建一个画板 canvas
    reDraw.f = Figure(figsize=(5, 4), dpi=100)
    reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
    reDraw.canvas.show()
    reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

    reDraw.rawDat = mat(regTrees.loadDataSet('db/9.RegTrees/sine.txt'))
    reDraw.testDat = arange(min(reDraw.rawDat[:, 0]), max(reDraw.rawDat[:, 0]),
                            0.01)
    reDraw(1.0, 10)
Example #16
0
def main(root):
    # 标题
    Label(root, text="Plot Place Holder").grid(row=0, columnspan=3)
    # 输入栏1, 叶子的数量
    Label(root, text="tolN").grid(row=1, column=0)
    global tolNentry
    tolNentry = Entry(root)
    tolNentry.grid(row=1, column=1)
    tolNentry.insert(0, '10')
    # 输入栏2, 误差量
    Label(root, text="tolS").grid(row=2, column=0)
    global tolSentry
    tolSentry = Entry(root)
    tolSentry.grid(row=2, column=1)
    # 设置输出值
    tolSentry.insert(0,'1.0')

    # 设置提交的按钮
    Button(root, text="确定", command=drawNewTree).grid(row=1, column=2, rowspan=3)

    # 设置复选按钮
    global chkBtnVar
    chkBtnVar = IntVar()
    chkBtn = Checkbutton(root, text="Model Tree", variable = chkBtnVar)
    chkBtn.grid(row=3, column=0, columnspan=2)

    # 退出按钮
    Button(root, text="退出", fg="black", command=quit).grid(row=1, column=2)

    # 创建一个画板 canvas
    reDraw.f = Figure(figsize=(5, 4), dpi=100)
    reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
    reDraw.canvas.show()
    reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

    reDraw.rawDat = mat(regTrees.loadDataSet('input/9.RegTrees/sine.txt'))
    reDraw.testDat = arange(min(reDraw.rawDat[:, 0]), max(reDraw.rawDat[:, 0]), 0.01)
    reDraw(1.0, 10)
Example #17
0

def drawNewTree():
    tolN, tolS = getInputs()  # get values from Entry boxes
    reDraw(tolS, tolN)


if __name__ == "__main__":
    import regTrees
    testMat = mat(eye(4))
    print("\ntestData:\n", testMat)
    mat0, mat1 = binSplitDataSet(testMat, 2, 0.5)
    print("\nmat0:\n", mat0)
    print("mat1:\n", mat1)

    myDat = regTrees.loadDataSet('ex00.txt')
    myDat = mat(myDat)
    print("\ncreateTree for ex00:\n", regTrees.createTree(myDat))

    myDat = regTrees.loadDataSet('ex0.txt')
    myDat = mat(myDat)
    print("\ncreateTree for ex0:\n", regTrees.createTree(myDat))

    ####################prepuning
    print("\nPrepruning\n###########")
    print("\nOps(0,1):\n", createTree(myDat, ops=(0, 1)))

    #########################ex2
    myDat2 = loadDataSet('ex2.txt')
    myDat2 = mat(myDat2)
    print("\nmyData2 : \n", createTree(myDat2))
Example #18
0

#myDat2 = regTrees.loadDataSet('ex2.txt')
#myDat2 = regTrees.loadDataSet('ex2.txt')
#myMat2 = mat(myDat2, )
#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]):
import regTrees
from numpy import *

myMat2 = mat(regTrees.loadDataSet('exp2.txt'))
print(regTrees.createTree(myMat2, regTrees.modelLeaf, regTrees.modelErr, \
    (1, 10)))
Example #20
0
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.show()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0, '4')
Label(root, text="tolS").grid(row=3, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=3, column=1)
tolSentry.insert(0, '1.0')
Label(root, text="prune").grid(row=5, column=0)
need_prune = Entry(root)
need_prune.grid(row=5, column=1)
need_prune.insert(0, 'false')
Button(root, text="ReDraw", command=drawNewTree).grid(row=1,
                                                      column=2,
                                                      rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable=chkBtnVar)
chkBtn.grid(row=7, column=0, columnspan=2)

reDraw.rawDat = mat(regTrees.loadDataSet('train.txt'))
reDraw.testDat = mat(regTrees.loadDataSet('test.txt'))
reDraw.testX = arange(min(reDraw.testDat[:, 0]), max(reDraw.testDat[:, 0]),
                      0.01)
reDraw(1.0, 4)

root.mainloop()
def drawNewTree():
    tolN, tolS = getInputs()  # get values from Entry boxes
    reDraw(tolS, tolN)


root = Tk()

reDraw.f = Figure(figsize=(5, 4), dpi=100)  # create canvas
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.show()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0, "10")
Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0, "1.0")
Button(root, text="ReDraw", command=drawNewTree).grid(row=1, column=2, rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable=chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)

reDraw.rawDat = mat(regTrees.loadDataSet("sine.txt"))
reDraw.testDat = arange(min(reDraw.rawDat[:, 0]), max(reDraw.rawDat[:, 0]), 0.01)
reDraw(1.0, 10)

root.mainloop()
import regTrees
from numpy import *

# mydat = regTrees.loadDataSet('ex00.txt')
# mydat = mat(mydat)
# print(regTrees.createTree(mydat))
#
# testmat = mat(eye(4))
# mat0,mat1 = regTrees.binSplitDataSet(testmat,1,0.5)
# print(mat0,mat1)

mydat = regTrees.loadDataSet('ex2.txt')
mydat = mat(mydat)
mytree = regTrees.createTree(mydat, ops=(0, 1))
mytest = regTrees.loadDataSet('ex2test.txt')
mytest = mat(mytest)
print(regTrees.prune(mytree, mytest))
root=Tk()

reDraw.f = Figure(figsize=(5,4), dpi=100) #create canvas
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.show()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0,'10')
Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0,'1.0')
Button(root, text="ReDraw", command=drawNewTree).grid(row=1, column=2, rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable = chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)

reDraw.rawDat = mat(regTrees.loadDataSet('sine.txt'))
reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)
dataSet = []
for data in reDraw.testDat:
    datList = []
    datList.append(data)
    dataSet.append(datList)
reDraw.testDat = dataSet
reDraw(1.0, 10)
               
root.mainloop()
Example #24
0
def drawNewTree():
    tolN, tolS = getInputs()  # 从输入文本框中获取参数
    reDraw(tolS, tolN)  # 绘制图


root = Tk()
reDraw.f = Figure(figsize=(5, 4), dpi=100)  # 创建画布
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.show()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)
Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0, '10')
Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0, '1.0')
Button(root, text="ReDraw", command=drawNewTree).grid(row=1,
                                                      column=2,
                                                      rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable=chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)
reDraw.rawDat = mat(re.loadDataSet('sine.txt'))
reDraw.testDat = arange(min(reDraw.rawDat[:, 0]), max(reDraw.rawDat[:, 0]),
                        0.01)
reDraw(1.0, 10)
root.mainloop()
Example #25
0
def drawNewTree():  # 有人点击ReDraw按钮时就会调用该函数
    tolN, tolS = getInputs()  # 得到输入框的值
    reDraw(tolS, tolN)


root = Tk()
# Label(root, text="绘制占位符").grid(row=0, columnspan=3)  # 设置文本,第0行,距0的行值为3,
reDraw.f = Figure(figsize=(5, 4), dpi=100)
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.draw()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)  # Entry为允许单行文本输入的文本框,设置文本框,再定位置第1行第1列,再插入数值
tolNentry.grid(row=1, column=1)
tolNentry.insert(0, "10")

Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0, "1.0")

Button(root, text="重画", command=drawNewTree).grid(row=1, column=2, rowspan=3)  # Botton按钮,设置第1行第2列,列值为3
chkBtnVar = IntVar()  # IntVar为按钮整数值小部件
chkBtn = Checkbutton(root, text="模型树", variable=chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)

reDraw.rawDat = mat(regTrees.loadDataSet("sine.txt"))
reDraw.testDat = arange(min(reDraw.rawDat[:, 0]), max(reDraw.rawDat[:, 0]), 0.01)
reDraw(1.0, 10)
root.mainloop()
Example #26
0
    reDraw(tolS,tolN)
    
root=Tk()

reDraw.f = Figure(figsize=(5,4), dpi=100) #create canvas
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.show()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)#cloumnspan:为跨列

Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0,'10')
Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0,'1.0')
Button(root, text="ReDraw", command=drawNewTree).grid(row=1, column=2, rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable = chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)

reDraw.rawDat = mat(regTrees.loadDataSet('sine.txt')) #python中的函数也可以有变量
reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01) #从原数据集的范围中产生
reDraw(1.0, 10)
               
root.mainloop()

if __name__=='__main__':
    print u'合用tkinter与matplotlib集成..'
    
Example #27
0
import regTrees
from numpy import *

myDat=regTrees.loadDataSet('ex00.txt')
myMat = mat(myDat)
tree = regTrees.createTree(myMat)

print "%s" % tree


myDat1=regTrees.loadDataSet('ex0.txt')
myMat1=mat(myDat1)
tree2 = regTrees.createTree(myMat1) 

print "%s" % tree2
root = Tk()

fig = plt.figure(figsize=(5, 4), dpi=100)  #默认是dpi=80
canvas = FigureCanvasTkAgg(fig, root)
canvas.show()
canvas.get_tk_widget().grid(row=1, columnspan=3)

Label(root, text="Plot Place Holder").grid(row=0, columnspan=3)

Label(root, text="tolS").grid(row=2, column=0)
tolSEntry = Entry(root)
tolSEntry.grid(row=2, column=1)
tolSEntry.insert(0, 1.0)
Label(root, text="tolN").grid(row=3, column=0)
tolNEntry = Entry(root)
tolNEntry.grid(row=3, column=1)
tolNEntry.insert(0, 10)

Button(root, text="ReDraw", command=drawNewTree).grid(row=3,
                                                      column=2,
                                                      columnspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable=chkBtnVar)
chkBtn.grid(row=4, columnspan=3)

loadData = mat(regTrees.loadDataSet("sine.txt"))  #放在reDraw函数外,只需一开始加载一次数据
testData = arange(min(loadData[:, 0]), max(loadData[:, 0]), 0.01)
#
reDraw(1.0, 10)

root.mainloop()
Example #29
0
# regTreesTest.py

import regTrees
from numpy import *

myDat = regTrees.loadDataSet('ex00.txt')
tMat = mat(myDat)

print('-------- loadDataSet regLeaf : ')
print(regTrees.regLeaf(tMat))

print('-------- loadDataSet regErr : ')
print(regTrees.regErr(tMat))

print('-------- tMat : ')
print(tMat)

myTree = regTrees.createTree(tMat)

print('-------- regTree createTree : ')
print(myTree)

myDat2 = regTrees.loadDataSet('ex2.txt')
myMat2 = mat(myDat2)
myTree2 = regTrees.createTree(myMat2, ops=(0, 1))
print('-------- regTree createTree2 : ')
print(myTree2)

myDat3 = regTrees.loadDataSet('ex2test.txt')
myMat2Test = mat(myDat3)

def reDraw(tolS, tolN):
    pass


def drawNewTree():
    pass


root = Tk()
Label(root, text="Plot Place Holder").grid(row=0, columnspan=3)
Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0, '10')
Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0, '1.0')
Button(root, text="ReDraw", command=drawNewTree).grid(row=1,
                                                      column=2,
                                                      rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable=chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)
reDraw.rawDat = mat(regTrees.loadDataSet(homedir + 'sine.txt'))
reDraw.testDat = arange(min(reDraw.rawDat[:, 0]), max(reDraw.rawDat[:, 0]),
                        0.01)
reDraw(1.0, 10)
root.mainloop()
Example #31
0
def main():
    import regTrees
    from numpy import mat
    myDat = regTrees.loadDataSet('/home/jerry/ml_in_action/CART/ex00.txt')
    myMat = mat(myDat)
    print(regTrees.createTree(myMat))
Example #32
0
# This Python file uses the following encoding: utf-8
import os, sys
import regTrees
from numpy import *
from Tkinter import *
root=Tk()
Label(root,text="Plot Place Holder").grid(row=0,columnspan=3)
Label(root,text="tolN").grid(row=1,column=0)
tolNentry=Entry(root)
tolNentry.grid(row=1,column=1)
tolNentry.insert(0,"10")
Label(root,text="tolS").grid(row=2,column=0)
tolSentry=Entry(root)
tolSentry.grid(row=2,column=1)
tolSentry.insert(0,"0.1")
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)
Button(root,text="ReDraw",command=drawNewTree).grid(row=1,column=2,rowspan=3)
chkBtnVar=IntVar()#按钮数值键
chkBtn=Checkbutton(root,text="Model Tree",variable=chkBtnVar)
chkBtn.grid(row=3,column=0,columnspan=2)

import matplotlib
matplotlib.use("Tkagg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
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)
import regTrees
from numpy import *

testMat = mat(eye(4))
print testMat
mat0, mat1 = regTrees.binSplitDataSet(testMat, 1, 0.5)
print mat0
print mat1

myDat = regTrees.loadDataSet('ex00.txt')
myMat = mat(myDat)
print regTrees.createTree(myMat)

myDat1 = regTrees.loadDataSet('ex0.txt')
myMat1 = mat(myDat1)
print regTrees.createTree(myMat1)

#print regTrees.createTree(myMat, ops=(0,1))
myDat2 = regTrees.loadDataSet('ex2.txt')
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))
Example #34
0
import regTrees
from numpy import *

myDat = regTrees.loadDataSet('ex00.txt')
myMat = mat(myDat)
print regTrees.createTree(myMat)
ops = (1, 4)
print(ops)
Example #35
0
import regTrees
import numpy as np
testMat = np.mat(np.eye((4)))
# print(testMat)
# mat0 ,mat1 = regTrees.binSplitDataSet(testMat,1,0.5)
# print(mat0)
# print(mat1)
myDat = regTrees.loadDataSet('ex00.txt')
myMat = np.mat(myDat)
# print(regTrees.createTree(myMat))
myDat1 = regTrees.loadDataSet('ex0.txt')
myMat1 = np.mat(myDat1)
# print(regTrees.createTree(myMat1))
myDat2 = regTrees.loadDataSet('ex2.txt')
myMat2 = np.mat(myDat2)
myTree = regTrees.createTree(myMat2, ops=(0, 1))
myDatTest = regTrees.loadDataSet('ex2test.txt')
myMat2Test = np.mat(myDatTest)
print(regTrees.prune(myTree, myMat2Test))
Example #36
0
File: 9.py Project: niumeng07/ML
#!/usr/bin/env python3
#-*- coding:UTF-8 -*-

import regTrees
from numpy import *


testMat=mat(eye(4))
print(testMat)
mat0,mat1=regTrees.binSplitDataSet(testMat,1,0.5)#1为特征下标,0.5为阈值
print("mat0=\n",mat0)
print("mat1=\n",mat1)

myDat=regTrees.loadDataSet('ex00.txt')
#print(myDat)
myMat=mat(myDat)
print("myMat size:",shape(myMat))
#print(myMat)
print(regTrees.createTree(myMat))

print("ex0.txt")
myDat1=regTrees.loadDataSet('ex0.txt')
myMat1=mat(myDat1)
print(shape(myMat1))#200 3
print(regTrees.createTree(myMat1))
#建树完成
myDat2=regTrees.loadDataSet('ex2.txt')
myMat2=mat(myDat2)
print(regTrees.createTree(myMat2))
myTree=regTrees.createTree(myMat2,ops=(0,1))
myDatTest=regTrees.loadDataSet('ex2test.txt')
Example #37
0
def drawNewTree():
    tolN,tolS = getInputs()#get values from Entry boxes
    reDraw(tolS,tolN)

root=Tk()

reDraw.f = Figure(figsize=(5,4), dpi=100) #create canvas
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.show()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0,'10')
Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0,'1.0')
Button(root, text="ReDraw", command=drawNewTree).grid(row=1, column=2, rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable = chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)

reDraw.rawDat = mat(regTrees.loadDataSet('/home/wangxy/data/sine.txt'))
reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)
reDraw(1.0, 10)

root.mainloop()
Example #38
0
    reDraw(tolS, tolN)


root = Tk()

reDraw.f = Figure(figsize=(5, 4), dpi=100)  #create canvas
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.show()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0, '10')
Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0, '1.0')
Button(root, text="ReDraw", command=drawNewTree).grid(row=1,
                                                      column=2,
                                                      rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable=chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)

reDraw.rawDat = mat(regTrees.loadDataSet('sine.txt'))
reDraw.testDat = arange(min(reDraw.rawDat[:, 0]), max(reDraw.rawDat[:, 0]),
                        0.01)
reDraw(1.0, 10)

root.mainloop()
Example #39
0
def drawNewTree():
    tolN,tolS = getInputs()#get values from Entry boxes
    reDraw(tolS,tolN)

root=Tk()

reDraw.f = Figure(figsize=(5,4), dpi=100) #create canvas
reDraw.canvas = FigureCanvasTkAgg(reDraw.f, master=root)
reDraw.canvas.show()
reDraw.canvas.get_tk_widget().grid(row=0, columnspan=3)

Label(root, text="tolN").grid(row=1, column=0)
tolNentry = Entry(root)
tolNentry.grid(row=1, column=1)
tolNentry.insert(0,'10')
Label(root, text="tolS").grid(row=2, column=0)
tolSentry = Entry(root)
tolSentry.grid(row=2, column=1)
tolSentry.insert(0,'1.0')
Button(root, text="ReDraw", command=drawNewTree).grid(row=1, column=2, rowspan=3)
chkBtnVar = IntVar()
chkBtn = Checkbutton(root, text="Model Tree", variable = chkBtnVar)
chkBtn.grid(row=3, column=0, columnspan=2)

reDraw.rawDat = mat(regTrees.loadDataSet("C:/Users/Administrator/Desktop/season2/period2/user_purchase_and_redeem_table/purchase/4month.txt"))
reDraw.testDat = arange(min(reDraw.rawDat[:,0]),max(reDraw.rawDat[:,0]),0.01)
reDraw(1.0, 10)

root.mainloop()