如何绘制混淆矩阵与每个块中的图?

时间:2017-12-13 15:12:46

标签: python matplotlib scikit-learn seaborn

我训练了一个2级分类器,这里有tp(真阳性),tn(真阴性),fp(假阳性)和{{1每个时间步长都有(假阴性)。

fn

通常我使用上面一行数据来绘制混淆矩阵,例如,我使用数据的最后一行来绘制混淆矩阵。

a = 
[
    # tp tn fp fn
    (0, 5052, 0, 4948),
    (7, 5027, 9, 4957),
    (431, 4502, 428, 4639),
    (3412, 1630, 3296, 1662),
    (4906, 169, 4782, 143),
    (4971, 21, 4981, 27),
    (5001, 11, 4978, 10),
    (4894, 13, 5083, 10),
    (4902, 48, 4999, 51),
    (4867, 204, 4758, 171),
    (4256, 804, 4219, 721),
    (2930, 2252, 2780, 2038),
    (1375, 3794, 1200, 3631),
    (613, 4475, 446, 4466),
    (342, 4614, 295, 4749),
    (322, 4786, 241, 4651),
    (310, 4730, 258, 4702),
    (520, 4665, 399, 4416),
    (798, 4497, 532, 4173),
    (1229, 4109, 904, 3758),
    (1842, 3634, 1302, 3222),
    (2686, 2975, 2025, 2314),
    (3439, 2250, 2749, 1562),
    (3969, 1667, 3381, 983),
    (4223, 1442, 3548, 787),
    (4359, 1291, 3749, 601),
    (4379, 1325, 3686, 610),
    (4346, 1465, 3543, 646),
    (4330, 1751, 3201, 718),
    (4246, 1977, 2904, 873),
    (4179, 2119, 2809, 893),
    (4189, 2210, 2792, 809),
    (4135, 2330, 2714, 821),
    (4157, 2500, 2492, 851),
    (3969, 2799, 2268, 964),
    (3972, 2976, 2002, 1050),
    (3929, 3092, 1900, 1079),
    (3930, 3133, 1862, 1075),
    (3990, 3214, 1767, 1029),
    (4067, 3052, 1862, 1019),
    (4195, 3099, 1825, 881),
    (4215, 3059, 1976, 750),
    (4364, 2945, 1983, 708),
    (4382, 2898, 2083, 637),
    (4423, 2910, 2103, 564),
    (4475, 2865, 2075, 585),
    (4400, 2907, 2167, 526),
    (4450, 2980, 2000, 570),
    (4461, 2948, 2027, 564),
    (4362, 3074, 2004, 560),
    (4380, 3127, 1919, 574),
    (4361, 3095, 1924, 620),
    (4399, 3163, 1836, 602),
    (4326, 3242, 1737, 695),
    (4374, 3271, 1703, 652),
    (4280, 3305, 1717, 698),
    (4341, 3328, 1674, 657),
    (4265, 3318, 1729, 688),
    (4257, 3345, 1663, 735),
    (4323, 3346, 1634, 697),
    (4182, 3395, 1674, 749),
    (4211, 3354, 1645, 790),
    (4298, 3418, 1522, 762),
    (4261, 3360, 1574, 805),
    (4144, 3534, 1553, 769),
    (4112, 3491, 1506, 891),
    (4103, 3525, 1533, 839),
    (4155, 3605, 1440, 800),
    (4115, 3531, 1467, 887),
    (4103, 3540, 1434, 923),
    (4054, 3597, 1456, 893),
    (4096, 3614, 1378, 912),
    (4058, 3646, 1365, 931),
    (4124, 3622, 1391, 863),
    (4143, 3633, 1364, 860),
    (4015, 3665, 1445, 875),
    (3992, 3714, 1402, 892),
    (4045, 3677, 1359, 919),
    (4181, 3645, 1255, 919),
    (4090, 3665, 1324, 921),
    (4076, 3719, 1268, 937),
    (4033, 3686, 1308, 973),
    (4102, 3667, 1314, 917),
    (4097, 3674, 1291, 938),
    (4153, 3633, 1314, 900),
    (4049, 3691, 1287, 973),
    (4072, 3763, 1318, 847),
    (4110, 3740, 1279, 871),
    (4100, 3796, 1214, 890),
    (4107, 3716, 1272, 905),
    (4121, 3819, 1189, 871),
    (4179, 3705, 1206, 910),
    (4170, 3770, 1201, 859),
    (4101, 3760, 1263, 876),
    (4078, 3829, 1190, 903),
    (4078, 3818, 1220, 884),
    (4168, 3759, 1181, 892),
    (4160, 3734, 1237, 869),
    (4214, 3721, 1170, 895),
    (4140, 3786, 1251, 823),
    (4282, 3660, 1233, 825),
    (4208, 3719, 1269, 804),
    (4198, 3785, 1247, 770),
    (4206, 3780, 1225, 789),
    (4220, 3763, 1190, 827),
    (4198, 3804, 1181, 817),
    (4144, 3820, 1137, 899),
    (4189, 3835, 1147, 829),
    (4158, 3910, 1149, 783),
    (4225, 3768, 1185, 822),
    (4219, 3836, 1177, 768),
    (4243, 3812, 1183, 762),
    (4159, 3822, 1201, 818),
    (4274, 3720, 1236, 770),
    (4186, 3817, 1232, 765),
    (4154, 3813, 1241, 792),
    (4211, 3817, 1201, 771),
    (4275, 3800, 1179, 746),
    (4228, 3784, 1182, 806),
    (4217, 3781, 1177, 825),
    (4243, 3815, 1153, 789),
    (4173, 3918, 1140, 769),
    (4259, 3836, 1158, 747),
    (4208, 3859, 1205, 728),
    (4265, 3831, 1184, 720),
    (4281, 3842, 1156, 721),
    (4247, 3771, 1196, 786),
    (4164, 3881, 1196, 759),
    (4263, 3865, 1093, 779),
    (4184, 3894, 1180, 742),
    (4217, 3889, 1120, 774),
    (4358, 3835, 1108, 699),
    (4211, 3866, 1224, 699),
    (4365, 3746, 1221, 668),
    (4259, 3836, 1191, 714),
    (4228, 3852, 1164, 756),
    (4366, 3830, 1072, 732),
    (4276, 3877, 1123, 724),
    (4355, 3790, 1151, 704),
    (4381, 3829, 1068, 722),
    (4274, 3870, 1198, 658),
    (4345, 3856, 1146, 653),
    (4357, 3780, 1181, 682),
    (4402, 3798, 1140, 660),
    (4323, 3881, 1135, 661),
    (4414, 3738, 1141, 707),
    (4268, 3837, 1190, 705),
    (4328, 3831, 1151, 690),
    (4356, 3736, 1149, 759),
    (4389, 3836, 1070, 705),
    (4323, 3832, 1191, 654),
    (4253, 3897, 1193, 657),
    (4375, 3822, 1137, 666),
    (4290, 3895, 1118, 697),
    (4280, 3864, 1126, 730),
    (4258, 3943, 1110, 689),
    (4289, 3888, 1156, 667),
    (4233, 3966, 1133, 668),
    (4353, 3824, 1123, 700),
    (4337, 3832, 1129, 702),
    (4322, 3918, 1072, 688),
    (4362, 3891, 1090, 657),
    (4329, 3847, 1155, 669),
    (4328, 3915, 1121, 636),
    (4315, 3962, 1118, 605),
    (4381, 3830, 1131, 658),
    (4202, 3906, 1221, 671),
    (4368, 3819, 1118, 695),
    (4393, 3770, 1161, 676)
]

输出是。

enter image description here

我的问题是如何绘制混淆矩阵,其中每个块都是绘制的,例如在# (4393, 3770, 1161, 676) In [325]: import seaborn as sn In [326]: import pandas as pd In [327]: import matplotlib.pyplot as plt In [328]: array = [[4393, 676], [1161, 3770]] In [335]: df_cm = pd.DataFrame(array, ['POS', 'NEG'], ['POS', 'NEG']) In [338]: sns.heatmap(df_cm, annot=True, annot_kws={'size':16}, fmt='g') Out[338]: <matplotlib.axes._subplots.AxesSubplot at 0x2319899cac8> In [339]: plt.show() 块(POS,POS)中,每个时间步都有tp个值的图。

tp

enter image description here

如何将上述图添加到混淆矩阵中?谢谢!

0 个答案:

没有答案