如何通过 python 中的热图减法来显示两个矩阵的差异?
How can display differences of two matrices by subtraction via heatmap in python?
我有两个矩阵 [A]
(Expected_matrice) , [B]
(Predicted_matrice) 我需要创建第三个 [C]
(Error_matrice) 通过减去它们 [C]=[A]-[B]
并将其传递给 Pandas 数据框并保存在 csv 文件中。
由于上述矩阵的大小是 24*20,我尝试:
首先:情节 [C]
通过 sns.heatmap(C, cbar=True, cmap="gray_gist")
第二:评估 [C] 通过应用 sum(abs(abs([A])-abs([B])))/24*20
来检查它有多好。事实上,我通过它计算了 Error 的数量。我也知道可以通过应用来使用另一种方法,例如 MSE from Sklearn import metrics
:
from Sklearn import metrics
print(metrics.mean_squared_error(A,B))
因为矩阵的元素是列表我用过:[i - j for (i, j) in zip(A,B)]
我的代码如下:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.ndimage.filters import gaussian_filter
#A,B can be read from .csv files
A = pd.read_csv('D:\A.csv', header=None)
B = pd.read_csv('D:\B.csv', header=None)
#A,B can be assumed as lists
df_A = pd.DataFrame(A)
df_B = pd.DataFrame(B)
#calculate error matrix via subtraction
C = [i - j for (i, j) in zip(A,B)]
#Pass error matrix to Pandas dataframe
df_C = pd.DataFrame(C)
df_C.to_csv('Error_Matrix.csv', header=None, index=None)
#Evaluation
Eval= sum(abs(abs([A])-abs([B])))/24*20
Eval_ = '{:04}'.format(Eval)
print(Eval_)
#Plotting C
fig, ax = plt.subplots(nrows=1, ncols=2 , figsize=(20,15))
plt.subplot(1,2,1)
ax = sns.heatmap(C, cbar=True, cmap="gist_gray")
plt.title(f'Error Matrix Error={Eval_}', fontsize=14 , fontweight='bold', color='black', loc='center', style='italic')
plt.axis('off')
plt.subplot(1,2,2)
C_smooth = gaussian_filter(dfr_b, sigma=1)
ax = sns.heatmap(C_smooth, vmin=np.min(C_smooth), vmax=np.max(C_smooth), cmap ="gray" , cbar=True , cbar_kws={"ticks":[0,33,67,100]})
plt.title(f'Error Matrix Smooth Error={Eval_}', fontsize=14 , fontweight='bold', color='black', loc='center', style='italic')
plt.axis('off')
plt.savefig('Error_Matrix.png')
plt.show()
预期结果:
注意 白色表示 Error=0,纯黑色表示 Error=maximum。
大部分应该是错误的,但我不想得到黑色结果,我主要希望灰色。
我会这样做:
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()
给予:
>> 219.40945851846487 1.0970472925923245
不过我不得不说,我不喜欢丢失有关偏差符号的信息。因此,左下方的图表是我的实际偏好。它可以像上一个一样缩放和移动,使零变为 100%,数据范围从 80% 到 120% 或类似的范围。
我有两个矩阵 [A]
(Expected_matrice) , [B]
(Predicted_matrice) 我需要创建第三个 [C]
(Error_matrice) 通过减去它们 [C]=[A]-[B]
并将其传递给 Pandas 数据框并保存在 csv 文件中。
由于上述矩阵的大小是 24*20,我尝试:
首先:情节 [C]
通过 sns.heatmap(C, cbar=True, cmap="gray_gist")
第二:评估 [C] 通过应用 sum(abs(abs([A])-abs([B])))/24*20
来检查它有多好。事实上,我通过它计算了 Error 的数量。我也知道可以通过应用来使用另一种方法,例如 MSE from Sklearn import metrics
:
from Sklearn import metrics
print(metrics.mean_squared_error(A,B))
因为矩阵的元素是列表我用过:[i - j for (i, j) in zip(A,B)]
我的代码如下:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from scipy.ndimage.filters import gaussian_filter
#A,B can be read from .csv files
A = pd.read_csv('D:\A.csv', header=None)
B = pd.read_csv('D:\B.csv', header=None)
#A,B can be assumed as lists
df_A = pd.DataFrame(A)
df_B = pd.DataFrame(B)
#calculate error matrix via subtraction
C = [i - j for (i, j) in zip(A,B)]
#Pass error matrix to Pandas dataframe
df_C = pd.DataFrame(C)
df_C.to_csv('Error_Matrix.csv', header=None, index=None)
#Evaluation
Eval= sum(abs(abs([A])-abs([B])))/24*20
Eval_ = '{:04}'.format(Eval)
print(Eval_)
#Plotting C
fig, ax = plt.subplots(nrows=1, ncols=2 , figsize=(20,15))
plt.subplot(1,2,1)
ax = sns.heatmap(C, cbar=True, cmap="gist_gray")
plt.title(f'Error Matrix Error={Eval_}', fontsize=14 , fontweight='bold', color='black', loc='center', style='italic')
plt.axis('off')
plt.subplot(1,2,2)
C_smooth = gaussian_filter(dfr_b, sigma=1)
ax = sns.heatmap(C_smooth, vmin=np.min(C_smooth), vmax=np.max(C_smooth), cmap ="gray" , cbar=True , cbar_kws={"ticks":[0,33,67,100]})
plt.title(f'Error Matrix Smooth Error={Eval_}', fontsize=14 , fontweight='bold', color='black', loc='center', style='italic')
plt.axis('off')
plt.savefig('Error_Matrix.png')
plt.show()
预期结果:
注意 白色表示 Error=0,纯黑色表示 Error=maximum。 大部分应该是错误的,但我不想得到黑色结果,我主要希望灰色。
我会这样做:
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()
给予:
>> 219.40945851846487 1.0970472925923245
不过我不得不说,我不喜欢丢失有关偏差符号的信息。因此,左下方的图表是我的实际偏好。它可以像上一个一样缩放和移动,使零变为 100%,数据范围从 80% 到 120% 或类似的范围。