Exemplo n.º 1
0
sgd = SGD_SVM.SGDSVM(C=best_C,
                     ro=best_ro,
                     epochs=best_epoch,
                     W0=[0] * len(data[0]),
                     gamma0=best_g0)
sgd.fit(data, data_labels)

predictTrain = sgd.predict(data)
predictTest = sgd.predict(testset)

print("Accuracy for training set:")
print(Stat.Accuracy(predictTrain, data_labels))

print("F1 score for training set:")
print(Stat.F1_Score(predictTrain, data_labels))

print("Precision for training set:")
print(Stat.Precision(predictTrain, data_labels))

print("Recall for training set:")
print(Stat.Recall(predictTrain, data_labels))

print("Accuracy for test set")
print(Stat.Accuracy(predictTest, test_labels))

print("F1 score for test set")
print(Stat.F1_Score(predictTest, test_labels))

print("Precision for test set")
print(Stat.Precision(predictTest, test_labels))
###  

predictTrains = []
predictTests = []

dataAccuracy = []
testAccuracy = []

for i in range(5):
    sgd = SGD.SGD(r=best_r,epochs=best_epoch,W0=[0]*len(data[i][0]))
    sgd.fit(data[i],data_labels)
    
    predictTrains.append(sgd.predict(data[i]))
    predictTests.append(sgd.predict(testset[i]))
    
    dataAccuracy.append(Stat.F1_Score(sgd.predict(data[i]), data_labels))
    testAccuracy.append(Stat.F1_Score(sgd.predict(testset[i]), test_labels))

trainT = np.asarray(predictTrains).T.tolist()
testT = np.asarray(predictTests).T.tolist()

predictTrain = []
for i in range(len(data[0])):
    probPos = 0
    probNeg = 0
    for j in range(5):
        if predictTrains[j][i] == 1:
            probPos += dataAccuracy[j]
        else:
            probNeg += dataAccuracy[j]
    if probPos > probNeg:
Exemplo n.º 3
0
best_f1 = 0
for d in [10, 20, 30, 40, 50, float('inf')]:
    tmp = []

    dt = DecisionTree.DecisionTree(max_depth=d)
    kfold = KFold.KFold(n_splits=5)

    for kf in kfold.split(data):
        train = [data[i] for i in kf[0]]
        train_label = [data_labels[i] for i in kf[0]]
        test = [data[i] for i in kf[1]]
        test_label = [data_labels[i] for i in kf[1]]

        dt.fit(train, train_label)
        predict_tmp = dt.predict(test)
        tmp.append(Stat.F1_Score(predict_tmp, test_label))

    if np.mean(tmp) > best_f1:
        best_f1 = np.mean(tmp)
        best_depth = d

        print("Best result so far >>", best_f1, d)

print("best depth:", best_depth)

###

dt = DecisionTree.DecisionTree(max_depth=best_depth)
dt.fit(data, data_labels)

predictTrain = dt.predict(data)
Exemplo n.º 4
0
filename = 'ensembleResult/test' + '_ensemble_SVM_Perceptron'

predictTest = [
    1 if s == 1 and p == 1 else -1
    for p, s in zip(predict_labels_Perceptron, predict_labels_SVM)
]
predictEval = [
    1 if s == 1 and p == 1 else -1
    for p, s in zip(eval_labels_Perceptron, eval_labels_SVM)
]

print("Accuracy for test set")
print(Stat.Accuracy(predictTest, test_labels))

print("F1 score for test set")
print(Stat.F1_Score(predictTest, test_labels))

print("Precision for test set")
print(Stat.Precision(predictTest, test_labels))

print("Recall for test set")
print(Stat.Recall(predictTest, test_labels))

# filename = 'results/eval_Ensemble_SVM_Perceptron'
#
# with open(filename, 'wb') as thefile:
#     for item in predictEval:
#         thefile.write("%s\n" % item)
#
# print("eval is written!")