def model_elm(XX, TT, XX_test, TT_test, model_type):
    '''
	Builda elm model using hpelm package

	Arguments:
	XX -- randomized and normalized training X-values, numpy array of shape (# compositions, # molar%)
	TT -- randomized and normalized training Y-values, numpy array of shape (Fragility value, 1)
	XX_test -- randomized and normalized testing X-values, numpy array of shape (# compositions, # molar%)
	TT_test -- randomized and normalized testing Y-values, numpy array of shape (Fragility value, 1)

	Returns:
	model -- save model in ELM format
	'''

    # Hyperparameters
    k = 5  # Use this if model_type == CV
    np.random.seed(10)

    # Build hpelm model
    # ELM(inputs, outputs, classification='', w=None, batch=1000, accelerator=None, precision='double', norm=None, tprint=5)
    model = ELM(20, 1, tprint=5)

    # Add neurons
    model.add_neurons(7, 'tanh')  # Number of neurons with tanh activation
    model.add_neurons(7, 'lin')  # Number of neurons with linear activation

    # if then condition for types of training
    if (model_type == 'CV'):
        print('-' * 10 + 'Training with Cross-Validation' + '-' * 10)
        model.train(XX, TT, 'CV', k=k)  # Train the model with cross-validation
    elif (model_type == 'LOO'):
        print('-' * 10 + 'Training with Leave-One-Out' + '-' * 10)
        model.train(XX, TT, 'LOO')  # Train the model with Leave-One-Out
    else:
        print('-' * 10 + 'Training with regression' + '-' * 10)
        model.train(XX, TT, 'r')  # Train the model with regression

    # Train ELM models
    TTH = model.predict(XX)  # Calculate training error
    YY_test = model.predict(XX_test)  # Calculate testing error
    print('Model Training Error: ', model.error(TT,
                                                TTH))  # Print training error
    print('Model Test Error: ', model.error(YY_test,
                                            TT_test))  # Print testing error
    print(str(model))  # Print model information
    print('-' * 50)

    # Call plot function
    my_plot(TT_test, YY_test)

    return model
Exemple #2
0
 def test_MultilabelError_CorrectWithMultipleClasses(self):
     T = np.zeros((100, 5))
     T[:, 0] = 1
     Y = np.zeros((100, 5))
     Y[:, 1] = 1
     model = ELM(1, 5, classification='ml')
     self.assertEqual(0.4, model.error(T, Y))
 def test_RegressionError_Works(self):
     T = np.array([1, 2, 3])
     Y = np.array([1.1, 2.2, 3.3])
     err1 = np.mean((T - Y) ** 2)
     elm = ELM(1, 1)
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, err1)
 def test_RegressionError_Works(self):
     T = np.array([1, 2, 3])
     Y = np.array([1.1, 2.2, 3.3])
     err1 = np.mean((T - Y)**2)
     elm = ELM(1, 1)
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, err1)
Exemple #5
0
 def test_MultilabelError_CorrectWithMultipleClasses(self):
     T = np.zeros((100, 5))
     T[:, 0] = 1
     Y = np.zeros((100, 5))
     Y[:, 1] = 1
     model = ELM(1, 5, classification='ml')
     self.assertEqual(0.4, model.error(T, Y))
 def test_MultiLabelClassError_Works(self):
     X = np.array([1, 2, 3])
     T = np.array([[0, 1], [1, 1], [1, 0]])
     Y = np.array([[0.4, 0.6], [0.8, 0.6], [1, 1]])
     elm = ELM(1, 2, classification="ml")
     elm.add_neurons(1, "lin")
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, 1.0 / 6)
 def test_MultiLabelClassError_Works(self):
     X = np.array([1, 2, 3])
     T = np.array([[0, 1], [1, 1], [1, 0]])
     Y = np.array([[0.4, 0.6], [0.8, 0.6], [1, 1]])
     elm = ELM(1, 2, classification="ml")
     elm.add_neurons(1, "lin")
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, 1.0 / 6)
 def test_WeightedClassError_Works(self):
     T = np.array([[0, 1], [0, 1], [1, 0]])
     Y = np.array([[0, 1], [0.4, 0.6], [0, 1]])
     # here class 0 is totally incorrect, and class 1 is totally correct
     w = (9, 1)
     elm = ELM(1, 2, classification="wc", w=w)
     elm.add_neurons(1, "lin")
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, 0.9)
 def test_WeightedClassError_Works(self):
     T = np.array([[0, 1], [0, 1], [1, 0]])
     Y = np.array([[0, 1], [0.4, 0.6], [0, 1]])
     # here class 0 is totally incorrect, and class 1 is totally correct
     w = (9, 1)
     elm = ELM(1, 2, classification="wc", w=w)
     elm.add_neurons(1, "lin")
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, 0.9)
Exemple #10
0
def build_ELM_encoder(xinput, target, num_neurons):

    elm = ELM(xinput.shape[1], target.shape[1])
    elm.add_neurons(num_neurons, "sigm")
    elm.add_neurons(num_neurons, "lin")
    #elm.add_neurons(num_neurons, "rbf_l1")
    elm.train(xinput, target, "r")
    ypred = elm.predict(xinput)
    print "mse error", elm.error(ypred, target)
    return elm, ypred
