Commit f636a1b0 authored by Leonie Pick's avatar Leonie Pick

Show both CM normalizations; Draw tolerance in calibration plot; Make...

Show both CM normalizations; Draw tolerance in calibration plot; Make background for driver histograms transparent
parent 308e6c52
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -858,8 +858,8 @@ def Assess_Model(Data,Target,N2,K2,modelTest0,n_classes,Plot,Save,SaveName):
scores_mean_train = np.nanmean(Model_Mean[[11,15,18,19,20],1,:],axis=0)
decision_boundary_all = int(thresholds[np.nanargmax(scores_mean)]*100)
decision_boundary_all_train = int(thresholds[np.nanargmax(scores_mean_train)]*100)
#decision_boundary = 50
decision_boundary = decision_boundary_all
decision_boundary = 50
#decision_boundary = decision_boundary_all
#print('best MCC:',np.nanmax(Model_Mean[19,0,:]))
print('best decision test:',thresholds[decision_boundary_MCC], thresholds[decision_boundary_all])
......
......@@ -346,10 +346,10 @@ def ModelCounts(nModels,SumCounts,CV2_means,N1,K1,N2,K2,Param_Comb,BestID,scorin
def CM(CM,Condition,n_classes,Save,SaveName):
CM = np.reshape(CM,(n_classes,n_classes))
CM_names = ['TNR','FPR','FNR','TPR']
#CM_names = ['NPV','FDR','FOR','PPV']
CM_names_row = ['TNR','FPR','FNR','TPR']
CM_names_col = ['NPV','FDR','FOR','PPV']
#CM_names = ['TN','FP','FN','TP']
CM_names = np.asarray(CM_names).reshape((2,2))
#CM_names = np.asarray(CM_names).reshape((2,2))
classes = range(n_classes)
class_names = ['0: C/SIR', '1: ICME']
......@@ -357,40 +357,49 @@ def CM(CM,Condition,n_classes,Save,SaveName):
#print(Condition)
print(CM)
#Normalization
for i in range(n_classes):
CM[i,:] /= Condition[i,0] #row-normalization
#CM[:,i] /= Condition[i,1] #column-normalization
#class_names.append(np.str(i))
fig, ax = plt.subplots(1,1,facecolor='w',edgecolor='k',sharey=True)
fig.set_size_inches([4,4], forward=True)
fig.subplots_adjust(bottom=0.01,top=0.99,left=0.2,right=0.9,wspace=0.0,hspace=0.0)
cmap=plt.cm.Blues
Im = ax.imshow(CM, interpolation='nearest', cmap=cmap)
CBar = plt.colorbar(Im,shrink=0.66)
thresh = CM.max()/2.
for i, j in itertools.product(range(CM.shape[0]), range(CM.shape[1])):
ax.text(j, i, CM_names[i,j], horizontalalignment='center', verticalalignment = 'bottom', color='white' if CM[i, j] > thresh else "black", fontsize=16)
ax.text(j, i, format(CM[i, j], '.3f'), horizontalalignment='center', verticalalignment = 'top', color='white' if CM[i, j] > thresh else "black", fontsize=16)
#ax.set_title('Confusion matrix')
ax.set_xticks(classes); ax.set_yticks(classes)
ax.set_xticklabels(class_names); ax.set_yticklabels(class_names,rotation=90)
ax.set_xlabel('Predicted class',fontsize=18); ax.set_ylabel('True class',fontsize=18)
ax.tick_params(axis ='x',which='both',direction='inout',labelsize=16)
ax.tick_params(axis ='y',which='both',direction='inout',labelsize=16)
CBar.set_label('Normalized count',fontsize=18)
CBar.ax.tick_params(axis ='y',which='both',direction='inout',labelsize=16)
ax.grid(False)
for j in range(2):
#Normalization
CMnorm = np.copy(CM)
for i in range(n_classes):
if j == 0:
CM_names = np.asarray(CM_names_row).reshape((2,2))
Name ='ConfusionRow'
CMnorm[i,:] /= Condition[i,0] #row-normalization
else:
CM_names = np.asarray(CM_names_col).reshape((2,2))
Name ='ConfusionCol'
CMnorm[:,i] /= Condition[i,1] #column-normalization
#class_names.append(np.str(i))
fig, ax = plt.subplots(1,1,facecolor='w',edgecolor='k',sharey=True)
fig.set_size_inches([4,4], forward=True)
fig.subplots_adjust(bottom=0.01,top=0.99,left=0.2,right=0.9,wspace=0.0,hspace=0.0)
cmap=plt.cm.Blues
Im = ax.imshow(CMnorm, interpolation='nearest', cmap=cmap)
CBar = plt.colorbar(Im,shrink=0.66)
thresh = CMnorm.max()/2.
for i, j in itertools.product(range(CMnorm.shape[0]), range(CMnorm.shape[1])):
ax.text(j, i, CM_names[i,j], horizontalalignment='center', verticalalignment = 'bottom', color='white' if CMnorm[i, j] > thresh else "black", fontsize=16)
ax.text(j, i, format(CMnorm[i, j], '.3f'), horizontalalignment='center', verticalalignment = 'top', color='white' if CMnorm[i, j] > thresh else "black", fontsize=16)
#ax.set_title('Confusion matrix')
ax.set_xticks(classes); ax.set_yticks(classes)
ax.set_xticklabels(class_names); ax.set_yticklabels(class_names,rotation=90)
ax.set_xlabel('Predicted class',fontsize=18); ax.set_ylabel('True class',fontsize=18)
ax.tick_params(axis ='x',which='both',direction='inout',labelsize=16)
ax.tick_params(axis ='y',which='both',direction='inout',labelsize=16)
CBar.set_label('Normalized count',fontsize=18)
CBar.ax.tick_params(axis ='y',which='both',direction='inout',labelsize=16)
ax.grid(False)
if Save == True:
fig.savefig('./Dump/Fig/development/ConfusionRow_'+SaveName+'.pdf',format='pdf',dpi=300,transparent=True)
#fig.savefig('./Dump/Fig/ConfusionCol.png',format='png',dpi=300,transparent=True)
if Save == True:
fig.savefig('./Dump/Fig/development/'+Name+'_'+SaveName+'.pdf',format='pdf',dpi=200,transparent=True)
#fig.savefig('./Dump/Fig/ConfusionCol.png',format='png',dpi=300,transparent=True)
plt.show()
plt.show()
###
###
#def Curves(N2,K2,Curves,curve_i,Scorers,Model_Mean,Model_Std,C,Save,SaveName):
......@@ -521,7 +530,8 @@ def Calibration(Calib,bin_edges,Save,SaveName):
Calib_outer = np.nanmean(Calib_inner,axis=0)
Calib_outer_std = np.nanstd(Calib_inner,axis=0)
axs[0].plot(np.linspace(0,1,11),np.linspace(0,1,11),linestyle='--',color='gray',label=r'perfectly calibrated')
axs[0].fill_between(np.linspace(0,1,11),np.linspace(-0.1,0.9,11),np.linspace(0.1,1.1,11),color='gray',alpha=0.3,zorder=0)
axs[0].plot(np.linspace(0,1,11),np.linspace(0,1,11),linestyle='--',color='gray',label=r'perfectly calibrated $\pm$ 0.1')
axs[0].errorbar(Calib_outer[1,:],Calib_outer[0,:],xerr=Calib_outer_std[1,:],yerr=Calib_outer_std[0,:],ecolor='black', label=r'reliability curve $\pm$ std')
#axs[0].plot(Calib_all[1,:],Calib_all[0,:],color='red')
axs[1].bar(bin_edges[0:-1],height=Calib_outer[2,:],width=bin_edges[1]-bin_edges[0],align='edge',yerr=Calib_outer_std[2,:],color='darkgray',edgecolor='black',linewidth=0.5)
......@@ -867,17 +877,14 @@ def MusicalClassified(Time,HTime,Reference,Source,Storms,Class_Proba,Class,Kp_al
################################################
# Plots class occurrences
#n, bins = np.histogram(HTime[Storms,4],np.arange(1900.5,2017.5,1))
n, bins = np.histogram(HTime[Storms,4],np.arange(1900,2017,1))
start = 1; step=0.1; loops = 1./step; nip = 0
for i in range(int(loops)):
#zi = np.where(np.logical_and(z>start-step,z<=start))[0]
if i == 0:
zi = np.where(np.logical_and(z>=start-step,z<=start))[0]
else:
zi = np.where(np.logical_and(z>=start-step,z<start))[0]
#ni, bins = np.histogram(HTime[Storms[zi],4],np.arange(1900.5,2017.5,1))
ni, bins = np.histogram(HTime[Storms[zi],4],np.arange(1900,2017,1))
axs2.barh(np.arange(1900.5,2016.5,1), (ni*100/n)/100, left = nip, height=0.8, color=cm(1-(i*step)-step/2)) #left = (nip*100/n)/100
start -= step
......@@ -943,8 +950,8 @@ def MusicalClassified(Time,HTime,Reference,Source,Storms,Class_Proba,Class,Kp_al
fig.savefig('./Dump/Fig/development/StormsClassified_'+SaveName+'.pdf',format='pdf',dpi=200,bbox_inches='tight',pad_inches=0.01)#,transparent=True)
fig.savefig('./Dump/Fig/development/StormsClassified_'+SaveName+'.png',format='png',dpi=200,bbox_inches='tight',pad_inches=0.01)#,transparent=True)
fig2.savefig('./Dump/Fig/development/StormsHisto_'+SaveName+'.pdf',format='pdf',dpi=200,bbox_inches='tight',pad_inches=0.01)#,transparent=True)
fig2.savefig('./Dump/Fig/development/StormsHisto_'+SaveName+'.png',format='png',dpi=200,bbox_inches='tight',pad_inches=0.01)#,transparent=True)
fig2.savefig('./Dump/Fig/development/StormsHisto_'+SaveName+'.pdf',format='pdf',dpi=200,bbox_inches='tight',pad_inches=0.01,transparent=True)
fig2.savefig('./Dump/Fig/development/StormsHisto_'+SaveName+'.png',format='png',dpi=200,bbox_inches='tight',pad_inches=0.01,transparent=True)
plt.show()
###
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment