Exemplo n.º 1
0
def test_distance_calculation():
    # Start afresh again
    # Let us now test how our distance behaves
    # Get the first sample
    N = extract_samples(X, y, 1)
    Samp_Size = 10000
    rand = [numpy.random.choice(N.shape[0], Samp_Size, replace = True) for i in \
    xrange(Samp_Size)]
    N = N[rand, :]
    scaler = preprocessing.StandardScaler().fit(N)
    N_transform = scaler.transform(N)
    Ref, Tree = initialize_calculation(T = None, Data = N_transform, gsize = 3, \
    par_train = 0)

    # Get the other sample
    T = extract_samples(X, y, 4)
    rand = [numpy.random.choice(N.shape[0], Samp_Size, replace = True) for i in \
    xrange(Samp_Size)]
    T = T[rand, :]
    T_transform = scaler.transform(T)
    Test, Tree = initialize_calculation(T = Tree, Data = T_transform, gsize = 3,\
    par_train = 1)
    Tmp = traditional_MTS(Ref, Test, par=0)

    import matplotlib.pyplot as plt
    plt.plot(Tmp)
    plt.show()
Exemplo n.º 2
0
def distance_Iteration():
    ## Class wise distance evaluation
    from Library_Paper_one import traditional_MTS
    # # Loop to evaluate distance value for all the classes.
    for p in tqdm(xrange(1, C + 1)):
        N = np.loadtxt(path_store + 'C' + str(p) + dataset_type + '.csv',
                       delimiter=',')
        scaler = preprocessing.StandardScaler().fit(N)
        N_transform = scaler.transform(N)
        Ref, Tree = initialize_calculation(T = None, Data = N_transform, gsize = 3,\
         par_train = 0)

        T = np.loadtxt(path_store + 'C' + str(p) + dataset_type + '.csv',
                       delimiter=',')
        T_transform = scaler.transform(T)
        Test, Tree = initialize_calculation(T = Tree, Data = T_transform, gsize = 3,\
        par_train = 1)

        Data = []
        for i in tqdm(xrange(T_iterations)):
            rand = np.random.choice(Test.shape[0], Samp_Size, replace=True)
            T = Test[rand, :]
            Tmp = traditional_MTS(Ref, T, par=0)
            Data.append(Tmp.mean())

        np.savetxt((path_store + 'R' + str(p) + dataset_type + '.csv'),
                   np.reshape(np.array(Data), [
                       -1,
                   ]),
                   delimiter=',')
def distance_Iteration():
    ## Class wise distance evaluation
    from Library_Paper_one import traditional_MTS

    # # Loop to evaluate distance value for all the classes.
    for p in tqdm(xrange(1, C + 1)):
        N = extract_samples(X, y, p)
        np.savetxt(("../" + 'C' + str(p) + dataset_type + '.csv'),
                   np.array(N),
                   delimiter=',')
        scaler = preprocessing.StandardScaler().fit(N)
        N_transform = scaler.transform(N)

        Ref, Tree = initialize_calculation( T = None, Data = N_transform, gsize = 2,\
        par_train = 0, output_dimension = 4 )
        Data = []
        np.savetxt(("../" + 'C_dimred' + str(p) + dataset_type + '.csv'),
                   np.array(Ref),
                   delimiter=',')

        for i in tqdm(xrange(T_iterations)):
            T = np.array(subsample(N_transform, ratio=0.06))
            T_dim, Tree = Test, Tree = initialize_calculation(T = Tree, Data = T, gsize = 2,\
            par_train = 1, output_dimension = 4)
            Tmp = traditional_MTS(Ref, T_dim, par=0)
            Data.append(Tmp.mean())
        np.savetxt(("../" + 'R' + str(p) + dataset_type + '.csv'),
                   np.reshape(np.array(Data), [
                       -1,
                   ]),
                   delimiter=',')
Exemplo n.º 4
0
def Classification_Faults_Bearing(N, T):
    p_value_0 = []
    p_value_1 = []
    p_value_2 = []
    p_value_3 = []
    for ola in xrange(4):
        print "ola --", ola

        Test = np.array(T[ola].copy())
        print "Shape -- ", Test.shape

        D_M = np.zeros((Test.shape[0], 4))
        print "D_M shape", D_M.shape

        D_M[:, 0] = traditional_MTS(N[0], Test, 0).reshape((Test.shape[0], ))
        D_M[:, 1] = traditional_MTS(N[1], Test, 0).reshape((Test.shape[0], ))
        D_M[:, 2] = traditional_MTS(N[2], Test, 0).reshape((Test.shape[0], ))
        D_M[:, 3] = traditional_MTS(N[3], Test, 0).reshape((Test.shape[0], ))

        Label = fisher(D_M, N[0].shape[0], N[1].shape[0], N[2].shape[0],
                       N[3].shape[0])

        c = [0, 0, 0, 0]
        c[0] = list(Label).count(0)
        c[1] = list(Label).count(1)
        c[2] = list(Label).count(2)
        c[3] = list(Label).count(3)
        print "Counted detection", "(", c[0], ",", c[1], ",", c[2], ",", c[
            3], ")"
        if ola == 0:
            p_value_0.append(
                (c[1] + c[2] + c[3]) / float(c[0] + c[1] + c[2] + c[3]))
        if ola == 1:
            p_value_1.append(
                (c[0] + c[2] + c[3]) / float(c[0] + c[1] + c[2] + c[3]))
        if ola == 2:
            p_value_2.append(
                (c[1] + c[0] + c[3]) / float(c[0] + c[1] + c[2] + c[3]))
        if ola == 3:
            p_value_3.append(
                (c[1] + c[2] + c[0]) / float(c[0] + c[1] + c[2] + c[3]))
    P = []
    P.append(sum(p_value_0) / float(len(p_value_0)))
    P.append(sum(p_value_1) / float(len(p_value_1)))
    P.append(sum(p_value_2) / float(len(p_value_2)))
    P.append(sum(p_value_3) / float(len(p_value_3)))
    return P
Exemplo n.º 5
0
def classification_dimred(N, N1, Test, Y):

    D_M = np.zeros((Test.shape[0], 2))
    D_M[:, 0] = np.resize(np.array(traditional_MTS(Test, N)),
                          (Test.shape[0], ))
    D_M[:, 1] = np.resize(np.array(traditional_MTS(Test, N1)),
                          (Test.shape[0], ))
    M_1 = N.shape[0]
    M_2 = N1.shape[0]
    print N.shape
    print N1.shape
    print Test.shape
    # First let us decide on the prior probabilities
    pi1 = 1 / float(2)
    pi2 = 1 / float(2)
    for i in range(0, 1):
        Prob_label = []
        i = 0
        for element in D_M:
            P_1 = ((1 + (1 / float(M_1))) * pi1 * exp(-0.5 * element[0])) / (
                ((1 + (1 / float(M_1))) * pi1 * exp(-0.5 * element[0])) +
                ((1 + (1 / float(M_2))) * pi2 * exp(-0.5 * element[1])))
            P_2 = ((1 + (1 / float(M_2))) * pi2 * exp(-0.5 * element[1])) / (
                ((1 + (1 / float(M_1))) * pi1 * exp(-0.5 * element[0])) +
                ((1 + (1 / float(M_2))) * pi2 * exp(-0.5 * element[1])))

            lab_max = max(P_1, P_2)
            if lab_max == P_1:
                Prob_label.append(0)
            else:
                Prob_label.append(1)

        P = np.resize(np.array(Prob_label), (len(Prob_label), ))
        #print Prob_label
        c = Counter(Prob_label)
        pi1 = ((c[0]) / float(c[0] + c[1]))
        pi2 = ((c[1]) / float(c[0] + c[1]))
    print("The Counter of the labels are", Counter(P))
    print("The Counter of the labels are", Counter(Y))
    c1 = Counter(Y)
    Corr = P[0:c1[0]]
    TE = Y[0:c1[0]]
    c = Counter(Corr)
    print(c[0] / float(c1[0])) * 100
