Ejemplo n.º 1
0
    y_test = testLabelArr
    # clf = ELMClassifier(n_hidden=100, activation_func='gaussian', alpha=0.0, random_state=1)
    # clf = ELMClassifier(n_hidden=100 000, activation_func='hardlim', alpha=1.0, random_state=0)
    clf = ELMClassifier(n_hidden=100,
                        activation_func='hardlim',
                        alpha=1.0,
                        random_state=1)
    for iter in range(2):
        # y_target = mat(labelArr)
        y_target = sign(mat(labelArr) - mat(y_train_pred))
        print y_target
        print y_train_pred
        # print shape(y_target)
        # iterate over classifiers
        clf.fit(datArr, list(array(y_target).reshape(-1)))
        y_train_pred = clf.predict(datArr)
        y_pred = clf.predict(testDatArr)
        acc = calcAccurary(sign(y_train_pred), labelArr)
        print "Training accurarcy:\n"
        print acc
        acc = calcAccurary(sign(y_pred), y_test)
        print acc
        y_preds = y_preds + LRATE * y_pred
    print mat(y_preds)
    acc = calcAccurary(sign(y_preds), y_test)
    # print y_preds
    print "Final score:\n"
    print acc
    """
	for name, clf in zip(names, classifiers):
		clf.fit(X_train, y_train)
Ejemplo n.º 2
0
    def run(self):

        rawDataset = open('../../selectedUserMore', 'r')

        # Value 0 represents max number of apps => It must be set
        maxValuesByFeature = [6, 4, 2, 2, 2, 0, 4, 5, 3]

        datasetFeatures = []
        datasetLabels = []

        maxApps = self.findMaxNumberOfApps(rawDataset)

        # Setting max number of apps
        maxValuesByFeature[5] = maxApps

        rawDataset.seek(0)
        for line in rawDataset:
            tuples = line.split(';')

            for t in range(len(tuples)):

                tup = tuples[t]
                vals = tup.split(',')
                label = vals[0]
                features = vals[1].split('_')

                datasetFeatures.append(features)
                datasetLabels.append(label)

        rawDataset.close()

        nTuples = len(datasetLabels)

        nTrain = int(0.7 * nTuples)

        nMaxHidden = 3005
        totalAccuracy = 0.0

        trainSetFeatures = np.array(datasetFeatures[:nTrain], dtype=np.int)
        trainSetLabels = np.array(datasetLabels[:nTrain], dtype=np.int)

        testSetFeatures = np.array(datasetFeatures[nTrain:], dtype=np.int)
        testSetLabels = np.array(datasetLabels[nTrain:], dtype=np.int)

        # for x in range(200,nMaxHidden):

        ffsn = ELMClassifier(n_hidden=1000, activation_func='sine')
        ffsn.fit(trainSetFeatures, trainSetLabels)

        results = ffsn.predict(testSetFeatures)

        nPredictedCorrectly = 0

        for test in range(len(testSetLabels)):
            realValue = testSetLabels[test]
            predictedValue = results[test]

            if (int(realValue) == int(predictedValue)):
                nPredictedCorrectly += 1

            # print "Real: " + str(realValue) + " / Predicted: " + str(predictedValue)

        totalTests = nTuples - nTrain
        accuracy = float(nPredictedCorrectly) / totalTests
        # print "N Hidden: " + str(x) + " / Accuracy: " + str(accuracy)
        print "Accuracy: " + str(accuracy)
        totalAccuracy += accuracy
Ejemplo n.º 3
0
        train_label = train_data.map(lambda x: x[class_index]).collect()
        print train_label
        ###############  ELM 训练  #############

        train_array = train_.collect()
        print "train_array_num:", len(train_array)
        #训练分类器
        elmc.fit(train_array, train_label)
        pred_class = elmc.predict_class(test_array)
        #分类精度
        soc = accuracy_score(pred_class, test_label)
        print "test_soc:", soc
        #软最大化函数
        ruanzuida = lambda x: np.exp(x) / sum(np.exp(x))
        #对无类标的数据集进行预测 每个样例的到一个向量  然后进行软最大化处理
        ruanhua_result = unlabel_.flatMap(lambda x: elmc.predict(x)).map(
            lambda x: max(ruanzuida(x)))
        print ruanhua_result.collect()
        #对软最大化处理后的数据进行过滤 为了使排序的样例变少  所以先进行初步过滤
        filter_result = ruanhua_result.filter(
            lambda x: x < frist_filter).collect()
        #对过滤后的样例排序 得到第10小的数值作为阈值
        try:
            yuzhi = sorted(filter_result)[select_num]
        except IndexError:
            print "select_num", (select_num / 2)
            yuzhi = sorted(filter_result)[int(select_num // 2)]
            continue
        finally:
            pass
Ejemplo n.º 4
0
# Shuffle target and data
shfl_data = loaded_data.data[rand_perm]
shfl_target = loaded_data.target[rand_perm]
# divide train data and test data
offset = np.floor(data_size*0.8) # 80 percent data as training
train_data = shfl_data[:offset,:]
train_target = shfl_target[:offset]
test_data = shfl_data[offset+1:,:]
test_target = shfl_target[offset+1:]

# Build ELM Classifier using default value
elm_classifier = ELMClassifier()
# Train ELM
elm_classifier.fit(train_data,train_target)

predicted_class = elm_classifier.predict(test_data)
error_elm = 1.0 - elm_classifier.score(test_data, test_target)
print 'Predicted Class\t\t', predicted_class
print 'Original Class\t\t', test_target
print 'Error Score\t\t\t', error_elm
print ''

#print '##############################################################\nSINGLE GEN-ELM RESULT\n##############################################################'
from elm import GenELMClassifier
## Build GEN_ELM Classifier using default value
#genelm_classifier = GenELMClassifier()
## Train the ELM
#genelm_classifier.fit(train_data,train_target)
#
#predicted_class = genelm_classifier.predict(test_data)
#error_elm = 1.0 - genelm_classifier.score(test_data, test_target)
Ejemplo n.º 5
0
    print "\nTime: %.3f secs" % (time() - start_time)

    print "Test Min: %.3f Mean: %.3f Max: %.3f SD: %.3f" % (
        min(test_res), np.mean(test_res), max(test_res), np.std(test_res))
    print "Train Min: %.3f Mean: %.3f Max: %.3f SD: %.3f" % (
        min(train_res), np.mean(train_res), max(train_res), np.std(train_res))
    print
    return (train_res, test_res)


stdsc = StandardScaler()

iris = load_iris()
irx, iry = stdsc.fit_transform(iris.data), iris.target
irx_train, irx_test, iry_train, iry_test = train_test_split(irx,
                                                            iry,
                                                            test_size=0.2)

srh = SimpleRandomHiddenLayer(activation_args='sigmoid', n_hidden=500)
elmc = ELMClassifier(hidden_layer=srh)
# elmc = ELMClassifier(SimpleRandomHiddenLayer(activation_func='sigmoid'))
# print "SimpleRandomHiddenLayer(activation_func='sigmoid')"
# tr,ts = res_dist(irx, iry, elmc, n_runs=100, random_state=0)
# plt.hist(tr),plt.hist(tr)
# plt.show()

elmc.fit(irx_train, iry_train)
r = elmc.predict(irx_test)
print r
res = elmc.score(irx_test, iry_test)
print res
Ejemplo n.º 6
0
def simulate(trn, tst):
    start = time.time()
    b_tp = b_fp = b_tn = b_fn = 0
    s_tp = s_fp = s_tn = s_fn = 0
    b_min = s_min = 1000000
    b_max = s_max = 0
    b_money = s_money = 0
    b_money_vec = [0]
    s_money_vec = [0]
    b_gain = s_gain = 0
    b_loss = s_loss = 0
    b_draw = s_draw = 0
    b_gain_vec = []
    s_gain_vec = []
    b_loss_vec = []
    s_loss_vec = []
    b_max_drawdown = s_max_drawdown = 0
    b_pos = s_pos = False
    time_vec = []
    aux_ii = len(tst) - 1

    for t, val in enumerate(tst):
        start_i = time.time()

        if t == 201:
            continue

        if t == 0:
            tst[0, 5] = id.log_return(tst[0, 0], tst[0, 3], trn[-1, 0], trn[-1,
                                                                            3])
        else:
            tst[t, 5] = id.log_return(tst[t, 0], tst[t, 3], trn[t - 1, 0],
                                      trn[t - 1, 3])
        tst[t, 6] = mnm.get(val[5])
        tst[t, 7] = obv.get_obv(val[3], val[4])
        aux = bbs.sma(val[3])
        if aux is not None:
            tst[t, 8], tst[t, 9] = aux
        aux_9 = m_9.ema(val[3])
        aux12 = m12.ema(val[3])
        aux26 = m26.ema(val[3])
        tst[t, 10] = aux12 - aux26
        tst[t, 11] = tst[t, 10] - aux_9

        aux = trn[-1000:]
        aux_i = [(i[1] - mn[i[0]]) * mx[i[0]] for i in enumerate(tst[t, :12])]
        # aux_j = trn[-1000:, :]

        b_elm = ELMClassifier(random_state=0,
                              n_hidden=200,
                              activation_func='sigmoid',
                              alpha=0.0)
        b_elm.fit(aux[:, :12], aux[:, 12])
        b_res = b_elm.predict([aux_i[:12]])
        s_elm = ELMClassifier(random_state=0,
                              n_hidden=200,
                              activation_func='sigmoid',
                              alpha=0.0)
        s_elm.fit(aux[:, :12], aux[:, 13])
        s_res = s_elm.predict([aux_i[:12]])

        if b_res == 1.0:
            if val[12] == 1.0:
                b_tp += 1
            else:
                b_fp += 1
            if not b_pos:
                # Entra
                b_money -= val[3]
                b_pos = True
        else:
            if val[12] == 0.0:
                b_tn += 1
            else:
                b_fn += 1
            if b_pos:
                # Sai
                b_money += val[3]
                b_pos = False
                if b_money < b_money_vec[-1]:
                    b_loss += 1
                    b_loss_vec.append(b_money_vec[-1] - b_money)
                elif b_money > b_money_vec[-1]:
                    b_gain += 1
                    b_gain_vec.append(b_money - b_money_vec[-1])
                else:
                    b_draw += 1
        if val[14] == 1.0:
            # Sai
            b_money += val[3]
            b_pos = False
            if b_money < b_money_vec[-1]:
                b_loss += 1
                b_loss_vec.append(b_money_vec[-1] - b_money)
            elif b_money > b_money_vec[-1]:
                b_gain += 1
                b_gain_vec.append(b_money - b_money_vec[-1])
            else:
                b_draw += 1

        if b_pos:
            b_money_vec.append(b_money_vec[-1])
        else:
            b_money_vec.append(b_money)
            if b_money > b_max:
                b_max = b_money
            if b_money < b_min:
                b_min = b_money

        if s_res == 1.0:
            if val[13] == 1.0:
                s_tp += 1
            else:
                s_fp += 1
            if not s_pos:
                # Entra
                s_money += val[3]
                s_pos = True
        else:
            if val[13] == 0.0:
                s_tn += 1
            else:
                s_fn += 1
            if s_pos:
                # Sai
                s_money -= val[3]
                s_pos = False
                if s_money < s_money_vec[-1]:
                    s_loss += 1
                    s_loss_vec.append(s_money_vec[-1] - s_money)
                elif s_money > s_money_vec[-1]:
                    s_gain += 1
                    s_gain_vec.append(s_money - s_money_vec[-1])
                else:
                    s_draw += 1
        if val[14] == 1.0:
            # Sai
            s_money -= val[3]
            s_pos = False
            if s_money < s_money_vec[-1]:
                s_loss += 1
                s_loss_vec.append(s_money_vec[-1] - s_money)
            elif s_money > s_money_vec[-1]:
                s_gain += 1
                s_gain_vec.append(s_money - s_money_vec[-1])
            else:
                s_draw += 1

        if s_pos:
            s_money_vec.append(s_money_vec[-1])
        else:
            s_money_vec.append(s_money)
            if s_money > s_max:
                s_max = s_money
            if s_money < s_min:
                s_min = s_money

        # print(aux_i + list(tst[t, 12:]))
        trn = np.append(trn, [aux_i + list(tst[t, 12:])], axis=0)
        time_vec.append(time.time() - start_i)
        sys.stdout.write('\r' + '%6d / %d' % (t, aux_ii) + '\033[K')
    sys.stdout.write('\r' + '>> %6.2f: Simulation Done!\n\n' %
                     (time.time() - start) + '\033[K')

    print('#### ' + sys.argv[1] + ' ####')
    print('Tempo médio: %f' % np.mean(time_vec))
    print('Final      : %5.5f | %5.5f' % (b_money, s_money))
    # print('Final      : %5.5f | %5.5f' % (b_money_vec[-1], s_money_vec[-1]))
    print('Minimo     : %5.5f | %5.5f' % (b_min, s_min))
    print('Maximo     : %5.5f | %5.5f' % (b_max, s_max))
    print('Ganho qtd  : %10d | %10d' % (b_gain, s_gain))
    print('Perda qtd  : %10d | %10d' % (b_loss, s_loss))
    print('Empate qtd : %10d | %10d' % (b_draw, s_draw))
    print('Ganho medio: %5.5f | %5.5f' %
          (np.mean(b_gain_vec), np.mean(s_gain_vec)))
    print('Perda media: %5.5f | %5.5f' %
          (np.mean(b_loss_vec), np.mean(s_loss_vec)))
    print('TP         : %10d | %10d' % (b_tp, s_tp))
    print('FP         : %10d | %10d' % (b_fp, s_fp))
    print('TN         : %10d | %10d' % (b_tn, s_tn))
    print('FN         : %10d | %10d' % (b_fn, s_fn))

    plot(b_money_vec, s_money_vec, sys.argv[1], tst[:, 3])
Ejemplo n.º 7
0
reg = reg.fit(X, y)
cla = cla.fit(X, y)

#Read testing data from testing.csv
testData = pd.read_csv('testing.csv')
pdt = np.zeros(len(testData) - 3)
pdt2 = np.zeros(len(testData) - 3)

for i in range(len(testData) - 3):
    ftr1 = float(testData['close'][i]) - float(testData['open'][i])
    ftr2 = float(testData['close'][i+1]) - float(testData['open'][i+1])
    ftr3 = float(testData['close'][i+2]) - float(testData['open'][i+2])
    ftr4 = float(testData['highest'][i] - float(testData['lowest'][i]))
    ftr5 = float(testData['highest'][i+1] - float(testData['lowest'][i+1]))
    ftr6 = float(testData['highest'][i+2] - float(testData['lowest'][i+2]))
    pdt[i] = reg.predict([ftr1, ftr2, ftr3, ftr4, ftr5, ftr6])
    pdt2[i] = cla.predict([ftr1, ftr2, ftr3, ftr4, ftr5, ftr6])

#print pdt
print pdt2
#Verify the testing data
#The 0.01 is the threshold to determine rise or full
correct = 0
for i in range(len(testData) - 3):
    if testData['changerange'][i] >=0 and float(pdt[i]) >= 0.09:
        correct += 1
    elif testData['changerange'][i] < 0 and float(pdt[i]) < 0.09:
        correct += 1

#calculate the result
#print 'The accuracy is', float(correct / float(len(testData))) * 100, 'percent'
Ejemplo n.º 8
0
	# testLabelArr = array(testLabelMat)
	# print targets[:10]
	y_train_pred = zeros(shape(labelArr)); y_preds = zeros(shape(testLabelArr))
	X_test = testDatArr; y_test = testLabelArr
	# clf = ELMClassifier(n_hidden=100, activation_func='gaussian', alpha=0.0, random_state=1)
	# clf = ELMClassifier(n_hidden=100 000, activation_func='hardlim', alpha=1.0, random_state=0)
	clf = ELMClassifier(n_hidden=100, activation_func='hardlim', alpha=1.0, random_state=1)
	for iter in range(2):
		# y_target = mat(labelArr)
		y_target = sign(mat(labelArr) - mat(y_train_pred));
		print y_target
		print y_train_pred
		# print shape(y_target)
		# iterate over classifiers
		clf.fit(datArr, list(array(y_target).reshape(-1)))
		y_train_pred = clf.predict(datArr)
		y_pred = clf.predict(testDatArr)
		acc = calcAccurary(sign(y_train_pred), labelArr)
		print "Training accurarcy:\n"
		print acc
		acc = calcAccurary(sign(y_pred), y_test)
		print acc
		y_preds = y_preds + LRATE * y_pred
	print mat(y_preds)
	acc = calcAccurary(sign(y_preds), y_test)
	# print y_preds
	print "Final score:\n"
	print acc
	"""
	for name, clf in zip(names, classifiers):
		clf.fit(X_train, y_train)