How Can Display Differences Of Two Matrices By Subtraction Via Heatmap In Python?
I have two matrices [A](Expected_matrice) , [B](Predicted_matrice) I need to create the third one [C](Error_matrice) via subtraction of them [C]=[A]-[B] and pass it to Pandas dataf
Solution 1:
I'd do something like this:
import matplotlib.pyplot as plt
import numpy as np
mx = 10 + 3 * np.random.random( 20 * 10 )
mx = mx.reshape( 20, 10 )
nx = 10 + 3 * np.random.random( 20 * 10 )
nx = nx.reshape( 20, 10 )
deltax = mx - nx
ox = 100 * ( 1 - np.abs( ( deltax) / mx ) )
scale = max( [ abs(min( np.concatenate( deltax ) ) ), abs( max( np.concatenate( deltax ) ) ) ] )
chi2 = np.sum( mx - nx )**2
chi2Red = chi2/( len( mx ) * len( mx[0] ) )
print chi2, chi2Red
fig = plt.figure()
ax = fig.add_subplot( 2, 2, 1 )
bx = fig.add_subplot( 2, 2, 2 )
cx = fig.add_subplot( 2, 2, 3 )
dx = fig.add_subplot( 2, 2, 4 )
MX1 = ax.matshow( mx, vmin=0, vmax=30 )
MX2 = bx.matshow( nx, vmin=0, vmax=30 )
diffMX = cx.matshow( deltax, cmap='seismic', vmin=-scale, vmax=scale )
errMX = dx.matshow( ox, vmin=0, vmax=100 )
plt.colorbar( MX1, ax=ax )
plt.colorbar( MX2, ax=bx )
plt.colorbar( diffMX, ax=cx )
plt.colorbar( errMX, ax=dx )
plt.show()
giving:
>>219.409458518464871.0970472925923245
I have to say though, that I do not like loosing the information about the sign of deviation. The lower left graph, hence, would be my actual preference. It could be scaled and shifted like the last one such that zero becomes 100% and the data will range from 80% to 120% or somewhat like that.
Post a Comment for "How Can Display Differences Of Two Matrices By Subtraction Via Heatmap In Python?"