Exemple #11
0
def build_ELM_encoder(xinput, target, num_neurons):


    elm = ELM(xinput.shape[1], target.shape[1])
    elm.add_neurons(num_neurons, "sigm")
    elm.add_neurons(num_neurons, "lin")
    #elm.add_neurons(num_neurons, "rbf_l1")
    elm.train(xinput, target, "r")
    ypred = elm.predict(xinput)
    print "mse error", elm.error(ypred, target)
    return elm, ypred
Exemple #12
0
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"
elm = hpelm.ELM(1804, 10)
elm.add_neurons(150, "rbf_l1")
elm.train(X, Y, 'c')
Y1 = elm.predict(X)
        elmHiddenProjectionNormed), "Min : ", np.min(elmHiddenProjectionNormed)

    # OUTPUT LAYER
    elmOutput = ELM(elmHiddenProjectionNormed.shape[1], YY.shape[1])
    elmOutput.add_neurons(ninputsig, "tanh")
    elmOutput.add_neurons(ninputlin, "lin")

    # train and get result
    elmOutput.train(elmHiddenProjectionNormed, YYtrain, "c")
    # now prediction for trainining
    youtput = elmOutput.predict(elmHiddenProjectionNormed)
    print "\n Trained output elm", elmOutput
    print " Output Projection Max :", np.max(youtput), "Min :", np.min(youtput)

    # training results
    mse_error = elmOutput.error(youtput, YYtrain)
    print "Training mse:", mse_error
    p = ytrain.squeeze()
    yout = np.argmax(youtput, axis=1)
    nhit = sum(yout == p)
    ntpos = sum((yout == 1) & (p == 1))
    npos = sum((p == 1))
    print "\n Training results"
    print "Tpos:", ntpos, " / ", npos, "TD:", ntpos / float(npos)
    print "Acc: ", nhit / (float)(len(p)), "total", len(p)

    # now prediction for testing set
    prjinput = elmInput.project(XXtest)
    prjinput_normed = mapMinMax(prjinput)
    prjhidden = elmHidden1.project(prjinput_normed)
    prjhidden_normed = mapMinMax(prjhidden)