Exemplo n.º 6
0
def TypeOneError(Ref, Data):
    p_values = []
    detect = traditional_MTS(Ref, np.array(Data), 0)
    # print "The mean of the MD values are", detect.mean()
    p = 4.615
    index1 = list([i for i, v in enumerate(detect) if v < p])
    p = 6.42
    index2 = list([i for i, v in enumerate(detect) if v < p])
    p = 9.26
    index3 = list([i for i, v in enumerate(detect) if v < p])
    p_values.append((len(index1)) / float(len(Data)))
    p_values.append((len(index2)) / float(len(Data)))
    p_values.append((len(index3)) / float(len(Data)))
    return p_values
Exemplo n.º 7
0
def TypeTwoError(Ref, Data):
    detect = traditional_MTS(Ref, np.array(Data), 0)
    p = 4.615
    index1 = list([i for i, v in enumerate(detect) if v < p])
    p = 6.42
    index2 = list([i for i, v in enumerate(detect) if v < p])
    p = 9.26
    index3 = list([i for i, v in enumerate(detect) if v < p])
    p_values = []
    pow_values = []
    p_values.append((len(index1)) / float(len(Data)))
    p_values.append((len(index2)) / float(len(Data)))
    p_values.append((len(index3)) / float(len(Data)))
    pow_values.append(1 - ((len(index1)) / float(len(Data))))
    pow_values.append(1 - ((len(index2)) / float(len(Data))))
    pow_values.append(1 - ((len(index3)) / float(len(Data))))
    return p_values, pow_values
Exemplo n.º 8
0
def test_data_samples(Ref, dat):
    t = time.time()
    CF = Classification_Distances(Ref, data, 0, 111)
    print "The time for GDM-HDR is", (time.time() - t)
    print "The length of the values are", len(CF)
    count = 0
    for element in CF:
        if element < 10:
            count = count + 1
    print count
    print "The mean of the distance values are", CF.mean()
    t = time.time()
    CF = []
    CF = traditional_MTS(Ref, data, 0)
    print "The time fo MD is", (time.time() - t)
    print "The mean of the distance values are", CF.mean()
    count = 0
    for element in CF:
        if element < 10:
            count = count + 1
    print count
Exemplo n.º 9
0
    return C

from scipy import stats
# Gather the normal and the test samples from the data
N = extract_samples(train_dataset, train_labels, 1);
T = extract_samples(train_dataset, train_labels, 4);

temp_scalar = preprocessing.StandardScaler(with_mean = True, with_std = True).fit(N)
N = temp_scalar.transform(N)
T = temp_scalar.transform(T)
import time
start = time.time()


# # 1 -- Dimension reduction
# Ref, Tree = initialize_calculation(T = None, Data = Xtr_s, gsize = 2,\
# par_train = 0, output_dimension = 4)
# # Test, Tree = initialize_calculation(T = Tree, Data = Xte_s, gsize = 2,\
# # par_train = 1, output_dimension = 4)

# 2 - Second type of dimension reduction
from distanceHDR import dim_reduction, dim_reduction_test
start = time.time()
Level, Ref = dim_reduction(N, i_dim=N.shape[1], o_dim=5, g_size=2)
# print(stats.describe(Ref))
Test = dim_reduction_test(T, Level, i_dim=T.shape[1], o_dim=5, g_size=2)
print("The time elapsed is", time.time()-start)
print("\nRef", Ref.shape, "Test", Test.shape)
print("\nref-ref", traditional_MTS(Ref, Ref, 0).mean())
print("\nref-test", traditional_MTS(Ref, Test, 0).mean())