Beispiel #1
0
def case2():
    y0 = [[0,0],[1,0],[0.3,0],[2.7,0]]
    t_tuple = 2
    stepsize = 0.001
    order = 2
    maxorder = 2

    # start = time.time()
    t_list, y_list = simulation_ode(conti_test, y0, t_tuple, stepsize, eps=0)
    # end_simulation = time.time()
    # result_coef, calcdiff_time, pseudoinv_time = infer_dynamic(t_list, y_list, stepsize, order)
    # end_inference = time.time()

    # print(result_coef)
    # print()
    # print("Total time: ", end_inference-start)
    # print("Simulation time: ", end_simulation-start)
    # print("Calc-diff time: ", calcdiff_time)
    # print("Pseudoinv time: ", pseudoinv_time)

    for temp_y in y_list:
        y0_list = temp_y.T[0]
        y1_list = temp_y.T[1]
        plt.plot(y0_list,y1_list,'b')
    plt.show()
    P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.01)
    print(P)
    print(G)
    print(D)
Beispiel #2
0
def case3():
    y0 = [[5,5,5],[2,2,2]]
    t_tuple = 5
    stepsize = 0.01
    maxorder = 2
    t_list, y_list = simulation_ode(fvdp3_3, y0, t_tuple, stepsize, eps=0)
    draw3D(y_list)
    A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize)
    # A, b1, b2, Y = diff_method_backandfor(t_list, y_list, maxorder, stepsize)
    # print(b1)
    # print(b2)
    P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.02)
    print(P)
    print(G)
    print(D)
Beispiel #3
0
def case1():
    np.random.seed(0)
    modetr = get_modetr(0)
    event = get_event(0)
    labeltest = get_labeltest(0)
    y0 = [[1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0]]
    # y1 = [[3,-1], [-1,3]]
    T = 5
    stepsize = 0.005
    maxorder = 2
    boundary_order = 1
    num_mode = 5
    ep = 0.005
    mergeep = 0.01
    t_list, y_list = simulation_ode_3(modetr, event, labeltest, y0, T,
                                      stepsize)
    # A, b1, b2, Y, ytuple = diff_method_backandfor(t_list, y_list, maxorder, stepsize)
    # res, drop, clfs = segment_and_fit(A, b1, b2, ytuple,ep=0.005)
    # np.savetxt("data/YY.txt",Y,fmt='%8f')
    # print(len(res))
    # print(res)
    # P, G = merge_cluster_tol2(res, A, b1, num_mode, ep)
    A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize)
    np.savetxt("data/YY.txt", Y, fmt='%8f')
    P, G, D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, ep)
    # print(P)
    print(len(P))
    if len(P) > num_mode:
        P, G = merge_cluster_tol2(P, A, b, num_mode, ep)
    P, _ = dropclass0(P, G, D, A, b, Y, ep, stepsize)
    print(P)
    print(len(P))
    L_y = len(y_list[0][0])
    boundary = svm_classify(P, Y, L_y, boundary_order, num_mode)
    print(boundary)
    d = test_model(P,
                   G,
                   boundary,
                   num_mode,
                   y_list,
                   modetr,
                   event,
                   maxorder,
                   boundary_order,
                   labeltest=labeltest)
    print(d)
