Exemplo n.º 1
0
def hpELM(data, target):  # new ELM method for multi output

    # print("This is train data input : ", X_train)
    # print("This is train data output: ", T_train)
    # print("This is test data input: ", X_test)
    # print("This is test data output: ", T_test)

    # print("This is predict value: ", T_pred)
    # print("This is real value: ", T_test)

    # print("This is length of output: ", len(T_pred))
    elmList = []
    for j in range(100):
        X_train, X_test, T_train, T_test = train_test_split(data,
                                                            target,
                                                            test_size=0.2)
        elm = hpelm.ELM(
            data.shape[1],
            target.shape[1])  # number of data features /  number of classes
        elm.add_neurons(5000, "sigm")
        elm.train(X_train, T_train, 'r')
        T_pred = elm.predict(X_test)
        print("This is error: ", elm.error(T_test, T_pred))
        sum_mean = 0
        T_pred_List = []
        T_test_List = []
        for x in np.nditer(T_pred):
            T_pred_List.append(x)
        for y in np.nditer(T_test):
            T_test_List.append(y)
        for i in range(len(T_pred_List)):
            sum_mean += (T_pred_List[i] - T_test_List[i])**2
        sum_erro = (np.sqrt(sum_mean / len(T_pred_List)))
        elmList.append(sum_erro)
    return elmList
Exemplo n.º 2
0
def run():
    X = np.loadtxt("mnist_Xts.txt")
    Y = np.loadtxt("mnist_Yts.txt")
    X = X[:, (X > 0).sum(axis=0) >= 5].copy()  # choose features with at least 5 non-zero entrances
    N, d = X.shape

    V = np.random.rand(N, 2)*2 - 1
    V = (V - V.mean(0)) / V.std(0)

    # build initial ELM
    L = 18
    print "L", L
    elm = hpelm.ELM(2, d)
    elm.add_neurons(2, 'lin')
    elm.add_neurons(L, 'sigm')
    H = elm.project(V)
    A = H.dot(np.linalg.pinv(np.dot(H.T, H))).dot(H.T)

    I, info = elmvis(X, A, slowdown=20)
    print info

    scalarMap = cm.ScalarMappable(norm=colors.Normalize(vmin=0, vmax=10), cmap=plt.get_cmap('gist_rainbow'))
    clr = lambda c: scalarMap.to_rgba(c)

    for i in range(X.shape[0]):
        plt.text(V[i,0], V[i,1], "%d"%Y[I[i]], size=8, color=clr(Y[I[i]]))

    plt.xlim([-2, 2])
    plt.ylim([-2, 2])
    fig = plt.gcf()
    fig.set_size_inches(8, 6)
    plt.savefig("plot_mnist.pdf", bbox_inches="tight")

    plt.show()
def late_fusion_cnn_hog_hist():
    print('1')
    cnn_hog_hist_train = combine_3_feature(cnn_X_train, hog_X_train,
                                           hist_X_train)
    cnn_hog_hist_val = combine_3_feature(cnn_X_val, hog_X_val, hist_X_val)
    cnn_hog_hist_size = cnn_hog_hist_train.shape[1]
    autoencoder = hpelm.ELM(inputs=cnn_hog_hist_size,
                            outputs=cnn_hog_hist_size)
    autoencoder.add_neurons(hidden_num, 'sigm')
    print('start')
    starttime = datetime.datetime.now()
    autoencoder.train(cnn_hog_hist_train, cnn_hog_hist_train)
    endtime = datetime.datetime.now()
    print('time:' + str((endtime - starttime).seconds))

    late_fusion_cnn_hog_hist_train = autoencoder.project(cnn_hog_hist_train)
    late_fusion_cnn_hog_hist_val = autoencoder.project(cnn_hog_hist_val)

    predict = autoencoder.predict(cnn_hog_hist_train)
    print('cnn_hog error ' + str(hidden_num) + ':' +
          str(cal_error(predict, cnn_hog_hist_train)))
    np.save('./data/late_fusion_cnn_hog_hist_train.npy',
            late_fusion_cnn_hog_hist_train)
    np.save('./data/late_fusion_cnn_hog_hist_val.npy',
            late_fusion_cnn_hog_hist_val)
