|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
from sklearn.metrics import confusion_matrix, cohen_kappa_score |
|
import numpy as np |
|
|
|
def plot_confusion_matrix(conf_matrix, labels, title, filename): |
|
plt.figure(figsize=(12, 10)) |
|
sns.set(font_scale=0.7) |
|
sns.heatmap(conf_matrix, annot=True, fmt=".2f", cmap="Blues", |
|
cbar_kws={'label': '%'}) |
|
plt.xlabel('predict') |
|
plt.ylabel('true') |
|
plt.title(title) |
|
plt.xticks(ticks=np.arange(len(labels)) + 0.5, labels=labels, rotation=45, ha='right') |
|
plt.yticks(ticks=np.arange(len(labels)) + 0.5, labels=labels, rotation=0) |
|
plt.tight_layout() |
|
|
|
|
|
plt.savefig(filename, format='png') |
|
|
|
|
|
plt.show() |
|
|
|
def process_csv(file_path, title, filename): |
|
df = pd.read_csv(file_path) |
|
y_true = df['expected'].astype(str) |
|
y_pred = df['predicted'].astype(str) |
|
|
|
labels = sorted(set(y_true) | set(y_pred)) |
|
conf_matrix = confusion_matrix(y_true, y_pred, labels=labels, normalize='true') * 100 |
|
|
|
kappa = cohen_kappa_score(y_true, y_pred) |
|
|
|
print(f"{title} Kappa 系数: {kappa:.2f}") |
|
plot_confusion_matrix(conf_matrix, labels, title, filename) |
|
|
|
first_level_results_file = 'first_level_results_true.csv' |
|
second_level_results_file = 'second_level_results_true.csv' |
|
|
|
first_level_image_file = 'first_level_confusion_matrix.png' |
|
second_level_image_file = 'second_level_confusion_matrix.png' |
|
|
|
process_csv(first_level_results_file, 'FirstLevel', first_level_image_file) |
|
process_csv(second_level_results_file, 'SecondLevel', second_level_image_file) |
|
|