Beispiel #4
0
def case(y0, t_tuple, stepsize, maxorder, modelist, event, ep, method):
    t_list, y_list = simulation_ode_2(modelist, event, y0, t_tuple, stepsize)

    if method == "new":

        A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize)
        P, G, D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder,
                                          0.01)
        P, D = dropclass(P, G, D, A, b, Y, 0.01, stepsize)
        # print(len(P))
        print(G)
        y = []
        x = []

        for j in range(0, len(P[0])):
            y.append(1)
            x.append({1: Y[P[0][j], 0], 2: Y[P[0][j], 1], 3: Y[P[0][j], 2]})

        for j in range(0, len(P[1])):
            y.append(-1)
            x.append({1: Y[P[1][j], 0], 2: Y[P[1][j], 1], 3: Y[P[1][j], 2]})

        prob = svm_problem(y, x)
        param = svm_parameter('-t 1 -d 1 -c 10 -r 1 -b 0 -q')
        m = svm_train(prob, param)
        svm_save_model('model_file', m)
        nsv = m.get_nr_sv()
        svc = m.get_sv_coef()
        sv = m.get_SV()
        g = -m.rho[0]
        a1 = 0
        a2 = 0
        a3 = 0
        for i in range(0, nsv):
            a1 = a1 + svc[i][0] * 0.5 * sv[i][1]
            a2 = a2 + svc[i][0] * 0.5 * sv[i][2]
            a3 = a3 + svc[i][0] * 0.5 * sv[i][3]
            g = g + svc[i][0]

        print(a1 / a1, a2 / a1, a3 / a1, g / a1)

        def f(x):
            return a1 * x[0] + a2 * x[1] + a3 * x[2] + g > 0

        @eventAttr()
        def eventtest(t, y):
            y0, y1, y2 = y
            return a1 * y0 + a2 * y1 + a3 * y2 + g

        ttest_list, ytest_list = simulation_ode_2(
            [ode_test(G[0], maxorder),
             ode_test(G[1], maxorder)], eventtest, y0, t_tuple, stepsize)
        ax = plt.axes(projection='3d')
        for temp_y in y_list[0:1]:
            y0_list = temp_y.T[0]
            y1_list = temp_y.T[1]
            y2_list = temp_y.T[2]
            ax.plot3D(y0_list, y1_list, y2_list, c='b')
        for temp_y in ytest_list[0:1]:
            y0_list = temp_y.T[0]
            y1_list = temp_y.T[1]
            y2_list = temp_y.T[2]
            ax.plot3D(y0_list, y1_list, y2_list, c='r')
        plt.show()

    sum = 0
    num = 0

    def get_poly_pt(x):
        gene = generate_complete_polynomial(len(x), maxorder)
        val = []
        for i in range(gene.shape[0]):
            val.append(1.0)
            for j in range(gene.shape[1]):
                val[i] = val[i] * (x[j]**gene[i, j])
        poly_pt = np.mat(val)
        return poly_pt

    for ypoints in y_list:
        num = num + ypoints.shape[0]
        for i in range(ypoints.shape[0]):
            if event(0, ypoints[i]) > 0:
                exact = modelist[0](0, ypoints[i])
            else:
                exact = modelist[1](0, ypoints[i])
            if f(ypoints[i]) == 1:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[0].T)
            else:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[1].T)

            exact = np.mat(exact)
            diff = exact - predict
            c = 0
            a = 0
            b = 0
            for j in range(diff.shape[1]):
                c = c + diff[0, j]**2
                a = a + exact[0, j]**2
                b = b + predict[0, j]**2
            f1 = np.sqrt(c)
            f2 = np.sqrt(a) + np.sqrt(b)
            sum = sum + f1 / f2

    return sum / num
Beispiel #5
0
def case(y0, t_tuple, stepsize, maxorder, modelist, event, ep, method):
    t_list, y_list = simulation_ode_2(modelist, event, y0, t_tuple, stepsize)

    if method == "new":

        A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize)
        P, G, D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder,
                                          0.01)
        P, D = dropclass(P, G, D, A, b, Y, 0.01, stepsize)
        # print(len(P))
        # print(G)

        y = []
        x = []

        for j in range(0, len(P[0])):
            y.append(1)
            x.append({1: Y[P[0][j], 0], 2: Y[P[0][j], 1]})

        for j in range(0, len(P[1])):
            y.append(-1)
            x.append({1: Y[P[1][j], 0], 2: Y[P[1][j], 1]})

        prob = svm_problem(y, x)
        param = svm_parameter('-t 1 -d 2 -r 1 -c 10 -b 0 -q')
        m = svm_train(prob, param)
        svm_save_model('model_file', m)
        # p_label, p_acc, p_val = svm_predict(y, x, m)
        nsv = m.get_nr_sv()
        svc = m.get_sv_coef()
        sv = m.get_SV()

        def f(x):
            g = -m.rho[0]
            for i in range(0, nsv):
                g = g + svc[i][0] * (0.5 *
                                     (x[0] * sv[i][1] + x[1] * sv[i][2]) +
                                     1)**2
            return g > 0

    sum = 0
    num = 0

    def get_poly_pt(x):
        gene = generate_complete_polynomial(len(x), maxorder)
        val = []
        for i in range(gene.shape[0]):
            val.append(1.0)
            for j in range(gene.shape[1]):
                val[i] = val[i] * (x[j]**gene[i, j])
        poly_pt = np.mat(val)
        return poly_pt

    for ypoints in y_list:
        num = num + ypoints.shape[0]
        for i in range(ypoints.shape[0]):
            if event(0, ypoints[i]) > 0:
                exact = modelist[0](0, ypoints[i])
            else:
                exact = modelist[1](0, ypoints[i])
            if f(ypoints[i]) == 1:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[0].T)
            else:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[1].T)

            exact = np.mat(exact)
            diff = exact - predict
            c = 0
            a = 0
            b = 0
            for j in range(diff.shape[1]):
                c = c + diff[0, j]**2
                a = a + exact[0, j]**2
                b = b + predict[0, j]**2
            f1 = np.sqrt(c)
            f2 = np.sqrt(a) + np.sqrt(b)
            sum = sum + f1 / f2

    return sum / num
