Exemplo n.º 1
0
    def test_accuracy(self):
        expected_accuracy = 0.5

        cba = CBA(algorithm="m2")

        test_dataframe = pd.read_csv(dataset_file, sep=",")

        transactions = TransactionDB.from_DataFrame(test_dataframe)
        transactions_test = TransactionDB.from_DataFrame(test_dataframe[:2])

        cba.fit(transactions)

        accuracy = cba.rule_model_accuracy(transactions_test)

        self.assertAlmostEqual(accuracy, expected_accuracy, places=3)
def train_model(data, support, confidence, rule_length):
    print('Mining rules...')
    headers = data.columns
    data = TransactionDB.from_DataFrame(data)

    cba = CBA(support=support,
              confidence=confidence,
              maxlen=rule_length,
              algorithm="m1")
    cba.fit(data)
    accuracy = cba.rule_model_accuracy(data)

    print('Determining semantic coherence...')
    semantics.precompute_attributes(headers)

    for rule in cba.clf.rules:
        antecedent = list(rule.antecedent.itemset.keys())
        coherence = semantics.get_rule_coherence(antecedent)
        mined_rules.append((f'{rule_to_em_rule(rule)}', rule.confidence,
                            coherence, rule.consequent.value))

    print_model()
    print(f'Model accuracy: {round(accuracy * 100, 2)} %')
    print(f'Model support: {cba.support}')
Exemplo n.º 3
0
import pandas as pd
from pyarc import CBA, TransactionDB
from sklearn.model_selection import train_test_split

print("")
print("Rule Generation")
data = pd.read_csv('total_data.csv')  #disc_data/Known_attack_data
print("Data Size : %s" % str(data.shape))

# 데이터 분할
train, test = train_test_split(data, test_size=0.2, random_state=123)

txns_train = TransactionDB.from_DataFrame(train, target="class")
txns_test = TransactionDB.from_DataFrame(test)

print("Association Rule Generation")
cba = CBA(support=0.1)
cba.fit(txns_train)
print(cba.fit(txns_train))

print("\nRULES : ({})".format(len(cba.clf.rules)))
for i in cba.clf.rules:
    print(i)

accuracy = cba.rule_model_accuracy(txns_test)
print("")
print(accuracy)