-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathconfusion_matrix.py
71 lines (61 loc) · 2.1 KB
/
confusion_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
from numpy.random import default_rng
import pymatviz as pmv
pmv.set_plotly_template("pymatviz_dark")
np_rng = default_rng(seed=0)
# %% Custom annotations for material stability with specific metrics
fig = pmv.confusion_matrix(
conf_mat=np.array([[0.7, 0.3], [0.15, 0.85]]),
x_labels=("Stable", "Unstable"),
y_labels=("Stable", "Unstable"),
normalize=False,
colorscale="Reds",
metrics={"Prec": ".2%", "F1": ".0%", "MCC": ".2f"},
)
fig.layout.xaxis.title = "Predicted Stability"
fig.layout.yaxis.title = "True Stability"
fig.show()
pmv.io.save_and_compress_svg(fig, "stability-confusion-matrix")
# %% Multi-class crystal system classification
n_samples = 300
crystal_systems = ["cubic", "hexagonal", "tetragonal", "orthorhombic"]
# Generate true labels with uneven class distribution
y_true = np_rng.choice(crystal_systems, n_samples, p=[0.4, 0.3, 0.2, 0.1])
# Simulate predictions with 75% accuracy and some systematic errors
y_pred = np.where(
np_rng.random(n_samples) < 0.75,
y_true, # correct predictions
np_rng.choice(crystal_systems, n_samples), # random errors
)
fig = pmv.confusion_matrix(
y_true=y_true,
y_pred=y_pred,
colorscale="Viridis",
)
fig.layout.xaxis.title = "Predicted System"
fig.layout.yaxis.title = "True System"
fig.layout.width = 650
fig.layout.height = 650
fig.show()
pmv.io.save_and_compress_svg(fig, "crystal-system-confusion-matrix")
# %% Binary classification passing in raw labels and custom class names
n_samples = 200
# Generate synthetic stability data
y_true = np_rng.choice([True, False], n_samples, p=[0.3, 0.7])
# Model predicts unstable more often than it should
y_pred = np.where(
np_rng.random(n_samples) < 0.8, # 80% accuracy
y_true,
np_rng.choice([True, False], n_samples, p=[0.4, 0.6]),
)
fig = pmv.confusion_matrix(
y_true=y_true,
y_pred=y_pred,
x_labels=("Stable", "Unstable"),
y_labels=("Stable", "Unstable"),
colorscale="RdBu",
)
fig.layout.xaxis.title = "Predicted Stability"
fig.layout.yaxis.title = "True Stability"
fig.show()
pmv.io.save_and_compress_svg(fig, "stability-confusion-matrix-raw")