Beispiel #6
0
def case10():
    y0 = [[-1,1],[1,4],[2,-3]]
    t_tuple = [(0,5),(0,5),(0,5)]
    stepsize = 0.01
    maxorder = 2
    # start = time.time()
    def labeltest(y):
        if eventtr_1(0,y)<0 and eventtr_2(0,y)>0:
            return 0
        elif eventtr_1(0,y)>=0 and eventtr_2(0,y)>0:
            return 1
        else:
            return 2

    t_list, y_list = simulation_ode_3([modetr_1, modetr_2, modetr_3], [eventtr_1,eventtr_2,eventtr_2], labeltest, y0, t_tuple, stepsize)
    draw2D(y_list)
    A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize)
    P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.01)
    print(P)
    print(G)
    print(D)
    # for p in P:
    #     print(len(p))
    # tpar_list,ypar_list = parti(t_list,y_list,0.2,1/3)
    
    # for temp in tpar_list:
    #     print(temp[-1])
    # labels = infer_dynamic_modes_ex_dbs(tpar_list, ypar_list, stepsize, maxorder, 0.02)
    # print(labels)
    # draw2D(ypar_list)
    for i in range(0,len(P)):
        y0_list = []
        y1_list = []
        for j in range(0,len(P[i])):
            y0_list.append(Y[P[i][j],0])
            y1_list.append(Y[P[i][j],1])
    
        plt.scatter(y0_list,y1_list,s=1)
    plt.show()
    
    P,G = reclass(A,b,P,0.01)
    for p in P:
        print(len(p))
    print(G)

    for i in range(0,len(P)):
        y0_list = []
        y1_list = []
        for j in range(0,len(P[i])):
            y0_list.append(Y[P[i][j],0])
            y1_list.append(Y[P[i][j],1])
    
        plt.scatter(y0_list,y1_list,s=1)
    plt.show()
    P,D = dropclass(P,G,D,A,b,Y,0.01,0.01)
    print(D)
    for i in range(0,len(P)):
        y0_list = []
        y1_list = []
        for j in range(0,len(P[i])):
            y0_list.append(Y[P[i][j],0])
            y1_list.append(Y[P[i][j],1])
    
        plt.scatter(y0_list,y1_list,s=1)
    # plt.show()
    
    y=[]
    x=[]

    for j in range(0,len(P[2])):
        y.append(1)
        x.append({1:Y[P[2][j],0], 2:Y[P[2][j],1]})
    
    for j in range(0,len(P[1])):
        y.append(-1)
        x.append({1:Y[P[1][j],0], 2:Y[P[1][j],1]})
    
    for j in range(0,len(P[0])):
        y.append(-1)
        x.append({1:Y[P[0][j],0], 2:Y[P[0][j],1]})

    prob  = svm_problem(y, x)
    param = svm_parameter('-t 1 -d 1 -c 100 -r 1 -b 0')
    m = svm_train(prob, param)
    svm_save_model('model_file', m)
    print("pred")
    p_label, p_acc, p_val = svm_predict(y, x, m)
    # print(p_label)
    nsv = m.get_nr_sv()
    svc = m.get_sv_coef()
    sv = m.get_SV()
    # print(nsv)
    # print(svc)
    # print(sv)
    
    # def clafun(x):
    #     g = -m.rho[0]
    #     for i in range(0,nsv):
    #         g = g + svc[i][0] * ((0.5 * (x[0]*sv[i][1] + x[1]*sv[i][2]))**3)
    #     return g

    g = -m.rho[0]
    a1 = 0
    a2 = 0
    for i in range(0,nsv):
        a1 = a1 + svc[i][0] * 0.5 * sv[i][1]
        a2 = a2 + svc[i][0] * 0.5 * sv[i][2]
        g = g + svc[i][0]*1
    print(a1)
    print(a2)
    print(g)

    def f(x,y):
        g = -m.rho[0]
        for i in range(0,nsv):
            g = g + svc[i][0] * (0.5*(x*sv[i][1]+y*sv[i][2])+1)
        return g

    x=[]
    y=[]

    for j in range(0,len(P[1])):
        y.append(1)
        x.append({1:Y[P[1][j],0], 2:Y[P[1][j],1]})
    
    for j in range(0,len(P[0])):
        y.append(-1)
        x.append({1:Y[P[0][j],0], 2:Y[P[0][j],1]})

    prob  = svm_problem(y, x)
    param = svm_parameter('-t 1 -d 1 -c 100 -r 1 -b 0')
    m = svm_train(prob, param)
    svm_save_model('model_file', m)
    print("pred")
    p_label, p_acc, p_val = svm_predict(y, x, m)
    # print(p_label)
    nsv = m.get_nr_sv()
    svc = m.get_sv_coef()
    sv = m.get_SV()
    # print(nsv)
    # print(svc)
    # print(sv)
    
    # def clafun(x):
    #     g = -m.rho[0]
    #     for i in range(0,nsv):
    #         g = g + svc[i][0] * ((0.5 * (x[0]*sv[i][1] + x[1]*sv[i][2]))**3)
    #     return g

    g1 = -m.rho[0]
    b1 = 0
    b2 = 0
    for i in range(0,nsv):
        b1 = b1 + svc[i][0] * 0.5 * sv[i][1]
        b2 = b2 + svc[i][0] * 0.5 * sv[i][2]
        g1 = g1 + svc[i][0]*1
    # print(a1)
    # print(a2)
    # print(g)

    def h(x,y):
        g = -m.rho[0]
        for i in range(0,nsv):
            g = g + svc[i][0] * (0.5*(x*sv[i][1]+y*sv[i][2])+1)
        return g
    x = np.linspace(-10,10,100)
    y = np.linspace(-10,10,100)
    
    X,Y = np.meshgrid(x,y)#将x,y指传入网格中
    # plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)#8指图中的8+1根线,绘制等温线,其中cmap指颜色
    
    C = plt.contour(X,Y,f(X,Y),[0])#colors指等高线颜色
    plt.clabel(C,inline=True,fontsize=10)#inline=True指字体在等高线中
    D = plt.contour(X,Y,h(X,Y),[0])#colors指等高线颜色
    plt.clabel(D,inline=True,fontsize=10)#inline=True指字体在等高线中
    
    plt.xticks(())
    plt.yticks(())
    plt.show()
