Ejemplo n.º 1
0
class ELM(Classifier):
    def __init__(self, neurons: Tuple[Tuple] = None) -> None:
        clf = None
        self.neurons = neurons if neurons else DEFAULT_NEURONS
        super().__init__(clf)

    def fit(self, x_train: ndarray, y_train: ndarray, *args, **kwargs)\
            -> None:
        self.classifier = ELMachine(x_train.shape[1], y_train.shape[1])
        for neuron in self.neurons:
            logger.info("Adding {} neurons with '{}' function.".format(
                neuron[0], neuron[1]))
            self.classifier.add_neurons(neuron[0], neuron[1])
        logger.debug("Training the Extreme Learning Machine Classifier...")
        start = time()
        self.classifier.train(x_train, y_train, **kwargs)
        logger.debug("Done training in {} seconds.".format(time() - start))

    def predict(self, x_test: ndarray) -> ndarray:
        logger.debug("Predicting {} samples...".format(x_test.shape[0]))
        start = time()
        predictions = np.argmax(self.classifier.predict(x_test), axis=-1)
        logger.debug("Done all predictions in {} seconds.".format(time() -
                                                                  start))
        return predictions

    def predict_proba(self, x_test: ndarray) -> float:
        logger.debug("Predicting {} samples...".format(x_test.shape[0]))
        start = time()
        predictions = self.classifier.predict(x_test)
        logger.debug("Done all predictions in {} seconds.".format(time() -
                                                                  start))
        return predictions
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
Ejemplo n.º 3
0
    def tune_elm(train_x, train_y, test_x_raw, test_x, act_funcs,
                 neuron_counts):
        '''
		Assumptions:
		1. NN has only 1 hidden layer
		2. act_funcs: list of distinct activation functions
		3. neuron_counts: list of distinct '# of neurons in the hidden layer'
		'''
        print("Tuning ELM...")
        features = train_x.shape[1]
        train_y = Pre_processor.one_hot_encoding(train_y)
        ind_func = 0
        while (ind_func < len(act_funcs)):
            ind_neuron = 0
            cur_act_func = act_funcs[ind_func]
            while (ind_neuron < len(neuron_counts)):
                cur_neuron_count = neuron_counts[ind_neuron]
                print(cur_act_func + " | " + str(cur_neuron_count) + "...")
                clf = ELM(features, Constants.tot_labels)
                clf.add_neurons(cur_neuron_count, cur_act_func)
                clf.train(train_x, train_y, 'CV', 'OP', 'c', k=10)
                pred_y = clf.predict(test_x)
                pred_y = Pre_processor.one_hot_decoding_full(pred_y)
                file_name = "submission_" + str(
                    cur_neuron_count) + "_" + cur_act_func + ".csv"
                Database.save_results(test_x_raw, pred_y, file_name)
                ind_neuron = ind_neuron + 1
            ind_func = ind_func + 1
Ejemplo n.º 4
0
def predict_new_data(argv):
	'''
	Implements output prediction for new data

	Arguments:
	argv -- system inputs

	Returns:
	Y -- predicted Y value

	'''

	# file1 = saved model, file2 = excel file with new data
	print(argv)
	_, file1, file2 = argv
	print(file1)
	print(file2)

	# Process the excel data 
	X = process_data(file2)

	# Load model
	model = ELM(20, 1, tprint=5)
	model.load('{}'.format(file1))

	# Predict Y
	Y_predicted = model.predict(X)

	return Y_predicted	
Ejemplo n.º 5
0
def t10k_test(model_path='../models/elm.model'):
    images, labels = read_mnist.load_mnist('../data/', kind='t10k')
    images = map(read_mnist.up_to_2D, images)
    images = map(get_hog, images)
    images = np.mat(np.array(images))

    labels = np.mat(map(read_mnist.handle_label, labels))

    elm = ELM(images.shape[1], labels.shape[1])
    # print images.shape[1], images.shape[1]
    elm.load(model_path)
    results = elm.predict(images)

    labels = map(get_labels, np.array(labels))
    results = map(get_labels, np.array(results))
    yes, tot = 0, len(labels)

    for i in range(0, len(labels)):
        if labels[i] == results[i]:
            yes += 1

    print 'YES :', yes
    print 'TOT :', tot
    print 'ACC : ', str(float(yes) / tot * 100.0) + '%'
    return float(yes) / tot * 100.0
