Checking that the notebook is running on Google Colab or not.
import sys
try:
import google.colab
!{sys.executable} -m pip -q -q install pycm
except:
pass
!{sys.executable} -m pip -q -q install matplotlib;
import numpy as np
import matplotlib.pyplot as plt
import pycm
def plot_ci(cm, param, alpha=0.05, method="normal-approx"):
"""
Plot two-sided confidence interval.
:param cm: ConfusionMatrix
:type cm : pycm.ConfusionMatrix object
:param param: input parameter
:type param: str
:param alpha: type I error
:type alpha: float
:param method: binomial confidence intervals method
:type method: str
:return: None
"""
conf_str = str(round(100 * (1-alpha)))
print(conf_str + "%CI :")
if param in cm.class_stat.keys():
mean = []
error = [[], []]
data = cm.CI(param, alpha=alpha, binom_method=method)
class_names_str = list(map(str,(cm.classes)))
for class_index, class_name in enumerate(cm.classes):
print(str(class_name) +" : " + str(data[class_name][1]))
mean.append(cm.class_stat[param][class_name])
error[0].append(cm.class_stat[param][class_name] - data[class_name][1][0])
error[1].append(data[class_name][1][1] - cm.class_stat[param][class_name])
fig = plt.figure()
plt.errorbar(mean, class_names_str, xerr=error, fmt='o', capsize=5, linestyle="dotted")
plt.ylabel('Class')
fig.suptitle("Param :" + param + ", Alpha:" + str(alpha), fontsize=16)
for index,value in enumerate(mean):
down_point = data[cm.classes[index]][1][0]
up_point = data[cm.classes[index]][1][1]
plt.text(value, class_names_str[index], "%f" % value, ha="center", va="top", color="red")
plt.text(down_point, class_names_str[index], "%f" % down_point, ha="right", va="bottom", color="red")
plt.text(up_point , class_names_str[index], "%f" % up_point, ha="left", va="bottom", color="red")
else:
mean = cm.overall_stat[param]
data = cm.CI(param, alpha=alpha, binom_method=method)
print(data[1])
error = [[], []]
up_point = data[1][1]
down_point = data[1][0]
error[0] = [cm.overall_stat[param] - down_point]
error[1] = [up_point - cm.overall_stat[param]]
fig = plt.figure()
plt.errorbar(mean, [param], xerr=error, fmt='o',capsize=5, linestyle="dotted")
fig.suptitle("Alpha:" + str(alpha), fontsize=16)
plt.text(mean, param, "%f" % mean, ha="center", va="top", color="red")
plt.text(down_point, param, "%f" % down_point, ha="right", va="bottom", color="red")
plt.text(up_point, param, "%f" % up_point, ha="left", va="bottom", color="red")
plt.show()
cm = pycm.ConfusionMatrix(matrix={0:{0:13,1:2,2:5},1:{0:1,1:10,2:6},2:{0:2,1:0,2:9}})
plot_ci(cm,param="TPR",method="normal-approx")
plot_ci(cm,param="TPR",method="wilson")
plot_ci(cm,param="TPR",method="agresti-coull")
plot_ci(cm,param="FPR",method="normal-approx")
plot_ci(cm,param="FPR",method="wilson")
plot_ci(cm,param="FPR",method="agresti-coull")
plot_ci(cm,param="AUC")
plot_ci(cm,param="PLR")
plot_ci(cm,param="Overall ACC")
plot_ci(cm,param="Kappa")