Beispiel #7
0
def case1():
    # y0 = [[1,3],[-1,-2],[-3,-5],[2,4]]
    # t_tuple = [(0,20),(0,10),(0,15),(0,15)]
    y0 = [[1,3],[-1,-2]]
    t_tuple = 20
    stepsize = 0.01
    order = 2
    maxorder = 2
    # start = time.time()
    t_list, y_list = simulation_ode(mode2_1, y0, t_tuple, stepsize, eps=0)

    # for temp_y in y_list:
    #     y0_list = temp_y.T[0]
    #     y1_list = temp_y.T[1]
    #     plt.plot(y0_list,y1_list,'b')
    # plt.show()
    
    A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize)
    P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.01)
    
    print(P)
    print(G)
    print(D)
    y = []
    x = []

    for j in range(0,len(P[0])):
        y.append(1)
        x.append({1:Y[P[0][j],0], 2:Y[P[0][j],1]})
    
    for j in range(0,len(P[1])):
        y.append(-1)
        x.append({1:Y[P[1][j],0], 2:Y[P[1][j],1]})

    prob  = svm_problem(y, x)
    param = svm_parameter('-t 1 -d 1 -c 10 -b 0 ')
    m = svm_train(prob, param)
    svm_save_model('model_file', m)
    yt = [-1,-1,1,1]
    xt = [{1:1, 2:1},{1:-1, 2:1},{1:1, 2:-1},{1:-1, 2:-1}]
    print("pred")
    p_label, p_acc, p_val = svm_predict(y, x, m)
    # print(p_label)
    nsv = m.get_nr_sv()
    svc = m.get_sv_coef()
    sv = m.get_SV()
    # print(nsv)
    # print(svc)
    # print(sv)
    
    # def clafun(x):
    #     g = -m.rho[0]
    #     for i in range(0,nsv):
    #         g = g + svc[i][0] * ((0.5 * (x[0]*sv[i][1] + x[1]*sv[i][2]))**3)
    #     return g

    g = -m.rho[0]
    a1 = 0
    a2 = 0
    for i in range(0,nsv):
        a1 = a1 + svc[i][0] * 0.5 * sv[i][1]
        a2 = a2 + svc[i][0] * 0.5 * sv[i][2]

    print("a1",a1/a1)
    print("a2",a2/a1)
    print("g",g/a1)
    
    
    
 
    # ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap='rainbow')
    # plt.show()
    dim = G[0].shape[0]
    A = generate_complete_polynomial(dim,maxorder)
    def odepre(t,y):
        # print("in")
        basicf = []
        for i in range(0,A.shape[0]):
            ap = 1
            for j in range(0,A.shape[1]):
                ap = ap*(y[j]**A[i][j])
            basicf.append(ap)
        b = np.array(basicf)
        dydt = np.zeros(dim)
        if a1 * y[0] + a2 * y[1] + g > 0: 
            for l in range(0,dim):
                dydt[l] = G[0][l].dot(b)
        else:
            for l in range(0,dim):
                dydt[l] = G[1][l].dot(b)
        # print("out")
        return dydt
    py0 = [[2,4],[-1,-3]]
    pt_tuple = [(0,10),(0,10)]
    start = time.time()
    print("origin")
    tp_list, yp_list = simulation_ode(mode2_1, py0, pt_tuple, stepsize, eps=0)
    end1 = time.time()
    print("predict")
    tpre_list, ypre_list = simulation_ode(odepre, py0, pt_tuple, stepsize, eps=0)
    end2 = time.time()
    print("simutime",end1-start)
    print("predtime",end2-end1)

    for temp_y in yp_list:
        y0_list = temp_y.T[0]
        y1_list = temp_y.T[1]
        plt.plot(y0_list,y1_list,'b')
    for temp_y in ypre_list:
        y0_list = temp_y.T[0]
        y1_list = temp_y.T[1]
        plt.plot(y0_list,y1_list,'r')
    plt.show()
Beispiel #8
0
def case7():
    y0 = [[4,0.1,3.1,0],[5.9,0.2,-3,0],[4.1,0.5,2,0],[6,0.7,2,0]]
    t_tuple = [(0,5),(0,5),(0,5),(0,5)]
    stepsize = 0.01
    maxorder = 2
    # start = time.time()
    t_list, y_list = simulation_ode_2([mmode1, mmode2], event3, y0, t_tuple, stepsize)

    # for temp_y in y_list:
    #     y0_list = temp_y.T[0]
    #     y1_list = temp_y.T[1]
    #     plt.plot(y0_list,y1_list,'b')
    # plt.show()
    A, b, Y = diff_method_new1(t_list, y_list, maxorder, stepsize)
    P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.03)
    print(P)
    print(G)
    print(D)
    y = []
    x = []

    for j in range(0,len(P[0])):
        y.append(1)
        x.append({1:Y[P[0][j],0], 2:Y[P[0][j],1], 3:Y[P[0][j],2], 4:Y[P[0][j],3]})
    
    for j in range(0,len(P[1])):
        y.append(-1)
        x.append({1:Y[P[1][j],0], 2:Y[P[1][j],1], 3:Y[P[1][j],2], 4:Y[P[1][j],3]})

    prob  = svm_problem(y, x)
    param = svm_parameter('-t 1 -d 1 -c 10 -r 1 -b 1')
    m = svm_train(prob, param)
    svm_save_model('model_file', m)
    # print("pred")
    p_label, p_acc, p_val = svm_predict(y, x, m,'-q')
    # print(p_label)
    print('pre',p_acc[0])
    # print(p_val)
    nsv = m.get_nr_sv()
    svc = m.get_sv_coef()
    sv = m.get_SV()
    # print(nsv)
    # print(svc)
    # print(sv)
    
    # def clafun(x):
    #     g = -m.rho[0]
    #     for i in range(0,nsv):
    #         g = g + svc[i][0] * ((0.5 * (x[0]*sv[i][1] + x[1]*sv[i][2]))**3)
    #     return g

    g = -m.rho[0]
    a1 = 0
    a2 = 0
    a3 = 0
    a4 = 0
    for i in range(0,nsv):
        a1 = a1 + svc[i][0] * 0.5 * sv[i][1]
        a2 = a2 + svc[i][0] * 0.5 * sv[i][2]
        a3 = a3 + svc[i][0] * 0.5 * sv[i][3]
        a4 = a4 + svc[i][0] * 0.5 * sv[i][4]
        g = g + svc[i][0]*1