Ejemplo n.º 6
0
class HPELMNN(Classifier):
    
    def __init__(self):
        self.__hpelm = None
    
    @staticmethod
    def name():
        return "hpelmnn"

    def train(self, X, Y, class_number=-1):
        class_count = max(np.unique(Y).size, class_number)
        feature_count = X.shape[1]
        self.__hpelm = ELM(feature_count, class_count, 'wc')
        self.__hpelm.add_neurons(feature_count, "sigm")

        Y_arr = Y.reshape(-1, 1)
        enc = OneHotEncoder()
        enc.fit(Y_arr)
        Y_OHE = enc.transform(Y_arr).toarray()

        out_fd = sys.stdout
        sys.stdout = open(os.devnull, 'w')
        self.__hpelm.train(X, Y_OHE)
        sys.stdout = out_fd

    def predict(self, X):
        Y_predicted = self.__hpelm.predict(X)
        return Y_predicted
Ejemplo n.º 7
0
 def epoch(train_x, train_y, test_x, test_x_raw, filename):
     features = train_x.shape[1]
     train_y = Pre_processor.one_hot_encoding(train_y)
     clf = ELM(features, Constants.tot_labels)
     clf.add_neurons(550, "sigm")
     clf.train(train_x, train_y, 'CV', 'OP', 'c', k=10)
     pred_y = clf.predict(test_x)
     pred_y = Pre_processor.one_hot_decoding_full(pred_y)
     Database.save_results(test_x_raw, pred_y, filename)
Ejemplo n.º 8
0
 def test_Classification_WorksCorreclty(self):
     elm = ELM(1, 2)
     X = np.array([-1, -0.6, -0.3, 0.3, 0.6, 1])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'c')
     Y = elm.predict(X)
     self.assertGreater(Y[0, 0], Y[0, 1])
     self.assertLess(Y[5, 0], Y[5, 1])
 def test_Classification_WorksCorreclty(self):
     elm = ELM(1, 2)
     X = np.array([-1, -0.6, -0.3, 0.3, 0.6, 1])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'c')
     Y = elm.predict(X)
     self.assertGreater(Y[0, 0], Y[0, 1])
     self.assertLess(Y[5, 0], Y[5, 1])
Ejemplo n.º 10
0
 def test_WeightedClassification_ClassWithLargerWeightWins(self):
     elm = ELM(1, 2)
     X = np.array([1, 2, 3, 1, 2, 3])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'wc', w=(1, 0.1))
     Y = elm.predict(X)
     self.assertGreater(Y[0, 0], Y[0, 1])
     self.assertGreater(Y[1, 0], Y[1, 1])
     self.assertGreater(Y[2, 0], Y[2, 1])
 def test_WeightedClassification_ClassWithLargerWeightWins(self):
     elm = ELM(1, 2)
     X = np.array([1, 2, 3, 1, 2, 3])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'wc', w=(1, 0.1))
     Y = elm.predict(X)
     self.assertGreater(Y[0, 0], Y[0, 1])
     self.assertGreater(Y[1, 0], Y[1, 1])
     self.assertGreater(Y[2, 0], Y[2, 1])
Ejemplo n.º 12
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
Ejemplo n.º 13
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
Ejemplo n.º 14
0
def run(X, Y):
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)
    #ELM model
    X_train=X_train.values
    X_test=X_test.values
    y_train=y_train.values
    y_test=y_test.values
    print 'ELM tanh'
    for x in range(50, 500, 50):
        elm = ELM(X_train.shape[1], 1, classification='c')
        elm.add_neurons(x, 'tanh')
        elm.train(X_train, y_train)
        pred = elm.predict(X_test)
        temp = []
#        print 'Error(TANH, ', x, '): ', elm.error(y_test, pred)
        for p in pred:
            if p >=0.5:
                temp.append(1)
            else:
                temp.append(0)
        pred = np.asarray(temp)
#        print 'Error(TANH, ', x, '): ', elm.error(y_test, pred)
        evaluate(y_test, pred)
    
    print 'ELM rbf_linf tanh'
    for x in range(10, 100, 10):
        elm = ELM(X_train.shape[1], 1)
        elm.add_neurons(x, 'rbf_linf')
        elm.add_neurons(x*2, 'tanh')
        elm.train(X_train, y_train)
        pred = elm.predict(X_test)
        temp = []
#        print 'Error(TANH, ', x, '): ', elm.error(y_test, pred)
        for p in pred:
            if p >=0.5:
                temp.append(1)
            else:
                temp.append(0)
        pred = np.asarray(temp)
