def predict_test_data( test_set, discovered_rules ):
correct_predictions = 0
class_attr = test_set.class_attr
classes = test_set.attr_vals[class_attr]
confusion_matrix = {}.fromkeys(classes)
for clas in classes:
confusion_matrix[clas] = {}.fromkeys(classes, 0)
for row in test_set.data:
# Iterate through rules until one of the discovered rules fits the case
for rule in discovered_rules:
rule_attrs = rule.antecedent.keys()
is_case = True
for attr in rule_attrs:
if row[test_set.attr_idxs[attr]] != rule.antecedent[attr]:
is_case = False
break
# Make and check prediction
if is_case and rule.consequent == row[test_set.attr_idxs[class_attr]]:
correct_predictions += 1
confusion_matrix[rule.consequent][row[test_set.attr_idxs[class_attr]]] += 1
break
incorrect_predictions = len(test_set.data) - correct_predictions
accuracy = correct_predictions / len(test_set.data)
print("Correct classifcations: %d" % correct_predictions)
print("Incorrect classifcations: %d" % incorrect_predictions)
print("Accuracy = %f" % accuracy)
return accuracy
Этот код относится к оптимизации Ant Ant Miner Colony. Я хочу рассчитать F1-счет. Но я не понимаю, как рассчитать F1 балл в этом разделе.