Beispiel #9
0
def case6():
    y0 = [[0,1],[0,2],[0,3],[0,4],[0,5]]
    t_tuple = 20
    stepsize = 0.01
    maxorder = 3
    # start = time.time()
    t_list, y_list = simulation_ode_2([mode1, mode2], event2, y0, t_tuple, stepsize)

    for temp_y in y_list:
        y0_list = temp_y.T[0]
        y1_list = temp_y.T[1]
        plt.plot(y0_list,y1_list,'b')
    plt.show()
    # clfs, boundary = infer_multi_ch.infer_multi_linear_new(t_list, y_list, stepsize, maxorder)
    # for clf in clfs:
    #     print('g',clf.coef_)
    # print(boundary)
    A, b, Y = diff_method_new1(t_list, y_list, maxorder, stepsize)
    P,G,D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder, 0.01)
    print(P)
    print(G)
    print(D)
    y = []
    x = []

    for j in range(0,len(P[0])):
        y.append(1)
        x.append({1:Y[P[0][j],0], 2:Y[P[0][j],1]})
    
    
    for j in range(0,len(P[1])):
        y.append(-1)
        x.append({1:Y[P[1][j],0], 2:Y[P[1][j],1]})


    y0_list = []
    y1_list = []
    for j in range(0,len(P[0])):
        y0_list.append(Y[P[0][j],0])
        y1_list.append(Y[P[0][j],1])
    
    plt.scatter(y0_list,y1_list,s=1)
    y0_list = []
    y1_list = []
    
    for j in range(0,len(P[1])):
        y0_list.append(Y[P[1][j],0])
        y1_list.append(Y[P[1][j],1])
    
    plt.scatter(y0_list,y1_list,s=1)
    

    prob  = svm_problem(y, x)
    param = svm_parameter('-t 1 -d 2 -r 1 -c 10 -b 0')
    m = svm_train(prob, param)
    svm_save_model('model_file', m)
    print("pred")
    p_label, p_acc, p_val = svm_predict(y, x, m)
    # # print(p_label)
    nsv = m.get_nr_sv()
    svc = m.get_sv_coef()
    sv = m.get_SV()
    # print(nsv)
    # print(svc)
    # print(sv)
    
    def f(x,y):
        g = -m.rho[0]
        for i in range(0,nsv):
            g = g + svc[i][0] * (0.5*(x*sv[i][1]+y*sv[i][2])+1)**2
        return g

    x = np.linspace(-5,5,100)
    y = np.linspace(-5,5,100)
    
    X,Y = np.meshgrid(x,y)#将x,y指传入网格中
    # plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)#8指图中的8+1根线,绘制等温线,其中cmap指颜色
    
    C = plt.contour(X,Y,f(X,Y),[0])#colors指等高线颜色
    plt.clabel(C,inline=True,fontsize=10)#inline=True指字体在等高线中
    
    plt.xticks(())
    plt.yticks(())
    plt.show()