Exemplo n.º 4
0
def classification_accuracy(folder, nn, ntype="sigm", mss="V"):
    folder = os.path.join(os.path.dirname(__file__), folder)
    acc = np.empty((10,))
    for i in range(10):  # 10 random initializations
        # get file names
        Xtr = np.load(os.path.join(folder, "xtrain_%d.npy" % (i + 1)))
        Xts = np.load(os.path.join(folder, "xtest_%d.npy" % (i + 1)))
        Ttr = np.load(os.path.join(folder, "ytrain_%d.npy" % (i + 1)))
        Tts = np.load(os.path.join(folder, "ytest_%d.npy" % (i + 1)))
        # create validation set
        N = Xtr.shape[0]
        ix = np.arange(N)
        np.random.shuffle(ix)
        Xvl = Xtr[ix[:N//5]]
        Tvl = Ttr[ix[:N//5]]
        Xtr = Xtr[ix[N//5:]]
        Ttr = Ttr[ix[N//5:]]
        # train ELM
        elm = hpelm.ELM(Xtr.shape[1], Ttr.shape[1])
        elm.add_neurons(nn, ntype)
        elm.train(Xtr, Ttr, "c", mss, "OP", k=3, kmax=10, Xv=Xvl, Tv=Tvl)
        Yts = elm.predict(Xts)
        # evaluate classification results
        Tts = np.argmax(Tts, 1)
        Yts = np.argmax(Yts, 1)
        acc[i - 1] = float(np.sum(Yts == Tts)) / Tts.shape[0]
    return acc
Exemplo n.º 5
0
def regression_accuracy(folder, nn, mss="V"):
    folder = os.path.join(os.path.dirname(__file__), folder)
    mse = np.empty((10,))
    for i in range(10):  # 10 random initializations
        # get file names
        Xtr = np.load(os.path.join(folder, "xtrain_%d.npy" % (i + 1)))
        Xts = np.load(os.path.join(folder, "xtest_%d.npy" % (i + 1)))
        Ttr = np.load(os.path.join(folder, "ytrain_%d.npy" % (i + 1)))
        Tts = np.load(os.path.join(folder, "ytest_%d.npy" % (i + 1)))
        # create validation set
        N = Xtr.shape[0]
        ix = np.arange(N)
        np.random.shuffle(ix)
        Xvl = Xtr[ix[:N//5]]
        Tvl = Ttr[ix[:N//5]]
        Xtr = Xtr[ix[N//5:]]
        Ttr = Ttr[ix[N//5:]]
        # train ELM
        elm = hpelm.ELM(Xtr.shape[1], Ttr.shape[1])
        elm.add_neurons(nn, "sigm")
        elm.train(Xtr, Ttr, mss, "OP", k=3, Xv=Xvl, Tv=Tvl)
        Yts = elm.predict(Xts)
        # evaluate classification results
        mse[i - 1] = np.mean((Tts - Yts) ** 2)
    return mse
Exemplo n.º 6
0
def runupdates():
    X = np.random.rand(5000, 1000)
    N, d = X.shape

    V = np.random.rand(N, 2) * 2 - 1
    V = (V - V.mean(0)) / V.std(0)

    updates = []
    i = 0
    for N in range(100, 5001, 100):
        elm = hpelm.ELM(2, d)
        elm.add_neurons(2, 'lin')
        elm.add_neurons(50, 'sigm')
        H = elm.project(V[:N])

        A = H.dot(np.linalg.pinv(np.dot(H.T, H))).dot(H.T)

        X1 = X[:N].copy()
        _, info = elmvis_c(X1, A, tol=-100, maxupdate=1000, cossim=1, silent=1)
        ups_c = info['ups']

        X1 = X[:N].copy()
        _, info = elmvis_p(X1, A, tol=-100, maxupdate=1000, cossim=1, silent=1)
        ups_p = info['ups']

        X1 = X[:N].copy()
        _, info = elmvis_p(X1,
                           A,
                           tol=-100,
                           maxupdate=1000,
                           batch=1,
                           cossim=1,
                           silent=1)
        ups_p1 = info['ups']

        X1 = X[:N].copy()
        _, info = elmvis_g(X1, A, tol=-100, maxupdate=1000, cossim=1, silent=1)
        ups_g = info['ups']

        updates.append((ups_c, ups_p, ups_p1, ups_g))
        print "%d/5000" % N

    updates = np.array(updates)
    N = range(100, 5001, 100)
    plt.plot(N, updates[:, 0], '--r', linewidth=3, alpha=0.7)
    plt.plot(N, updates[:, 1], '--k')
    plt.plot(N, updates[:, 2], '-k')
    plt.plot(N, updates[:, 3], '-g')
    plt.ylabel("updates per second")

    plt.legend(("C opt, 1% batch", "Python, 1% batch", "Python", "GPU"))
    plt.title("$N$ data points with 1000 features")
    plt.xlabel("$N$")
    plt.xlim([0, 5000])
    #plt.ylim([0, 23000000])
    #plt.yticks([0, 1000000, 5000000, 10000000, 15000000, 20000000, 22000000], ["0", "1 mln.", "5 mln.", "10 mln.", "15 mln.", "20 mln.", "22 mln."])
    plt.savefig("plot_ups.pdf", bbox_inches="tight")
    print "done"
    plt.show()
Exemplo n.º 7
0
 def test_RBFClassification_Iris_BetterThanNaive(self):
     X = np.loadtxt("iris/iris_data.txt")
     T = np.loadtxt("iris/iris_classes.txt")
     elm = hpelm.ELM(4, 3)
     elm.add_neurons(10, "rbf_l2")
     elm.train(X, T, 'c')
     Y = elm.predict(X)
     err = elm.error(Y, T)
     self.assertLess(err, 0.66)
Exemplo n.º 8
0
 def test_SigmRegression_Sine_BetterThanNaive(self):
     X = np.loadtxt("sine/sine_x.txt")
     T = np.loadtxt("sine/sine_t.txt")
     elm = hpelm.ELM(1, 1)
     elm.add_neurons(10, "sigm")
     elm.train(X, T)
     Y = elm.predict(X)
     err = elm.error(Y, T)
     self.assertLess(err, 1)
    def __init__(self):
        # import model
        filename = "models/elm_alm04_Tx_Tn_Ra_deltaT_EnergyT_HorminTx_Tn_prev_Rs.pkl"
        with open(filename, 'rb') as file:
            self.model = hpelm.ELM(inputs=7, outputs=1)
            self.model.load(filename)

        # define required inputs
        self.parameters = [
            'tx', 'tn', 'ra', 'delta_t', 'energyt', 'hormin_tx', 'tx_prev',
            'rs'
        ]
def late_fusion_hog_hist():
    hog_hist_train = combine_feature(hog_X_train, hist_X_train)
    hog_hist_val = combine_feature(hog_X_val, hist_X_val)
    hog_hist_size = hog_hist_train.shape[1]
    autoencoder = hpelm.ELM(inputs=hog_hist_size, outputs=hog_hist_size)
    autoencoder.add_neurons(hidden_num, 'sigm')
    autoencoder.train(hog_hist_train, hog_hist_train)

    late_fusion_hog_hist_train = autoencoder.project(hog_hist_train)
    late_fusion_hog_hist_val = autoencoder.project(hog_hist_val)

    predict = autoencoder.predict(hog_hist_train)
    print('hog_hist error ' + str(hidden_num) + ':' +
          str(cal_error(predict, hog_hist_train)))
def late_fusion_cnn_hog():
    cnn_hog_train = combine_feature(cnn_X_train, hog_X_train)
    cnn_hog_val = combine_feature(cnn_X_val, hog_X_val)
    cnn_hog_size = cnn_hog_train.shape[1]
    autoencoder = hpelm.ELM(inputs=cnn_hog_size, outputs=cnn_hog_size)
    autoencoder.add_neurons(hidden_num, 'sigm')
    autoencoder.train(cnn_hog_train, cnn_hog_train)

    late_fusion_cnn_hog_train = autoencoder.project(cnn_hog_train)
    late_fusion_cnn_hog_val = autoencoder.project(cnn_hog_val)

    predict = autoencoder.predict(cnn_hog_train)
    print('cnn_hog error ' + str(hidden_num) + ':' +
          str(cal_error(predict, cnn_hog_train)))
    def test_basic_elm_single_machine(self):
        """Just run an ELM with sine function and check training MSE.
        """
        n = 1000
        err = 0.2
        Y = np.linspace(-1, 1, num=n)
        X = np.sin(16 * Y) * Y + np.random.randn(n) * err

        elm = hpelm.ELM(1, 1)
        elm.add_neurons(3, "sigm")
        print(elm.nnet.get_B())
        elm.train(X, Y)
        print(elm.nnet.get_B())
        Yt = elm.predict(X)

        MSE = np.mean((Y - Yt) ** 2)
        self.assertLess(MSE, 0.5)
Exemplo n.º 13
0
def regression_accuracy(folder, nn):
    folder = os.path.join(os.path.dirname(__file__), folder)
    mse = np.empty((10,))
    for i in range(10):  # 10 random initializations
        # get file names
        Xtr = np.load(os.path.join(folder, "xtrain_%d.npy" % (i + 1)))
        Xts = np.load(os.path.join(folder, "xtest_%d.npy" % (i + 1)))
        Ttr = np.load(os.path.join(folder, "ytrain_%d.npy" % (i + 1)))
        Tts = np.load(os.path.join(folder, "ytest_%d.npy" % (i + 1)))
        # train ELM
        elm = hpelm.ELM(Xtr.shape[1], Ttr.shape[1])
        elm.add_neurons(nn, "sigm")
        elm.train(Xtr, Ttr, "CV", k=5)
        Yts = elm.predict(Xts)
        # evaluate classification results
        mse[i - 1] = np.mean((Tts - Yts) ** 2)
    return mse
Exemplo n.º 14
0
def classification_conf(folder, nn, ntype="sigm", b=1):
    folder = os.path.join(os.path.dirname(__file__), folder)
    i = np.random.randint(0, 10)
    print "using init number: ", i
    # get file names
    Xtr = np.load(os.path.join(folder, "xtrain_%d.npy" % (i + 1)))
    Xts = np.load(os.path.join(folder, "xtest_%d.npy" % (i + 1)))
    Ttr = np.load(os.path.join(folder, "ytrain_%d.npy" % (i + 1)))
    Tts = np.load(os.path.join(folder, "ytest_%d.npy" % (i + 1)))
    # train ELM
    Bsize = Xtr.shape[0] / b + 1  # batch size larger than amount of data
    elm = hpelm.ELM(Xtr.shape[1], Ttr.shape[1], batch=Bsize)
    elm.add_neurons(nn, ntype)
    elm.train(Xtr, Ttr, 'c')
    Yts = elm.predict(Xts)
    conf = elm.confusion(Tts, Yts)
    return conf
Exemplo n.º 15
0
def test_adapt_elm_feature_fusion(features, Y):
    result = []
    for i in range(3):
        weights = np.load('./data/adapt_elm/weight_' + str(i) + '.npy')
        for k in range(K):
            elm = hpelm.ELM(inputs=input_sizes[i], outputs=8)
            elm.load('./data/adapt_elm/elm_' + str(i) + '_' + str(k))
            predict = elm.predict(features[i]) * weights[k]
            if i == 0 and k == 0:
                result = predict
            else:
                result = result + predict
    counter = 0.0
    for r, t in zip(result, Y):
        if np.argmax(r) == np.argmax(t):
            counter += 1
    right_rate = counter / features[0].shape[0]
    print('adapt elm right rate:' + str(right_rate))
Exemplo n.º 16
0
def accuracy_mc_cv(folder, nn, ntype="sigm"):
    folder = os.path.join(os.path.dirname(__file__), folder)
    acc = np.empty((10, ))
    for i in range(10):  # 10 random initializations
        # get file names
        Xtr = np.load(os.path.join(folder, "xtrain_%d.npy" % (i + 1)))
        Xts = np.load(os.path.join(folder, "xtest_%d.npy" % (i + 1)))
        Ttr = np.load(os.path.join(folder, "ytrain_%d.npy" % (i + 1)))
        Tts = np.load(os.path.join(folder, "ytest_%d.npy" % (i + 1)))
        # train ELM
        elm = hpelm.ELM(Xtr.shape[1], Ttr.shape[1])
        elm.add_neurons(nn, ntype)
        elm.train(Xtr, Ttr, "mc", "CV", k=5)
        Yts = elm.predict(Xts)
        # evaluate classification results
        Tts = np.argmax(Tts, 1)
        Yts = np.argmax(Yts, 1)
        acc[i - 1] = float(np.sum(Yts == Tts)) / Tts.shape[0]
    return acc
Exemplo n.º 17
0
def elm(folder, i, nn, param):
    #    folder = os.path.join(os.path.dirname(__file__), folder)
    #    acc = np.empty((10, 3))

    # get file names
    Xtr = np.load(os.path.join(folder, "xtrain_%d.npy" % (i + 1)))
    Xts = np.load(os.path.join(folder, "xtest_%d.npy" % (i + 1)))
    Ttr = np.load(os.path.join(folder, "ytrain_%d.npy" % (i + 1)))
    Tts = np.load(os.path.join(folder, "ytest_%d.npy" % (i + 1)))

    # create validation set
    #    N = Xtr.shape[0]
    #    ix = np.arange(N)
    #    np.random.shuffle(ix)
    #    Xvl = Xtr[ix[:N/5]]
    #    Tvl = Ttr[ix[:N/5]]
    #    Xtr = Xtr[ix[N/5:]]
    #    Ttr = Ttr[ix[N/5:]]

    #    elm.add_neurons(Xtr.shape[1], "lin")
    #    W, B = hpelm.modules.rbf_param(Xtr, nn, "l2")
    #    elm.add_neurons(nn, "rbf_l2", W, B)

    nn = min(nn, Xtr.shape[0] / 2)

    t = time()
    # build ELM
    elm = hpelm.ELM(Xtr.shape[1], Ttr.shape[1])
    elm.add_neurons(nn, "sigm")
    # train ELM
    elm.train(Xtr, Ttr, *param)
    Yts = elm.predict(Xts)
    err = elm.error(Yts, Tts)
    t = time() - t

    nns = [l[1] for l in elm.neurons]
    return err, nns, t
 def test_xor_one_neuron_solved(self):
     """ELM should be able to solve XOR problem.
     """
     X = np.array([[0, 0],
                   [1, 1],
                   [1, 0],
                   [0, 1]])
     Y = np.array([1, 1, -1, -1])
     for _ in range(100):  # try 100 random initializations to solve XOR problem
         try:
             elm = hpelm.ELM(2, 1)
             elm.add_neurons(1, "sigm")
             elm.train(X, Y)
             Yh = elm.predict(X)
             self.assertGreater(Yh[0], 0)
             self.assertGreater(Yh[1], 0)
             self.assertLess(Y[2], 0)
             self.assertLess(Y[3], 0)
             nn = sum([n[0] for n in elm.nnet.neurons])
             self.assertEqual(nn, 1)  # one neuron in the ELM
             return
         except AssertionError:
             pass
     self.fail("Cannot train 1 neuron to solve XOR problem in 100 re-initializations")
Exemplo n.º 19
0
    def __init__(self,
                 model,
                 xtrain_shape=7,
                 ytrain_shape=1,
                 hidden_layer_neurons=5,
                 activation='sigm',
                 max_iter=200,
                 normalize=False,
                 fit_intercept=True,
                 alpha=1.0,
                 solver='auto',
                 W=None,
                 pca=None):
        self.model_type = model

        if model in ['ELM']:
            self.model = hpelm.ELM(xtrain_shape, ytrain_shape)
            self.model.add_neurons(hidden_layer_neurons, activation)
        elif model in ['MLR']:
            self.model = LinearRegression(fit_intercept=fit_intercept)
        elif model in ['EM']:
            self.model = EnsembleMean()
        else:
            print('invalid model type {}'.format(model))
mae = mean_absolute_error(Y1, r1)
fpr, tpr, thresholds = metrics.roc_curve(Y1, r1,pos_label=2)
auc = metrics.auc(fpr, tpr)
print(metrics.classification_report(Y1, r1))
print(metrics.confusion_matrix(Y1, r1))
print("\n")

result.write("%1.5f" % accuracy + "\t%1.3f" % precision + "\t%1.3f"%recall + "\t%1.3f" % f1 +"\t%1.5f" % mse +"\t%1.5f" % mae +"\t%1.5f" % auc)
result.write("\n")

np.savetxt('data/result/sigmultiother.txt', (fpr,tpr,thresholds), fmt='%10.3f')



print "sigmoid with MSE"
elm = hpelm.ELM(41, 23)
elm.add_neurons(10, "sigm")
elm.train(X, Y)
r2 = elm.predict(X1)
print(str(elm))


result1.write(str(elm))
result1.write("\n")

r2=r2.argmax(1)
accuracy = accuracy_score(Y1, r2)
print(accuracy)
recall = recall_score(Y1, r2, average="weighted")
precision = precision_score(Y1, r2 , average="weighted")
f1 = f1_score(Y1, r2 , average="weighted")
Exemplo n.º 21
0
def runswaps():
    X = np.random.rand(1000, 10000)
    N, d = X.shape

    V = np.random.rand(N, 2) * 2 - 1
    V = (V - V.mean(0)) / V.std(0)

    swaps = []
    i = 0
    for d in range(20, 1001, 20):
        elm = hpelm.ELM(2, d)
        elm.add_neurons(2, 'lin')
        elm.add_neurons(20, 'sigm')
        H = elm.project(V)

        A = H.dot(np.linalg.pinv(np.dot(H.T, H))).dot(H.T)

        X1 = X[:, :d].copy()
        _, info = elmvis_c(X1,
                           A,
                           tol=100,
                           maxiter=10000,
                           maxstall=10000,
                           silent=1)
        ips_c = info['ips']

        X1 = X[:, :d].copy()
        _, info = elmvis_p(X1,
                           A,
                           tol=100,
                           maxiter=10000,
                           maxstall=10000,
                           silent=1)
        ips_p = info['ips']

        X1 = X[:, :d].copy()
        _, info = elmvis_g(X1,
                           A,
                           tol=100,
                           maxiter=10000,
                           maxstall=10000,
                           silent=1)
        ips_g = info['ips']

        swaps.append((ips_c, ips_p, ips_g))
        print d

    swaps = np.array(swaps)
    d = range(20, 1001, 20)
    plt.plot(d, swaps[:, 0], '--r', linewidth=3, alpha=0.7)
    plt.plot(d, swaps[:, 1], '--k')
    plt.plot(d, swaps[:, 2], '--g')
    plt.ylabel("swaps per second")

    plt.legend(("C optimized", "Python", "GPU"))
    plt.title("1000 data points with $d$ features")
    plt.xlabel("$d$")
    plt.xlim([0, 1000])
    #plt.ylim([0, 23000000])
    #plt.yticks([0, 1000000, 5000000, 10000000, 15000000, 20000000, 22000000], ["0", "1 mln.", "5 mln.", "10 mln.", "15 mln.", "20 mln.", "22 mln."])
    plt.savefig("plot_ips.pdf", bbox_inches="tight")
    print "done"
    plt.show()
pY = os.path.join(curdir, "../dataset_tests/iris/maltrte/class.txt")
pT = os.path.join(curdir, "../dataset_tests/iris/maltrte/test.txt")

X = np.loadtxt(pX)
Y = np.loadtxt(pY)
T = np.loadtxt(pT)
print(type(X))

print "sigmoid with multi class error"
elm = ELM(1804, 9)
elm.add_neurons(150, "sigm")
elm.train(X, Y, "c")
Yh = elm.predict(X)

print "sigmoid with MSE"
elm = hpelm.ELM(1804, 9)
elm.add_neurons(150, "sigm")
elm.train(X, Y)
Y1 = elm.predict(X)
err = elm.error(Y1, Y)
print err

print "rbf_12 with multi class error"
elm = hpelm.ELM(1804, 9)
elm.add_neurons(150, "rbf_l2")
elm.train(X, Y, 'c')
Y1 = elm.predict(X)
err = elm.error(Y1, Y)
print err

print "rbf_11 with multi class error"
Exemplo n.º 23
0
import hpelm
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

df = pd.read_csv('BELM/sample_data.csv', header=None)
output = np.array(df[df.columns[-1]])
data = np.array(df.iloc[:, :-1])

X_train, X_test, Y_train, Y_test = train_test_split(data,
                                                    output,
                                                    test_size=0.3)
Y_train = Y_train.reshape(-1, 1)

elm_gpu = hpelm.ELM(X_train.shape[1],
                    Y_train.shape[1],
                    precision="single",
                    accelerator="GPU")
elm_gpu.add_neurons(10, 'sigm')
elm_gpu.train(X_train, Y_train)
P = elm_gpu.predict(X_test)
e = elm_gpu.error(Y_test, P)
print("Error:  ", e)
Exemplo n.º 24
0
 def __init__(self, isEmpty, sequenceLength, numClasses, dimension,
              numHiddenNodes):
     self.model = hpelm.ELM(inputs=sequenceLength * dimension,
                            outputs=numClasses,
                            classification="c")
     self.model.add_neurons(number=numHiddenNodes, func="tanh")
recall = recall_score(Y1, r1, average="weighted")
precision = precision_score(Y1, r1 , average="weighted")
f1 = f1_score(Y1, r1 , average="weighted")
mse = mean_squared_error(Y1, r1)
mae = mean_absolute_error(Y1, r1)


result.write("%1.5f" % accuracy + "\t%1.3f" % precision + "\t%1.3f"%recall + "\t%1.3f" % f1 +"\t%1.5f" % mse +"\t%1.5f" % mae)
result.write("\n")

np.savetxt('data/result/sigmultiother.txt', (fpr,tpr,thresholds), fmt='%10.3f')



print "sigmoid with MSE"
elm = hpelm.ELM(41, 41)
elm.add_neurons(5, "sigm")
elm.train(X, Y)
r2 = elm.predict(X1)
result1.write(str(elm))
result1.write("\n")

r2=r2.max(1)
accuracy = accuracy_score(Y1, r2)
recall = recall_score(Y1, r2, average="weighted")
precision = precision_score(Y1, r2 , average="weighted")
f1 = f1_score(Y1, r2 , average="weighted")
mse = mean_squared_error(Y1, r2)
mae = mean_absolute_error(Y1, r2)
fpr, tpr, thresholds = metrics.roc_curve(Y1, r2,pos_label=9)
auc = metrics.auc(fpr, tpr)
f1 = f1_score(Y1, r1, average="weighted")
mse = mean_squared_error(Y1, r1)
mae = mean_absolute_error(Y1, r1)
fpr, tpr, thresholds = metrics.roc_curve(Y1, r1, pos_label=9)
auc = metrics.auc(fpr, tpr)

result.write("%1.5f" % accuracy + "\t%1.3f" % precision + "\t%1.3f" % recall +
             "\t%1.3f" % f1 + "\t%1.5f" % mse + "\t%1.5f" % mae +
             "\t%1.5f" % auc)
result.write("\n")

np.savetxt('data/result/sigmultiother.txt', (fpr, tpr, thresholds),
           fmt='%10.3f')

print "sigmoid with MSE"
elm = hpelm.ELM(243, 8)
elm.add_neurons(40, "sigm")
elm.train(X, Y)
r2 = elm.predict(X1)
result1.write(str(elm))
result1.write("\n")

r2 = r2.argmax(1)
accuracy = accuracy_score(Y1, r2)
recall = recall_score(Y1, r2, average="weighted")
precision = precision_score(Y1, r2, average="weighted")
f1 = f1_score(Y1, r2, average="weighted")
mse = mean_squared_error(Y1, r2)
mae = mean_absolute_error(Y1, r2)
fpr, tpr, thresholds = metrics.roc_curve(Y1, r2, pos_label=9)
auc = metrics.auc(fpr, tpr)
mae = mean_absolute_error(Y1, r1)
fpr, tpr, thresholds = metrics.roc_curve(Y1, r1,pos_label=2)
auc = metrics.auc(fpr, tpr)
print(recall)
print(metrics.classification_report(Y1, r1))
print(metrics.confusion_matrix(Y1, r1))
print("\n")

result.write("%1.5f" % accuracy + "\t%1.3f" % precision + "\t%1.3f"%recall + "\t%1.3f" % f1 +"\t%1.5f" % mse +"\t%1.5f" % mae +"\t%1.5f" % auc)
result.write("\n")

np.savetxt('data/result1/sigmultiother.txt', (fpr,tpr,thresholds), fmt='%10.3f')


print "sigmoid with MSE"
elm = hpelm.ELM(42, 2)
elm.add_neurons(32, "sigm")
elm.train(X, Y)
r2 = elm.predict(X)
print(str(elm))


result1.write(str(elm))
result1.write("\n")

r2=r2.argmax(1)
accuracy = accuracy_score(Y1, r2)
print(accuracy)
recall = recall_score(Y1, r2, average="weighted")
precision = precision_score(Y1, r2 , average="weighted")
f1 = f1_score(Y1, r2 , average="weighted")
Exemplo n.º 28
0
#X = np.genfromtxt(pX, dtype= None,delimiter=" ")
X = np.loadtxt(pX)
Y = np.loadtxt(pY)
print(type(X))

print "sigmoid with multi class error"
elm = ELM(1804, 10)
elm.add_neurons(150, "sigm")
elm.train(X, Y, "c")
Yh = elm.predict(X)
print Yh
acc = float(np.sum(Y.argmax(1) == Yh.argmax(1))) / Y.shape[0]
print "malware dataset training error: %.1f%%" % (100 - acc * 100)

print "sigmoid with MSE"
elm = hpelm.ELM(1804, 10)
elm.add_neurons(150, "sigm")
elm.train(X, Y)
Y1 = elm.predict(X)
err = elm.error(Y1, Y)
print err

print "rbf_12 with multi class error"
elm = hpelm.ELM(1804, 10)
elm.add_neurons(150, "rbf_l2")
elm.train(X, Y, 'c')
Y1 = elm.predict(X)
err = elm.error(Y1, Y)
print err

print "rbf_11 with multi class error"