#        print 'Error(RBF+TANH, ', x, ',', 2*x, '): ', elm.error(y_test, pred)
        evaluate(y_test, pred)
result6.write("accuracy" + "\t" + "precision" + "\t" + "recall" + "\t" +
              "f1-measure" + "\t" + " mse" + "\t" + " mae" + "\t" + "auc" +
              "\t" + "tpr" + "\t" + "fpr")
result6.write("\n")

result6a = open("data/result/linmse.txt", "w")
result6a.write("accuracy" + "\t" + "precision" + "\t" + "recall" + "\t" +
               "f1-measure" + "\t" + " mse" + "\t" + " mae" + "\t" + "auc" +
               "\t" + "pr" + "\t" + "fpr")
result6a.write("\n")

print "sigmoid with multi class error"
elm = ELM(41, 41)
elm.add_neurons(40, "sigm")
elm.train(X, Y, "c")
r1 = elm.predict(X1)
result.write(str(elm))
result.write("\n")

r1 = r1.argmax(1)
accuracy = accuracy_score(Y1, r1)
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)
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 +
Ejemplo n.º 16
0
Y = np.loadtxt("tweet_label.txt", delimiter=',')
Y_onehot = np.loadtxt("tweet_label_onehot.txt", delimiter=',')

X_train, X_test, Y_train, Y_test, Y_onehot_train, Y_onehot_test = train_test_split(X, Y, Y_onehot, test_size=0.20, shuffle=False)

print("Starting training...")
elm = ELM(X_train.shape[1], Y_onehot_train.shape[1])
elm.add_neurons(200, "sigm")
elm.add_neurons(100, "tanh")
elm.add_neurons(100, "sigm")
elm.add_neurons(100, "sigm")
elm.add_neurons(100, "tanh")
elm.train(X_train, Y_onehot_train, "CV", "OP", "c", k=5)
print("Finished training...")

Y_predicted_elm = elm.predict(X_test)
Y_predicted = np.zeros((Y_predicted_elm.shape[0]))
for i, row in enumerate(Y_predicted_elm):
    idx_of_max = np.argmax(row)
    Y_predicted[i] = idx_of_max+1

with open("Y_predicted.txt", 'w+') as predfile, open("Y_true.txt", 'w+') as trufile:
    for i in Y_predicted:
        predfile.write(str(i))
        predfile.write("\n")
    for i in Y_test:
        trufile.write(str(i))
        trufile.write("\n")

score = accuracy_score(Y_test, Y_predicted)
Ejemplo n.º 17
0
y_test = np.array(feature_test.iloc[:, -1])
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)
Ejemplo n.º 18
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
Ejemplo n.º 19
0
outp = np.loadtxt("output.txt")

# BUILD ELM -----------------------------------------------
neuron = 400
elm = ELM(92, 40)
elm.add_neurons(neuron, "sigm")

# TRAIN ---------------------------------------------------
t0 = time.clock()
elm.train(inp, outp, "c")
t1 = time.clock()
tr = t1-t0

# PREDICT -------------------------------------------------
t0 = time.clock()
pred = elm.predict(testp)
t1 = time.clock()
te = t1-t0

# RESULT --------------------------------------------------
for p in pred:
    i = 0
    for v in p:
        i += 1
        if 1.0 - abs(v) < 0.00001:
            print "people id:", i
            break

print "training took", tr*1000000, "ns"
print "testing took", te*1000000, "ns"
Ejemplo n.º 20
0
import hpelm

curdir = os.path.dirname(__file__)
pX = os.path.join(curdir, "../dataset_tests/iris/maldata1.txt")
pY = os.path.join(curdir, "../dataset_tests/iris/mallabel.txt")

#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")
Ejemplo n.º 21
0
#xoutput = XXtrain[id_input,]
#print xinput.shape
## INPUT LAYER
singleLayer = True
if singleLayer:
    t = 5
    mn_error = np.zeros([t, 1])
    for i in range(0, t):
        elmSingle = ELM(input_shape, YY.shape[1])
        #elmSingle.add_neurons(ninputsig/2, "sigm")
        elmSingle.add_neurons(ninputsig, "sigm")
        #elmSingle.add_neurons(100, "rbf_l1")
        #elmSingle.add_neurons(ninputsig/10, "lin")
        elmSingle.train(XXtrainIn, YYtrain, "c", norm=1e-5)
        print "\n Trained input elm", elmSingle
        youtput = elmSingle.predict(XXtest)

        p = ytest.squeeze()
        yout = np.argmax(youtput, axis=1)
        nhit = sum(yout == p)
        ntpos = sum((yout == 1) & (p == 1))
        ntneg = sum((yout == 0) & (p == 0))
        npos = sum((p == 1))
        nneg = sum((p == 0))
        print "\n Testing results"
        print "Tpos:", ntpos, " / ", npos, "TD:", ntpos / float(npos)
        print "Tneg:", ntneg, " / ", nneg, "TN:", ntneg / float(nneg)
        print "Acc: ", nhit / (float)(len(p)), "total", len(p)
        mn_error[i] = nhit / (float)(len(p))
    print mn_error
    print "mean error", np.mean(mn_error)