Beispiel #10
0
def case(y0, t_tuple, stepsize, maxorder, modelist, eventlist, labeltest, ep,
         method):
    t_list, y_list = simulation_ode_3(modelist, eventlist, labeltest, y0,
                                      t_tuple, stepsize)

    if method == "new":
        A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize)
        P, G, D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder,
                                          ep)
        P, G = reclass(A, b, P, ep)
        P, D = dropclass(P, G, D, A, b, Y, ep, stepsize)
        print(G)

        for i in range(0, len(P)):
            y0_list = []
            y1_list = []
            for j in range(0, len(P[i])):
                y0_list.append(Y[P[i][j], 0])
                y1_list.append(Y[P[i][j], 1])

            plt.scatter(y0_list, y1_list, s=1)
        plt.show()

        y = []
        x = []

        for j in range(0, len(P[2])):
            y.append(1)
            x.append({1: Y[P[2][j], 0], 2: Y[P[2][j], 1]})

        for j in range(0, len(P[1])):
            y.append(-1)
            x.append({1: Y[P[1][j], 0], 2: Y[P[1][j], 1]})

        for j in range(0, len(P[0])):
            y.append(-1)
            x.append({1: Y[P[0][j], 0], 2: Y[P[0][j], 1]})

        prob = svm_problem(y, x)
        param = svm_parameter('-t 1 -d 1 -c 100 -r 1 -b 0 -q')
        m = svm_train(prob, param)
        svm_save_model('model_file1', m)
        nsv = m.get_nr_sv()
        svc = m.get_sv_coef()
        sv = m.get_SV()
        g = -m.rho[0]
        a1 = 0
        a2 = 0
        for i in range(0, nsv):
            a1 = a1 + svc[i][0] * 0.5 * sv[i][1]
            a2 = a2 + svc[i][0] * 0.5 * sv[i][2]
            g = g + svc[i][0] * 1
        print(a1 / a2, a2 / a2, g / a2)

        def f(x):
            g = -m.rho[0]
            for i in range(0, nsv):
                g = g + svc[i][0] * (0.5 *
                                     (x[0] * sv[i][1] + x[1] * sv[i][2]) + 1)
            return g > 0

        x = []
        y = []

        for j in range(0, len(P[1])):
            y.append(1)
            x.append({1: Y[P[1][j], 0], 2: Y[P[1][j], 1]})

        for j in range(0, len(P[0])):
            y.append(-1)
            x.append({1: Y[P[0][j], 0], 2: Y[P[0][j], 1]})

        prob = svm_problem(y, x)
        param = svm_parameter('-t 1 -d 1 -c 100 -r 1 -b 0 -q')
        n = svm_train(prob, param)
        svm_save_model('model_file2', n)
        # p_label, p_acc, p_val = svm_predict(y, x, n)
        nsv1 = n.get_nr_sv()
        svc1 = n.get_sv_coef()
        sv1 = n.get_SV()
        g1 = -n.rho[0]
        b1 = 0
        b2 = 0
        for i in range(0, nsv1):
            b1 = b1 + svc1[i][0] * 0.5 * sv1[i][1]
            b2 = b2 + svc1[i][0] * 0.5 * sv1[i][2]
            g1 = g1 + svc1[i][0] * 1
        print(b1 / b1, b2 / b1, g1 / b1)

        def h(x):
            g = -n.rho[0]
            for i in range(0, nsv1):
                g = g + svc1[i][0] * (0.5 *
                                      (x[0] * sv1[i][1] + x[1] * sv1[i][2]) +
                                      1)
            return g > 0

        @eventAttr()
        def eventtest1(t, y):
            y0, y1 = y
            return a1 * y0 + a2 * y1 + g

        @eventAttr()
        def eventtest2(t, y):
            y0, y1 = y
            return b1 * y0 + b2 * y1 + g1

        def labeltesttest(y):
            if eventtest1(0, y) > 0:
                return 2
            elif eventtest2(0, y) > 0:
                return 1
            else:
                return 0

        ttest_list, ytest_list = simulation_ode_3([
            ode_test(G[0], maxorder),
            ode_test(G[1], maxorder),
            ode_test(G[2], maxorder)
        ], [eventtest1, eventtest2], labeltesttest, y0, t_tuple, stepsize)

        for temp_y in y_list:
            y0_list = temp_y.T[0]
            y1_list = temp_y.T[1]
            plt.plot(y0_list, y1_list, c='b')
        for temp_y in ytest_list:
            y0_list = temp_y.T[0]
            y1_list = temp_y.T[1]
            plt.plot(y0_list, y1_list, c='r')
        plt.show()

    sum = 0
    num = 0

    def get_poly_pt(x):
        gene = generate_complete_polynomial(len(x), maxorder)
        val = []
        for i in range(gene.shape[0]):
            val.append(1.0)
            for j in range(gene.shape[1]):
                val[i] = val[i] * (x[j]**gene[i, j])
        poly_pt = np.mat(val)
        return poly_pt

    for ypoints in y_list:
        num = num + ypoints.shape[0]
        for i in range(ypoints.shape[0]):
            exact = modelist[labeltest(ypoints[i])](0, ypoints[i])
            if f(ypoints[i]) == 1:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[2].T)
            elif h(ypoints[i]) == 1:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[1].T)
            else:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[0].T)

            exact = np.mat(exact)
            diff = exact - predict
            c = 0
            a = 0
            b = 0
            for j in range(diff.shape[1]):
                c = c + diff[0, j]**2
                a = a + exact[0, j]**2
                b = b + predict[0, j]**2
            f1 = np.sqrt(c)
            f2 = np.sqrt(a) + np.sqrt(b)
            sum = sum + f1 / f2

    return sum / num
