Beispiel #1
0
def test_model(fnn_model):
    org_data, org_label = LoadData.get_method2_test()
    X_train, X_test, y_train, y_test = train_test_split(org_data,
                                                        org_label,
                                                        test_size=0.3)

    # Convert y_test(28 category to 6 category)
    y_test = np.array([int(e[1:2]) for e in y_test])

    print('<---Test Model Start--->')
    output_list = np.array([])
    for model in fnn_model:
        fnn = FNN(fnn_input_size, fnn_membership_size, fnn_rule_size,
                  fnn_output_size, model.mean, model.stddev, model.weight,
                  fnn_lr, 1)
        output = fnn.testing_model(X_test)
        output_list = np.append(output_list, output)

    # y_label = label_convert(y_test, build_hash_table())
    output_list = output_list.reshape(-1, len(fnn_model))

    # 不再做正規畫了試試
    output_list = Normalize.normalization(output_list)

    label_pred, count = label_encoding(output_list, build_hash_table())
    # cnt = 0
    # for x, y in zip(output_list[0:10], y_test[0:10]):
    #     print(x, ' ', y, ' ', label_pred[cnt])
    #     cnt += 1

    for x, y in zip(y_test, label_pred):
        print('correct', x, '<->', 'predict', y)

    cnf_matrix = confusion_matrix(y_test, label_pred)
    # 做confusion matrix 的圖
    # plt.figure()
    # ConfusionMatrix.plot_confusion_matrix(cnf_matrix, classes=list(set(y_test)),
    #                       title='Confusion matrix(Final FNN Model)')

    cnf_accuracy = np.sum(cnf_matrix.diagonal()) / np.sum(cnf_matrix)

    print('FinalModel_Accuracy: ', accuracy_score(y_test, label_pred))

    print('This is the confusion matrix(test_all_model)\n', cnf_matrix)
    # print(C_matrix)
    # print(C_accuracy)

    print('<---Test Model Successfully--->')
    print('<----------------------------------------------->')
    return cnf_accuracy, count
Beispiel #2
0
fnn_label_size = 6
fnn_input_size = 3
fnn_membership_size = fnn_input_size * fnn_label_size
fnn_rule_size = 6
fnn_output_size = 1
fnn_lr = 0.001
fnn_epoch = 1
fnn_random_size = 1

fnn_threshold = [
    0.2, 0.0, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.1, 0.1, 0.0, 0.2, 0.1, 0.1, 0.0,
    0.2, 0.0, 0.1, 0.1, 0.2, 0.1, 0.0, 0.2, 0.2, 0.2, 0.1, 0.2, 0.2, 0.1, 0.1
]

# Load the Test data
org_data, org_label = LoadData.get_method2_test()
# print('org_data.shape', org_data.shape)
# print('org_label.shape', org_label)

output_array = np.array([])

# Load the test data, forward, store
for nn in nn_category:
    print('nn -> ', nn)
    rel_path = '../Experiment/Method2/FNNModel/FNN/' + str(nn) + '.json'
    abs_path = os.path.join(os.path.dirname(__file__), rel_path)
    attribute = LoadData.load_fnn_weight(abs_path)
    # print(attribute)
    mean = np.asarray(attribute['Mean'])
    stddev = np.asarray(attribute['Stddev'])
    weight = np.asarray(attribute['Weight'])