result6 = open("data/result/lin.txt", "w")
result6.write("accuracy" + "\t" + "precision" + "\t" + "recall" + "\t" +
              "f1-measure" + "\t" + " mse" + "\t" + " mae" + "\t" + "auc")
result6.write("\n")

result6a = open("data/result/linmse.txt", "w")
result6a.write("accuracy" + "\t" + "precision" + "\t" + "recall" + "\t" +
               "f1-measure" + "\t" + " mse" + "\t" + " mae" + "\t" + "auc")
result6a.write("\n")

print "sigmoid with multi class error"
elm = ELM(243, 8)
elm.add_neurons(40, "sigm")
elm.train(X, Y, "c")
r1 = elm.predict(X1)
result.write(str(elm))
result.write("\n")

r1 = r1.max(1)
print(r1)
accuracy = accuracy_score(Y1, r1)
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)
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 +
Ejemplo n.º 23
0
    plt.plot(t_real[0:len_learn],sampled_data_FC1[0:len_learn,1],'g-',markersize=8.0,linewidth=10.0,alpha=0.8)
    plt.plot(t_real[0:len_learn],sampled_data_FC1[0:len_learn,1],'b-',markersize=3.0,linewidth=3.0)
   # plt.plot(t_real[0:regressors_num],sampled_data_FC1[0:regressors_num,1],'r-',markersize=3.0,linewidth=3.0,dashes=[10, 6, 1, 6, 1, 6])
    train_out=train_out+3.5
    plt.plot(t_real[0:len_learn],train_out[0:len(train_out)],'r',markersize=3.0,linewidth=3.0,dashes=[10, 6, 1, 6, 1, 6])
    
    # plot the predict phase data
    plt.plot(t_real[len_learn-1:len_prognostics],sampled_data_FC1[len_learn-1:len_prognostics,1],'b-',markersize=3.0,linewidth=3.0)
    plt.plot(t_real[len_learn:len_prognostics],FC1_prognostics,'r',markersize=3.0,linewidth=3.0,dashes=[8, 4, 2, 4, 2, 4])
    # plt.ylim(3.1,3.5)
    # plt.plot(t_real[0:iterations],x_particle,'k.',markersize=0.5)
    plt.grid()
    plt.xlabel('real time')
    plt.ylabel('voltage')
    plt.title('ELM')
    plt.legend(['observation','real data','train_predict'])
    plt.show()


Yh = elm.predict(X_learn)

plt.plot(Y_learn,color='r',linewidth=3)
plt.plot(Yh,color='g',linewidth=1)
plt.show()

plot_prognostic(Yh)

acc = float(np.sum(Y_learn.argmax(1) == Yh.argmax(1))) / Y_learn.shape[0]
print "Iris dataset training error: %.1f%%" % (100-acc*100)

Ejemplo n.º 24
0
print(X_train_d2.shape)
print(X_test_d3.shape)
print(X_test_d2.shape)
X_train = ss.fit_transform(np.concatenate((X_train_d3, X_train_d2), axis=1))
X_test = ss.transform(np.concatenate((X_test_d3, X_test_d2), axis=1))
print(X_train.shape)
print(X_test.shape)

#use ELM as classifier

from hpelm import ELM
acc = []
elm = ELM(4, 1)
elm.add_neurons(50, 'sigm')
elm.train(X_train, y_train, "LOO")
y_pred = elm.predict(X_test)
print(len(y_pred))
for i in range(len(y_pred)):
    if y_pred[i] >= 0.5:
        y_pred[i] = 1
    else:
        y_pred[i] = 0
print(y_test)
acc.append(accuracy_score(y_test, y_pred))
avg_acc = np.mean(acc)
print(avg_acc)

# use LDA as classifier

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
clf = LinearDiscriminantAnalysis()
#!/usr/bin/env python