Exemple #14
0
def hpElM(data, target, iterNum, isNormal, isRegression, isPCA, n_components,
          normalMethod, testSize):
    print("ELM is running")
    y = target
    elmList = []
    # neuronsNum = [20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 200]
    neuronsNum = [5, 10, 20, 30, 40, 50, 75, 200]
    # neuronsNum = [5]

    if normalMethod == 1:
        sc = preprocessing.Normalizer()
    elif normalMethod == 2:
        sc = preprocessing.StandardScaler()
    elif normalMethod == 3:
        sc = preprocessing.MinMaxScaler()
    for j in range(iterNum):
        errorList = []
        X_train, X_test, y_train, y_test = train_test_split(data,
                                                            y,
                                                            test_size=testSize)

        sc.fit(X_train)
        X_train_std = sc.transform(X_train)
        X_test_std = sc.transform(X_test)
        if isPCA:
            X_train, X_test = reduceDim.featureExtraction(
                X_train_std, X_test_std, n_components)
            # print("This is the size of input by using PCA: ", len(X_train[0]))
        else:
            print("Not use PCA...", )
            X_train = X_train_std
            X_test = X_test_std
        # print("This is : ", X_train)
        # print("This is : ", y_train.values)

        for neuron in neuronsNum:
            elm1 = ELM(len(X_train[1]), y.shape[1])
            elm1.add_neurons(neuron, 'sigm')
            # elm1.add_neurons(neuron, 'tanh')
            # elm1.add_neurons(neuron,'rbf_l2')
            elm1.train(X_train, y_train.values, 'CV', 'OP', 'r', k=3)
            y_pred_temp = elm1.predict(X_test)
            errorPara = elm1.error(y_pred_temp, y_test.values)
            errorList.append(errorPara)
        print("This is error list: ", errorList)
        bestPos = errorList.index(min(errorList))
        bestPara = neuronsNum[bestPos]
        print("This is the best number of neuron: ", bestPara)

        elm = ELM(len(X_train[1]), y.shape[1])
        elm.add_neurons(bestPara, 'sigm')
        # elm.add_neurons(bestPara,'tanh')
        # elm.add_neurons(bestPara,'rbf_l2')
        elm.train(X_train, y_train.values, 'CV', 'OP', 'r', k=5)
        y_pred_temp = elm.predict(X_test)

        # elm.add_neurons(30, "sigm")
        # elm.add_neurons(30, "rbf_l2")
        # elm.train(X_train, y_train.values, 'CV','OP',k=5)
        #
        # # svr_rbf = SVR(kernel='rbf', C=1000.0, gamma='auto', max_iter=-1, epsilon=0.1)
        # # svr_poly = SVR(kernel='poly', C=1000, degree=3)
        # y_pred_temp = elm.predict(X_test)
        # print("This is temp y_pred: ", y_pred_temp )
        y_pred = []
        for t in y_pred_temp:
            if t < 0:
                y_pred.append(0)
            else:
                y_pred.append(t)
        # y_pred = svr_poly.fit(X_train, y_train).predict(X_test)
        if isRegression:
            return y_pred
        else:
            sum_mean = 0
            for i in range(len(y_pred)):
                if isNormal:
                    print(
                        "This is REAL value %.4f, ======ELM=====> PRED value: %.4f"
                        % (y_test[i], y_pred[i]))
                    # sum_mean += (y_pred[i] - y_test[i]) ** 2  # if the target is np array

                    sum_mean += (float("{0:.4f}".format(float(y_pred[i]))) -
                                 y_test[i])**2
                else:
                    print(
                        "This is REAL value %.4f, ======ELM=====> PRED value: %.4f"
                        % (y_test.values[i], y_pred[i]))
                    # sum_mean += (y_pred[i] - y_test.values[i]) ** 2

                    sum_mean += (float("{0:.4f}".format(float(y_pred[i]))) -
                                 y_test.values[i])**2
            sum_erro = np.sqrt(sum_mean / len(y_pred))
            elmList.append(sum_erro[0])
            print("This is RMSE for ELM: ", sum_erro[0])
            print("This is iteration num: ", j + 1)
    return elmList
Exemple #15
0
y_all = y_train
X_all = X_train
X_train_0 = X_train[y_train == 0][:]
X_train_1 = X_train[y_train == 1][:]
X_train_0_down = np.array(random.sample(X_train_0, X_train_1.shape[0]))
X_train = np.vstack([X_train_0_down, X_train_1])
y_train_0 = np.zeros([X_train_0_down.shape[0], 1], dtype=int)
y_train_1 = np.ones([X_train_1.shape[0], 1], dtype=int)
y_train = np.vstack([y_train_0, y_train_1])
y_train = utils.one_hot(y_train)
elm = ELM(X_train.shape[1], y_train.shape[1], classification="c")
elm.add_neurons(10, "sigm")
elm.train(X_train, y_train, "CV", k=10)

Y = elm.predict(X_train)
print(elm.error(y_train, Y))
# y_pred = np.argmax(Y, 1)
# cm = metrics.confusion_matrix(y_true=y_test, y_pred=y_pred)
# print cm
# X_hmm = []
# lengths_hmm = []
# frameNumber = 20
# n_components = 5
# n_mix = 6
# for index in range(0, len(y_all)):
#     if y_all[index] == 0:
#         continue
#     else:
#         cur = np.array(X_all[index - frameNumber:index, :]).tolist()
#         X_hmm.extend(cur)
#         lengths_hmm.append(frameNumber)