Beispiel #11
0
def case(y0, t_tuple, stepsize, maxorder, modelist, event, ep, method):
    # print('Simulating')
    t_list, y_list = simulation_ode_2(modelist, event, y0, t_tuple, stepsize)
    draw2D(y_list)

    if method == "new":
        # print('Classifying')
        A, b, Y = diff_method_new(t_list, y_list, maxorder, stepsize)
        P, G, D = infer_dynamic_modes_new(t_list, y_list, stepsize, maxorder,
                                          ep)
        P, G = reclass(A, b, P, ep)
        print(G)
        P, D = dropclass(P, G, D, A, b, Y, ep, stepsize)
        # print('Number of modes:', len(P))

        y = []
        x = []

        for j in range(0, len(P[0])):
            y.append(1)
            x.append({1: Y[P[0][j], 0], 2: Y[P[0][j], 1]})

        for j in range(0, len(P[1])):
            y.append(-1)
            x.append({1: Y[P[1][j], 0], 2: Y[P[1][j], 1]})

        prob = svm_problem(y, x)
        param = svm_parameter('-t 1 -d 1 -c 10 -r 1 -b 0 -q')
        m = svm_train(prob, param)
        svm_save_model('model_file', m)
        nsv = m.get_nr_sv()
        svc = m.get_sv_coef()
        sv = m.get_SV()
        g = -m.rho[0]
        a1 = 0
        a2 = 0
        for i in range(0, nsv):
            a1 = a1 + svc[i][0] * 0.5 * sv[i][1]
            a2 = a2 + svc[i][0] * 0.5 * sv[i][2]
            g = g + svc[i][0]

        def f(x):
            return a1 * x[0] + a2 * x[1] + g > 0

        print(a1 / a1, a2 / a1, g / a1)

    sum = 0
    num = 0

    @eventAttr()
    def eventtest(t, y):
        y0, y1 = y
        return a1 * y0 + a2 * y1 + g

    ttest_list, ytest_list = simulation_ode_2(
        [ode_test(G[0], maxorder),
         ode_test(G[1], maxorder)], eventtest, y0, t_tuple, stepsize)
    for i, temp_y in enumerate(y_list):
        y0_list = temp_y.T[0]
        y1_list = temp_y.T[1]
        if i == 0:
            plt.plot(y0_list, y1_list, c='b', label='Original')
        else:
            plt.plot(y0_list, y1_list, c='b')
    for i, temp_y in enumerate(ytest_list):
        y0_list = temp_y.T[0]
        y1_list = temp_y.T[1]
        if i == 0:
            plt.plot(y0_list, y1_list, c='r', label='Inferred')
        else:
            plt.plot(y0_list, y1_list, c='r')
    plt.xlabel('x1')
    plt.ylabel('x2')
    plt.legend()
    plt.show()

    def get_poly_pt(x):
        gene = generate_complete_polynomial(len(x), maxorder)
        val = []
        for i in range(gene.shape[0]):
            val.append(1.0)
            for j in range(gene.shape[1]):
                val[i] = val[i] * (x[j]**gene[i, j])
        poly_pt = np.mat(val)
        return poly_pt

    for ypoints in y_list:
        num = num + ypoints.shape[0]
        for i in range(ypoints.shape[0]):
            if event(0, ypoints[i]) > 0:
                exact = modelist[0](0, ypoints[i])
            else:
                exact = modelist[1](0, ypoints[i])
            if f(ypoints[i]) == 1:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[0].T)
            else:
                predict = np.matmul(get_poly_pt(ypoints[i]), G[1].T)

            exact = np.mat(exact)
            diff = exact - predict
            c = 0
            a = 0
            b = 0
            for j in range(diff.shape[1]):
                c = c + diff[0, j]**2
                a = a + exact[0, j]**2
                b = b + predict[0, j]**2
            f1 = np.sqrt(c)
            f2 = np.sqrt(a) + np.sqrt(b)
            sum = sum + f1 / f2

    return sum / num