import numpy as np
import time
import random
import sys
from hpelm import ELM

inp = np.loadtxt("input.txt")
outp = np.loadtxt("output.txt")

for neuron in range(10, 1000, 10):
    elm = ELM(92, 40)
    elm.add_neurons(neuron, "sigm")

    t0 = time.clock()
    elm.train(inp, outp, "c")
    t1 = time.clock()
    t = t1-t0

    pred = elm.predict(inp)
    acc = float(np.sum(outp.argmax(1) == pred.argmax(1))) / outp.shape[0]
    print "neuron=%d error=%.1f%% time=%dns" % (neuron, 100-acc*100, t*1000000)
    
    if int(acc) == 1:
        break
result5.write("accuracy" +  "\t" +  "precision" + "\t" + "recall" + "\t" + "f1-measure" +"\t" + " mse" + "\t" + " mae" + "\t" + "auc")
result5.write("\n")


result6 = open("data/result/lin.txt", "w")

result6.write("accuracy" +  "\t" +  "precision" + "\t" + "recall" + "\t" + "f1-measure" +"\t" + " mse" + "\t" + " mae" + "\t" + "auc")
result6.write("\n")


print "sigmoid with multi class error"
elm = ELM(41,23)
elm.add_neurons(15, "sigm")
elm.train(X, Y, "c")
r1 = elm.predict(X1)
print(str(elm))
print("performance measures")
result.write(str(elm))
result.write("\n")

r1=r1.argmax(1)
accuracy = accuracy_score(Y1, r1)
print(accuracy)
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)
fpr, tpr, thresholds = metrics.roc_curve(Y1, r1,pos_label=2)
auc = metrics.auc(fpr, tpr)
Ejemplo n.º 27
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 19 16:29:09 2014

@author: akusok
"""

import numpy as np
import os
from hpelm import ELM

curdir = os.path.dirname(__file__)
pX = os.path.join(curdir, "../datasets/Unittest-Iris/iris_data.txt")
pY = os.path.join(curdir, "../datasets/Unittest-Iris/iris_classes.txt")

X = np.loadtxt(pX)
Y = np.loadtxt(pY)

elm = ELM(4,3)
elm.add_neurons(15, "sigm")
elm.train(X, Y, "c")
Yh = elm.predict(X)
acc = float(np.sum(Y.argmax(1) == Yh.argmax(1))) / Y.shape[0]
print("Iris dataset training error: %.1f%%" % (100-acc*100))
Ejemplo n.º 28
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 19 16:29:09 2014

@author: akusok
"""

import numpy as np
import os
from hpelm import ELM

curdir = os.path.dirname(__file__)
pX = os.path.join(curdir, "../datasets/Unittest-Iris/iris_data.txt")
pY = os.path.join(curdir, "../datasets/Unittest-Iris/iris_classes.txt")

X = np.loadtxt(pX)
Y = np.loadtxt(pY)

elm = ELM(4, 3)
elm.add_neurons(15, "sigm")
elm.train(X, Y, "c")
Yh = elm.predict(X)
acc = float(np.sum(Y.argmax(1) == Yh.argmax(1))) / Y.shape[0]
print "Iris dataset training error: %.1f%%" % (100 - acc * 100)
Ejemplo n.º 29
0
                        temp[rating_mat['test_1'].T[mat_iid][i - 1] - 1] = 1
                        test_rat.append(temp)
                        #print mat_iid
                mat_iid += 1

        X = np.asarray(X, dtype=np.uint8)
        T = np.asarray(T, dtype=np.uint8)
        test = np.asarray(test, dtype=np.uint8)
        test_rat = np.asarray(test_rat, dtype=np.uint8)

        ##print X.shape,test.shape

        elm = ELM(X.shape[1], T.shape[1])
        elm.add_neurons(neuron, node)
        elm.train(X, T, "LOO")
        Y = elm.predict(test)

        pred = np.argmax(Y, axis=1)
        true = np.argmax(test_rat, axis=1)

        print 'Split 1 RMSE: ', mse(true, pred)**0.5
        print 'Split 1 NMAE: ', mae(true, pred) / 4

        i1_rmse = mse(true, pred)**0.5
        i1_nmae = mae(true, pred) / 4
        #################################SPLIT 2#####################################
        train_ids = map(int, train_ids_read.readline().strip().split(','))
        test_ids = map(int, test_ids_read.readline().strip().split(','))

        X = []
        T = []