This repository has been archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.xml
399 lines (312 loc) · 110 KB
/
feed.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="4.2.0">Jekyll</generator><link href="https://blog.heaplinker.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://blog.heaplinker.com/" rel="alternate" type="text/html" hreflang="en" /><updated>2021-07-03T00:04:00+06:00</updated><id>https://blog.heaplinker.com/feed.xml</id><title type="html">Heaplinker</title><subtitle>Heaplinker Blog is a platform that spreads multilingual digital writings with the motto of "think and make". It has been made in a very similar way to Medium. Just as Medium is an open platform of writings on many subjects, here Heaplinker Blog is also a platform where contains a variety of topics, especially technical ones for digital publishing. Special emphasis will be given on technical issues here. Its main purpose is to spread ideas, information, and deepen understanding of technical knowledge, and to publish posts in both English and Bengali language.</subtitle><entry><title type="html">How to make a custom confusion matrix on matplotlib with heatmap and annotation</title><link href="https://blog.heaplinker.com/How-to-make-a-custom-confusion-matrix-on-matplotlib-with-heatmap-and-annotation-2021070218326405004/" rel="alternate" type="text/html" title="How to make a custom confusion matrix on matplotlib with heatmap and annotation" /><published>2021-07-02T04:50:40+06:00</published><updated>2021-07-02T04:50:40+06:00</updated><id>https://blog.heaplinker.com/How-to-make-a-custom-confusion-matrix-on-matplotlib-with-heatmap-and-annotation</id><content type="html" xml:base="https://blog.heaplinker.com/How-to-make-a-custom-confusion-matrix-on-matplotlib-with-heatmap-and-annotation-2021070218326405004/"><p>A confusion matrix is also known as an <strong>error matrix</strong> or <strong>matching matrix</strong><sup><a href="https://en.wikipedia.org/wiki/Confusion_matrix">1</a></sup>. Each row and each column of this matrix represents the actual class and the predicted class respectively. Here in this post, I have discussed how we can build a custom confusion matrix for multi-class classification with <code class="language-plaintext highlighter-rouge">matplotlib</code> in python. Here is the post outline:</p>
<ul>
<li>Characteristics of the custom confusion matrix</li>
<li>Generate random data</li>
<li>Configure <code class="language-plaintext highlighter-rouge">matplotlib</code></li>
<li>Generate <code class="language-plaintext highlighter-rouge">confusion_matrix</code></li>
<li>Some necessary calculation</li>
<li>Make annotation</li>
<li>Make customized confusion matrix</li>
<li>Make <code class="language-plaintext highlighter-rouge">DataFrame</code></li>
<li>Show heatmap</li>
<li>Evaluation</li>
</ul>
<h4 id="characteristics-of-the-custom-confusion-matrix">Characteristics of the custom confusion matrix</h4>
<p>This matrix will contain six different classes and every cell contains two different data one is the total number of being truly or falsely predicted and another one is the percentage(prediction rate per class).</p>
<ul>
<li>1 to 6th cell of the seventh row contains <strong>precision</strong></li>
<li>1 to 6th cell of the seventh column contains <strong>recall</strong>, and <strong>support</strong></li>
<li>Remaining one contains the <strong>accuracy</strong></li>
<li>Generated matrix will show the heatmap depending on the total numbers of <code class="language-plaintext highlighter-rouge">true_positive</code>, <code class="language-plaintext highlighter-rouge">true_negative</code>, <code class="language-plaintext highlighter-rouge">false_positive</code>, and <code class="language-plaintext highlighter-rouge">false_negative</code> or percentages</li>
</ul>
<h4 id="generate-random-data">Generate random data</h4>
<p>Import some necessary modules:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="kn">import</span> <span class="nn">matplotlib.pylab</span> <span class="k">as</span> <span class="n">pylab</span>
<span class="kn">import</span> <span class="nn">seaborn</span> <span class="k">as</span> <span class="n">sns</span>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="kn">import</span> <span class="n">confusion_matrix</span></code></pre></figure>
<p>For the visualization purpose we have to generate some random data like this:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">np</span><span class="p">.</span><span class="n">random</span><span class="p">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">46</span><span class="p">)</span> <span class="c1"># for consistent result
</span>
<span class="c1"># generate random true data for 6 classes
</span><span class="n">y_true</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">random</span><span class="p">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span>
<span class="c1"># generate random predicted data for 6 classes
</span><span class="n">y_pred</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">random</span><span class="p">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">1000</span><span class="p">)</span>
<span class="c1"># labeling the classes as follows
</span><span class="n">labels</span> <span class="o">=</span> <span class="p">[</span>
<span class="s">'Potato__Early_blight'</span><span class="p">,</span>
<span class="s">'Potato__Late_blight'</span><span class="p">,</span>
<span class="s">'Potato__healthy'</span><span class="p">,</span>
<span class="s">'Tomato__Early_blight'</span><span class="p">,</span>
<span class="s">'Tomato__Late_blight'</span><span class="p">,</span>
<span class="s">'Tomato__healthy'</span>
<span class="p">]</span>
<span class="k">print</span><span class="p">(</span><span class="n">y_true</span><span class="p">[:</span><span class="mi">10</span><span class="p">])</span>
<span class="k">print</span><span class="p">(</span><span class="n">y_pred</span><span class="p">[:</span><span class="mi">10</span><span class="p">])</span></code></pre></figure>
<p>Output first 10 digits:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[</span><span class="mi">2</span> <span class="mi">2</span> <span class="mi">2</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">1</span> <span class="mi">0</span> <span class="mi">4</span> <span class="mi">4</span> <span class="mi">1</span><span class="p">]</span>
<span class="p">[</span><span class="mi">4</span> <span class="mi">3</span> <span class="mi">4</span> <span class="mi">1</span> <span class="mi">4</span> <span class="mi">4</span> <span class="mi">3</span> <span class="mi">5</span> <span class="mi">2</span> <span class="mi">1</span><span class="p">]</span></code></pre></figure>
<h4 id="configure-matplotlib">Configure <code class="language-plaintext highlighter-rouge">matplotlib</code></h4>
<p>Set the necessary design parameters for the <code class="language-plaintext highlighter-rouge">matplotlib</code>:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">params</span> <span class="o">=</span> <span class="p">{</span>
<span class="s">'figure.figsize'</span><span class="p">:</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span>
<span class="s">'axes.titleweight'</span><span class="p">:</span> <span class="s">'bold'</span><span class="p">,</span>
<span class="s">'axes.labelsize'</span><span class="p">:</span> <span class="s">'16'</span><span class="p">,</span>
<span class="s">'axes.titlesize'</span><span class="p">:</span><span class="s">'20'</span><span class="p">,</span>
<span class="s">'axes.labelweight'</span><span class="p">:</span><span class="s">'bold'</span><span class="p">,</span>
<span class="s">'xtick.labelsize'</span><span class="p">:</span><span class="s">'14'</span><span class="p">,</span>
<span class="s">'ytick.labelsize'</span><span class="p">:</span><span class="s">'14'</span><span class="p">,</span>
<span class="s">'font.size'</span><span class="p">:</span> <span class="s">'14'</span>
<span class="p">}</span>
<span class="n">pylab</span><span class="p">.</span><span class="n">rcParams</span><span class="p">.</span><span class="n">update</span><span class="p">(</span><span class="n">params</span><span class="p">)</span></code></pre></figure>
<h4 id="generate-confusion_matrix">Generate <code class="language-plaintext highlighter-rouge">confusion_matrix</code></h4>
<p>Now generate the confusion matrix by <code class="language-plaintext highlighter-rouge">sklearn.metrics.confusion_matrix</code> module:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">c_m</span> <span class="o">=</span> <span class="n">confusion_matrix</span><span class="p">(</span><span class="n">y_true</span><span class="p">,</span> <span class="n">y_pred</span><span class="p">,</span> <span class="n">labels</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">labels</span><span class="p">))))</span>
<span class="k">print</span><span class="p">(</span><span class="n">c_m</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="mi">37</span> <span class="mi">38</span> <span class="mi">28</span> <span class="mi">21</span> <span class="mi">24</span> <span class="mi">24</span><span class="p">]</span>
<span class="p">[</span><span class="mi">22</span> <span class="mi">19</span> <span class="mi">22</span> <span class="mi">30</span> <span class="mi">23</span> <span class="mi">31</span><span class="p">]</span>
<span class="p">[</span><span class="mi">20</span> <span class="mi">38</span> <span class="mi">30</span> <span class="mi">19</span> <span class="mi">32</span> <span class="mi">34</span><span class="p">]</span>
<span class="p">[</span><span class="mi">19</span> <span class="mi">30</span> <span class="mi">39</span> <span class="mi">29</span> <span class="mi">32</span> <span class="mi">24</span><span class="p">]</span>
<span class="p">[</span><span class="mi">25</span> <span class="mi">17</span> <span class="mi">27</span> <span class="mi">26</span> <span class="mi">28</span> <span class="mi">29</span><span class="p">]</span>
<span class="p">[</span><span class="mi">21</span> <span class="mi">34</span> <span class="mi">27</span> <span class="mi">31</span> <span class="mi">35</span> <span class="mi">35</span><span class="p">]]</span></code></pre></figure>
<h4 id="some-necessary-calculation">Some necessary calculation</h4>
<p>For multi-class classification let’s calculate the sum of <code class="language-plaintext highlighter-rouge">true_positive</code>(<strong>TP</strong>) and <code class="language-plaintext highlighter-rouge">false_positive</code>(<strong>FP</strong>) of every class:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="s">"""
axis = 1 because of column-wise calculation, for example:
37 + 38 + 28 + 21 + 24 + 24 = 172
To preserve the previous dimension of the array, keepdims = True
"""</span>
<span class="n">c_m_sum</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="nb">sum</span><span class="p">(</span><span class="n">c_m</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">keepdims</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">c_m_sum</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="mi">172</span><span class="p">]</span>
<span class="p">[</span><span class="mi">147</span><span class="p">]</span>
<span class="p">[</span><span class="mi">173</span><span class="p">]</span>
<span class="p">[</span><span class="mi">173</span><span class="p">]</span>
<span class="p">[</span><span class="mi">152</span><span class="p">]</span>
<span class="p">[</span><span class="mi">183</span><span class="p">]]</span></code></pre></figure>
<p>Calculate the element-wise probability for every cell along with that we have to calculate row-wise, column-wise and diagonal <em>(total number of <strong>true_positive</strong>)</em> sum for every row and column of the matrix.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">prediction_rate</span> <span class="o">=</span> <span class="n">c_m</span> <span class="o">/</span> <span class="n">c_m_sum</span><span class="p">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">float</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span>
<span class="n">row_wise_sum</span> <span class="o">=</span> <span class="n">c_m</span><span class="p">.</span><span class="nb">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">col_wise_sum</span> <span class="o">=</span> <span class="n">c_m</span><span class="p">.</span><span class="nb">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">total_true_pos</span> <span class="o">=</span> <span class="n">c_m</span><span class="p">.</span><span class="n">diagonal</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="n">prediction_rate</span><span class="p">,</span> <span class="n">row_wise_sum</span><span class="p">,</span> <span class="n">col_wise_sum</span><span class="p">,</span> <span class="n">total_true_pos</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="mf">21.51162791</span> <span class="mf">22.09302326</span> <span class="mf">16.27906977</span> <span class="mf">12.20930233</span> <span class="mf">13.95348837</span> <span class="mf">13.95348837</span><span class="p">]</span>
<span class="p">[</span><span class="mf">14.96598639</span> <span class="mf">12.92517007</span> <span class="mf">14.96598639</span> <span class="mf">20.40816327</span> <span class="mf">15.6462585</span> <span class="mf">21.08843537</span><span class="p">]</span>
<span class="p">[</span><span class="mf">11.56069364</span> <span class="mf">21.96531792</span> <span class="mf">17.34104046</span> <span class="mf">10.98265896</span> <span class="mf">18.49710983</span> <span class="mf">19.65317919</span><span class="p">]</span>
<span class="p">[</span><span class="mf">10.98265896</span> <span class="mf">17.34104046</span> <span class="mf">22.5433526</span> <span class="mf">16.76300578</span> <span class="mf">18.49710983</span> <span class="mf">13.87283237</span><span class="p">]</span>
<span class="p">[</span><span class="mf">16.44736842</span> <span class="mf">11.18421053</span> <span class="mf">17.76315789</span> <span class="mf">17.10526316</span> <span class="mf">18.42105263</span> <span class="mf">19.07894737</span><span class="p">]</span>
<span class="p">[</span><span class="mf">11.47540984</span> <span class="mf">18.57923497</span> <span class="mf">14.75409836</span> <span class="mf">16.93989071</span> <span class="mf">19.12568306</span> <span class="mf">19.12568306</span><span class="p">]]</span>
<span class="p">[</span><span class="mi">144</span> <span class="mi">176</span> <span class="mi">173</span> <span class="mi">156</span> <span class="mi">174</span> <span class="mi">177</span><span class="p">]</span>
<span class="p">[</span><span class="mi">172</span> <span class="mi">147</span> <span class="mi">173</span> <span class="mi">173</span> <span class="mi">152</span> <span class="mi">183</span><span class="p">]</span>
<span class="p">[</span><span class="mi">37</span> <span class="mi">19</span> <span class="mi">30</span> <span class="mi">29</span> <span class="mi">28</span> <span class="mi">35</span><span class="p">]</span></code></pre></figure>
<p>Calculate the <strong>precision</strong>, <strong>recall</strong> and <strong>accuracy</strong> percentages according to these formulas:</p>
<p><img src="https://render.githubusercontent.com/render/math?math=\LARGE precision=\frac{t_p}{t_p %2B f_p}" /></p>
<p><img src="https://render.githubusercontent.com/render/math?math=\LARGE recall=\frac{t_p}{t_p %2B f_n}" /></p>
<p><img src="https://render.githubusercontent.com/render/math?math=\LARGE accuracy=\frac{t_p %2B t_n}{t_p %2B t_n %2B f_p %2B f_n}" /></p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">accuracy</span> <span class="o">=</span> <span class="n">total_true_pos</span><span class="p">.</span><span class="nb">sum</span><span class="p">()</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">y_true</span><span class="p">)</span> <span class="o">*</span> <span class="mi">100</span>
<span class="n">precision</span> <span class="o">=</span> <span class="n">total_true_pos</span> <span class="o">/</span> <span class="n">col_wise_sum</span> <span class="o">*</span> <span class="mi">100</span>
<span class="n">recall</span> <span class="o">=</span> <span class="n">total_true_pos</span> <span class="o">/</span> <span class="n">row_wise_sum</span> <span class="o">*</span> <span class="mi">100</span>
<span class="k">print</span><span class="p">(</span><span class="n">accuracy</span><span class="p">,</span> <span class="n">precision</span><span class="p">,</span> <span class="n">recall</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="mf">17.8</span>
<span class="p">[</span><span class="mf">21.51162791</span> <span class="mf">12.92517007</span> <span class="mf">17.34104046</span> <span class="mf">16.76300578</span> <span class="mf">18.42105263</span> <span class="mf">19.12568306</span><span class="p">]</span>
<span class="p">[</span><span class="mf">25.69444444</span> <span class="mf">10.79545455</span> <span class="mf">17.34104046</span> <span class="mf">18.58974359</span> <span class="mf">16.09195402</span> <span class="mf">19.7740113</span> <span class="p">]</span></code></pre></figure>
<h4 id="make-annotation">Make annotation</h4>
<p>Take an empty <code class="language-plaintext highlighter-rouge">numpy.ndarray</code> like the shape of the confusion matrix:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">annotation</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">empty_like</span><span class="p">(</span><span class="n">c_m</span><span class="p">).</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
<span class="n">rows</span><span class="p">,</span> <span class="n">cols</span> <span class="o">=</span> <span class="n">c_m</span><span class="p">.</span><span class="n">shape</span>
<span class="k">print</span><span class="p">(</span><span class="n">rows</span><span class="p">,</span> <span class="n">cols</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="mi">6</span> <span class="mi">6</span></code></pre></figure>
<p>Fill the empty annotation matrix:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">rows</span><span class="p">):</span>
<span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">cols</span><span class="p">):</span>
<span class="n">count</span> <span class="o">=</span> <span class="n">c_m</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span>
<span class="n">pre</span> <span class="o">=</span> <span class="n">prediction_rate</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span>
<span class="n">annotation</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="s">'%d</span><span class="se">\n</span><span class="s">%.1f%%'</span> <span class="o">%</span> <span class="p">(</span><span class="n">count</span><span class="p">,</span> <span class="n">pre</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">annotation</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="s">'37</span><span class="se">\n</span><span class="s">21.5%'</span> <span class="s">'38</span><span class="se">\n</span><span class="s">22.1%'</span> <span class="s">'28</span><span class="se">\n</span><span class="s">16.3%'</span> <span class="s">'21</span><span class="se">\n</span><span class="s">12.2%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">14.0%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">14.0%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'22</span><span class="se">\n</span><span class="s">15.0%'</span> <span class="s">'19</span><span class="se">\n</span><span class="s">12.9%'</span> <span class="s">'22</span><span class="se">\n</span><span class="s">15.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">20.4%'</span> <span class="s">'23</span><span class="se">\n</span><span class="s">15.6%'</span> <span class="s">'31</span><span class="se">\n</span><span class="s">21.1%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'20</span><span class="se">\n</span><span class="s">11.6%'</span> <span class="s">'38</span><span class="se">\n</span><span class="s">22.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">17.3%'</span> <span class="s">'19</span><span class="se">\n</span><span class="s">11.0%'</span> <span class="s">'32</span><span class="se">\n</span><span class="s">18.5%'</span> <span class="s">'34</span><span class="se">\n</span><span class="s">19.7%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'19</span><span class="se">\n</span><span class="s">11.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">17.3%'</span> <span class="s">'39</span><span class="se">\n</span><span class="s">22.5%'</span> <span class="s">'29</span><span class="se">\n</span><span class="s">16.8%'</span> <span class="s">'32</span><span class="se">\n</span><span class="s">18.5%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">13.9%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'25</span><span class="se">\n</span><span class="s">16.4%'</span> <span class="s">'17</span><span class="se">\n</span><span class="s">11.2%'</span> <span class="s">'27</span><span class="se">\n</span><span class="s">17.8%'</span> <span class="s">'26</span><span class="se">\n</span><span class="s">17.1%'</span> <span class="s">'28</span><span class="se">\n</span><span class="s">18.4%'</span> <span class="s">'29</span><span class="se">\n</span><span class="s">19.1%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'21</span><span class="se">\n</span><span class="s">11.5%'</span> <span class="s">'34</span><span class="se">\n</span><span class="s">18.6%'</span> <span class="s">'27</span><span class="se">\n</span><span class="s">14.8%'</span> <span class="s">'31</span><span class="se">\n</span><span class="s">16.9%'</span> <span class="s">'35</span><span class="se">\n</span><span class="s">19.1%'</span> <span class="s">'35</span><span class="se">\n</span><span class="s">19.1%'</span><span class="p">]]</span></code></pre></figure>
<p>To add the accuracy and the total number of <strong>true_positive</strong> to the matrix, we need to append those to <code class="language-plaintext highlighter-rouge">precision</code> and <code class="language-plaintext highlighter-rouge">col_wise_sum</code> array respectively.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">col_wise_sum</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">col_wise_sum</span><span class="p">,</span> <span class="n">total_true_pos</span><span class="p">.</span><span class="nb">sum</span><span class="p">())</span>
<span class="n">precision</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">precision</span><span class="p">,</span> <span class="n">accuracy</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">col_wise_sum</span><span class="p">,</span> <span class="n">precision</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[</span><span class="mi">172</span> <span class="mi">147</span> <span class="mi">173</span> <span class="mi">173</span> <span class="mi">152</span> <span class="mi">183</span> <span class="mi">178</span><span class="p">]</span>
<span class="p">[</span><span class="mf">21.51162791</span> <span class="mf">12.92517007</span> <span class="mf">17.34104046</span> <span class="mf">16.76300578</span> <span class="mf">18.42105263</span> <span class="mf">19.12568306</span> <span class="mf">17.8</span><span class="p">]</span></code></pre></figure>
<p>Make tuple of <code class="language-plaintext highlighter-rouge">(total_num, percentage)</code> to make annotation for both <strong>recall</strong> and <strong>precision</strong>.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">recall</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">array</span><span class="p">([[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">j</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">row_wise_sum</span><span class="p">,</span> <span class="n">recall</span><span class="p">)])</span>
<span class="n">precision</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">array</span><span class="p">([[</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">j</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">col_wise_sum</span><span class="p">,</span> <span class="n">precision</span><span class="p">)])</span>
<span class="k">print</span><span class="p">(</span><span class="n">recall</span><span class="p">,</span> <span class="n">precision</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="mf">144.</span> <span class="mf">25.69444444</span><span class="p">]</span>
<span class="p">[</span><span class="mf">176.</span> <span class="mf">10.79545455</span><span class="p">]</span>
<span class="p">[</span><span class="mf">173.</span> <span class="mf">17.34104046</span><span class="p">]</span>
<span class="p">[</span><span class="mf">156.</span> <span class="mf">18.58974359</span><span class="p">]</span>
<span class="p">[</span><span class="mf">174.</span> <span class="mf">16.09195402</span><span class="p">]</span>
<span class="p">[</span><span class="mf">177.</span> <span class="mf">19.7740113</span> <span class="p">]]</span>
<span class="p">[[</span><span class="mf">172.</span> <span class="mf">21.51162791</span><span class="p">]</span>
<span class="p">[</span><span class="mf">147.</span> <span class="mf">12.92517007</span><span class="p">]</span>
<span class="p">[</span><span class="mf">173.</span> <span class="mf">17.34104046</span><span class="p">]</span>
<span class="p">[</span><span class="mf">173.</span> <span class="mf">16.76300578</span><span class="p">]</span>
<span class="p">[</span><span class="mf">152.</span> <span class="mf">18.42105263</span><span class="p">]</span>
<span class="p">[</span><span class="mf">183.</span> <span class="mf">19.12568306</span><span class="p">]</span>
<span class="p">[</span><span class="mf">178.</span> <span class="mf">17.8</span> <span class="p">]]</span></code></pre></figure>
<p>Add newly created annotation <em>(from <strong>recall</strong>)</em> to the previous annotation.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">annotation</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">annotation</span><span class="p">,</span> <span class="p">[</span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">data</span><span class="p">:</span> <span class="s">'%d</span><span class="se">\n</span><span class="s">%.1f%%'</span> <span class="o">%</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">recall</span><span class="p">))]),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">annotation</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="s">'37</span><span class="se">\n</span><span class="s">21.5%'</span> <span class="s">'38</span><span class="se">\n</span><span class="s">22.1%'</span> <span class="s">'28</span><span class="se">\n</span><span class="s">16.3%'</span> <span class="s">'21</span><span class="se">\n</span><span class="s">12.2%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">14.0%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">14.0%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'22</span><span class="se">\n</span><span class="s">15.0%'</span> <span class="s">'19</span><span class="se">\n</span><span class="s">12.9%'</span> <span class="s">'22</span><span class="se">\n</span><span class="s">15.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">20.4%'</span> <span class="s">'23</span><span class="se">\n</span><span class="s">15.6%'</span> <span class="s">'31</span><span class="se">\n</span><span class="s">21.1%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'20</span><span class="se">\n</span><span class="s">11.6%'</span> <span class="s">'38</span><span class="se">\n</span><span class="s">22.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">17.3%'</span> <span class="s">'19</span><span class="se">\n</span><span class="s">11.0%'</span> <span class="s">'32</span><span class="se">\n</span><span class="s">18.5%'</span> <span class="s">'34</span><span class="se">\n</span><span class="s">19.7%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'19</span><span class="se">\n</span><span class="s">11.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">17.3%'</span> <span class="s">'39</span><span class="se">\n</span><span class="s">22.5%'</span> <span class="s">'29</span><span class="se">\n</span><span class="s">16.8%'</span> <span class="s">'32</span><span class="se">\n</span><span class="s">18.5%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">13.9%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'25</span><span class="se">\n</span><span class="s">16.4%'</span> <span class="s">'17</span><span class="se">\n</span><span class="s">11.2%'</span> <span class="s">'27</span><span class="se">\n</span><span class="s">17.8%'</span> <span class="s">'26</span><span class="se">\n</span><span class="s">17.1%'</span> <span class="s">'28</span><span class="se">\n</span><span class="s">18.4%'</span> <span class="s">'29</span><span class="se">\n</span><span class="s">19.1%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'21</span><span class="se">\n</span><span class="s">11.5%'</span> <span class="s">'34</span><span class="se">\n</span><span class="s">18.6%'</span> <span class="s">'27</span><span class="se">\n</span><span class="s">14.8%'</span> <span class="s">'31</span><span class="se">\n</span><span class="s">16.9%'</span> <span class="s">'35</span><span class="se">\n</span><span class="s">19.1%'</span> <span class="s">'35</span><span class="se">\n</span><span class="s">19.1%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'144</span><span class="se">\n</span><span class="s">25.7%'</span> <span class="s">'176</span><span class="se">\n</span><span class="s">10.8%'</span> <span class="s">'173</span><span class="se">\n</span><span class="s">17.3%'</span> <span class="s">'156</span><span class="se">\n</span><span class="s">18.6%'</span> <span class="s">'174</span><span class="se">\n</span><span class="s">16.1%'</span>
<span class="s">'177</span><span class="se">\n</span><span class="s">19.8%'</span><span class="p">]]</span></code></pre></figure>
<p>Add newly created annotation <em>(from <strong>precision</strong>)</em> to the previous annotation.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">annotation</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">annotation</span><span class="p">,</span> <span class="n">np</span><span class="p">.</span><span class="n">array</span><span class="p">([</span><span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">data</span><span class="p">:</span> <span class="s">'%d</span><span class="se">\n</span><span class="s">%.1f%%'</span> <span class="o">%</span> <span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">]),</span> <span class="n">precision</span><span class="p">))]).</span><span class="n">T</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">annotation</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="s">'37</span><span class="se">\n</span><span class="s">21.5%'</span> <span class="s">'38</span><span class="se">\n</span><span class="s">22.1%'</span> <span class="s">'28</span><span class="se">\n</span><span class="s">16.3%'</span> <span class="s">'21</span><span class="se">\n</span><span class="s">12.2%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">14.0%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">14.0%'</span>
<span class="s">'172</span><span class="se">\n</span><span class="s">21.5%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'22</span><span class="se">\n</span><span class="s">15.0%'</span> <span class="s">'19</span><span class="se">\n</span><span class="s">12.9%'</span> <span class="s">'22</span><span class="se">\n</span><span class="s">15.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">20.4%'</span> <span class="s">'23</span><span class="se">\n</span><span class="s">15.6%'</span> <span class="s">'31</span><span class="se">\n</span><span class="s">21.1%'</span>
<span class="s">'147</span><span class="se">\n</span><span class="s">12.9%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'20</span><span class="se">\n</span><span class="s">11.6%'</span> <span class="s">'38</span><span class="se">\n</span><span class="s">22.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">17.3%'</span> <span class="s">'19</span><span class="se">\n</span><span class="s">11.0%'</span> <span class="s">'32</span><span class="se">\n</span><span class="s">18.5%'</span> <span class="s">'34</span><span class="se">\n</span><span class="s">19.7%'</span>
<span class="s">'173</span><span class="se">\n</span><span class="s">17.3%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'19</span><span class="se">\n</span><span class="s">11.0%'</span> <span class="s">'30</span><span class="se">\n</span><span class="s">17.3%'</span> <span class="s">'39</span><span class="se">\n</span><span class="s">22.5%'</span> <span class="s">'29</span><span class="se">\n</span><span class="s">16.8%'</span> <span class="s">'32</span><span class="se">\n</span><span class="s">18.5%'</span> <span class="s">'24</span><span class="se">\n</span><span class="s">13.9%'</span>
<span class="s">'173</span><span class="se">\n</span><span class="s">16.8%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'25</span><span class="se">\n</span><span class="s">16.4%'</span> <span class="s">'17</span><span class="se">\n</span><span class="s">11.2%'</span> <span class="s">'27</span><span class="se">\n</span><span class="s">17.8%'</span> <span class="s">'26</span><span class="se">\n</span><span class="s">17.1%'</span> <span class="s">'28</span><span class="se">\n</span><span class="s">18.4%'</span> <span class="s">'29</span><span class="se">\n</span><span class="s">19.1%'</span>
<span class="s">'152</span><span class="se">\n</span><span class="s">18.4%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'21</span><span class="se">\n</span><span class="s">11.5%'</span> <span class="s">'34</span><span class="se">\n</span><span class="s">18.6%'</span> <span class="s">'27</span><span class="se">\n</span><span class="s">14.8%'</span> <span class="s">'31</span><span class="se">\n</span><span class="s">16.9%'</span> <span class="s">'35</span><span class="se">\n</span><span class="s">19.1%'</span> <span class="s">'35</span><span class="se">\n</span><span class="s">19.1%'</span>
<span class="s">'183</span><span class="se">\n</span><span class="s">19.1%'</span><span class="p">]</span>
<span class="p">[</span><span class="s">'144</span><span class="se">\n</span><span class="s">25.7%'</span> <span class="s">'176</span><span class="se">\n</span><span class="s">10.8%'</span> <span class="s">'173</span><span class="se">\n</span><span class="s">17.3%'</span> <span class="s">'156</span><span class="se">\n</span><span class="s">18.6%'</span> <span class="s">'174</span><span class="se">\n</span><span class="s">16.1%'</span>
<span class="s">'177</span><span class="se">\n</span><span class="s">19.8%'</span> <span class="s">'178</span><span class="se">\n</span><span class="s">17.8%'</span><span class="p">]]</span></code></pre></figure>
<p>Previously I have added extra row and column so that we have to add another label name as <code class="language-plaintext highlighter-rouge">precision_recall_accuracy</code></p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">labels</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">'precision_recall_accuracy'</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">labels</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[</span><span class="s">'Potato__Early_blight'</span><span class="p">,</span> <span class="s">'Potato__Late_blight'</span><span class="p">,</span> <span class="s">'Potato__healthy'</span><span class="p">,</span> <span class="s">'Tomato__Early_blight'</span><span class="p">,</span> <span class="s">'Tomato__Late_blight'</span><span class="p">,</span> <span class="s">'Tomato__healthy'</span><span class="p">,</span> <span class="s">'precision_recall_accuracy'</span><span class="p">]</span></code></pre></figure>
<h4 id="make-customized-confusion-matrix">Make customized confusion matrix</h4>
<p>Change the structure of the previously created confusion matrix to add <strong>precision</strong> and <strong>recall</strong> to the row and column respectively.
So that here I have created a new <code class="language-plaintext highlighter-rouge">c_m_1</code> matrix with the total numbers of <strong>recall</strong> and <strong>precision</strong> from <code class="language-plaintext highlighter-rouge">c_m</code>.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">c_m_1</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">c_m</span><span class="p">,</span> <span class="p">[</span><span class="n">recall</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]]),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">c_m_1</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">c_m_1</span><span class="p">,</span> <span class="n">np</span><span class="p">.</span><span class="n">array</span><span class="p">([</span><span class="n">precision</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]]).</span><span class="n">T</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">c_m</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">c_m_1</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="mi">37</span> <span class="mi">38</span> <span class="mi">28</span> <span class="mi">21</span> <span class="mi">24</span> <span class="mi">24</span><span class="p">]</span>
<span class="p">[</span><span class="mi">22</span> <span class="mi">19</span> <span class="mi">22</span> <span class="mi">30</span> <span class="mi">23</span> <span class="mi">31</span><span class="p">]</span>
<span class="p">[</span><span class="mi">20</span> <span class="mi">38</span> <span class="mi">30</span> <span class="mi">19</span> <span class="mi">32</span> <span class="mi">34</span><span class="p">]</span>
<span class="p">[</span><span class="mi">19</span> <span class="mi">30</span> <span class="mi">39</span> <span class="mi">29</span> <span class="mi">32</span> <span class="mi">24</span><span class="p">]</span>
<span class="p">[</span><span class="mi">25</span> <span class="mi">17</span> <span class="mi">27</span> <span class="mi">26</span> <span class="mi">28</span> <span class="mi">29</span><span class="p">]</span>
<span class="p">[</span><span class="mi">21</span> <span class="mi">34</span> <span class="mi">27</span> <span class="mi">31</span> <span class="mi">35</span> <span class="mi">35</span><span class="p">]]</span>
<span class="p">[[</span> <span class="mf">37.</span> <span class="mf">38.</span> <span class="mf">28.</span> <span class="mf">21.</span> <span class="mf">24.</span> <span class="mf">24.</span> <span class="mf">172.</span><span class="p">]</span>
<span class="p">[</span> <span class="mf">22.</span> <span class="mf">19.</span> <span class="mf">22.</span> <span class="mf">30.</span> <span class="mf">23.</span> <span class="mf">31.</span> <span class="mf">147.</span><span class="p">]</span>
<span class="p">[</span> <span class="mf">20.</span> <span class="mf">38.</span> <span class="mf">30.</span> <span class="mf">19.</span> <span class="mf">32.</span> <span class="mf">34.</span> <span class="mf">173.</span><span class="p">]</span>
<span class="p">[</span> <span class="mf">19.</span> <span class="mf">30.</span> <span class="mf">39.</span> <span class="mf">29.</span> <span class="mf">32.</span> <span class="mf">24.</span> <span class="mf">173.</span><span class="p">]</span>
<span class="p">[</span> <span class="mf">25.</span> <span class="mf">17.</span> <span class="mf">27.</span> <span class="mf">26.</span> <span class="mf">28.</span> <span class="mf">29.</span> <span class="mf">152.</span><span class="p">]</span>
<span class="p">[</span> <span class="mf">21.</span> <span class="mf">34.</span> <span class="mf">27.</span> <span class="mf">31.</span> <span class="mf">35.</span> <span class="mf">35.</span> <span class="mf">183.</span><span class="p">]</span>
<span class="p">[</span><span class="mf">144.</span> <span class="mf">176.</span> <span class="mf">173.</span> <span class="mf">156.</span> <span class="mf">174.</span> <span class="mf">177.</span> <span class="mf">178.</span><span class="p">]]</span></code></pre></figure>
<p>Create another matrix named <code class="language-plaintext highlighter-rouge">c_m_2</code> which will contain the prediction rate and precision-recall percentage from <code class="language-plaintext highlighter-rouge">prediction_rate</code> matrix.</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">c_m_2</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">prediction_rate</span><span class="p">,</span> <span class="p">[</span><span class="n">recall</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]]),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">c_m_2</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">c_m_2</span><span class="p">,</span> <span class="n">np</span><span class="p">.</span><span class="n">array</span><span class="p">([</span><span class="n">precision</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">]]).</span><span class="n">T</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">prediction_rate</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">c_m_2</span><span class="p">)</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="p">[[</span><span class="mf">21.51162791</span> <span class="mf">22.09302326</span> <span class="mf">16.27906977</span> <span class="mf">12.20930233</span> <span class="mf">13.95348837</span> <span class="mf">13.95348837</span><span class="p">]</span>
<span class="p">[</span><span class="mf">14.96598639</span> <span class="mf">12.92517007</span> <span class="mf">14.96598639</span> <span class="mf">20.40816327</span> <span class="mf">15.6462585</span> <span class="mf">21.08843537</span><span class="p">]</span>
<span class="p">[</span><span class="mf">11.56069364</span> <span class="mf">21.96531792</span> <span class="mf">17.34104046</span> <span class="mf">10.98265896</span> <span class="mf">18.49710983</span> <span class="mf">19.65317919</span><span class="p">]</span>
<span class="p">[</span><span class="mf">10.98265896</span> <span class="mf">17.34104046</span> <span class="mf">22.5433526</span> <span class="mf">16.76300578</span> <span class="mf">18.49710983</span> <span class="mf">13.87283237</span><span class="p">]</span>
<span class="p">[</span><span class="mf">16.44736842</span> <span class="mf">11.18421053</span> <span class="mf">17.76315789</span> <span class="mf">17.10526316</span> <span class="mf">18.42105263</span> <span class="mf">19.07894737</span><span class="p">]</span>
<span class="p">[</span><span class="mf">11.47540984</span> <span class="mf">18.57923497</span> <span class="mf">14.75409836</span> <span class="mf">16.93989071</span> <span class="mf">19.12568306</span> <span class="mf">19.12568306</span><span class="p">]]</span>
<span class="p">[[</span><span class="mf">21.51162791</span> <span class="mf">22.09302326</span> <span class="mf">16.27906977</span> <span class="mf">12.20930233</span> <span class="mf">13.95348837</span> <span class="mf">13.95348837</span>
<span class="mf">21.51162791</span><span class="p">]</span>
<span class="p">[</span><span class="mf">14.96598639</span> <span class="mf">12.92517007</span> <span class="mf">14.96598639</span> <span class="mf">20.40816327</span> <span class="mf">15.6462585</span> <span class="mf">21.08843537</span>
<span class="mf">12.92517007</span><span class="p">]</span>
<span class="p">[</span><span class="mf">11.56069364</span> <span class="mf">21.96531792</span> <span class="mf">17.34104046</span> <span class="mf">10.98265896</span> <span class="mf">18.49710983</span> <span class="mf">19.65317919</span>
<span class="mf">17.34104046</span><span class="p">]</span>
<span class="p">[</span><span class="mf">10.98265896</span> <span class="mf">17.34104046</span> <span class="mf">22.5433526</span> <span class="mf">16.76300578</span> <span class="mf">18.49710983</span> <span class="mf">13.87283237</span>
<span class="mf">16.76300578</span><span class="p">]</span>
<span class="p">[</span><span class="mf">16.44736842</span> <span class="mf">11.18421053</span> <span class="mf">17.76315789</span> <span class="mf">17.10526316</span> <span class="mf">18.42105263</span> <span class="mf">19.07894737</span>
<span class="mf">18.42105263</span><span class="p">]</span>
<span class="p">[</span><span class="mf">11.47540984</span> <span class="mf">18.57923497</span> <span class="mf">14.75409836</span> <span class="mf">16.93989071</span> <span class="mf">19.12568306</span> <span class="mf">19.12568306</span>
<span class="mf">19.12568306</span><span class="p">]</span>
<span class="p">[</span><span class="mf">25.69444444</span> <span class="mf">10.79545455</span> <span class="mf">17.34104046</span> <span class="mf">18.58974359</span> <span class="mf">16.09195402</span> <span class="mf">19.7740113</span>
<span class="mf">17.8</span> <span class="p">]]</span></code></pre></figure>
<h4 id="make-dataframe">Make <code class="language-plaintext highlighter-rouge">DataFrame</code></h4>
<p>We need to create <code class="language-plaintext highlighter-rouge">DataFrame</code> for both <code class="language-plaintext highlighter-rouge">c_m_1</code> and <code class="language-plaintext highlighter-rouge">c_m_2</code> and set their index or column name as follows:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">c_m_1</span> <span class="o">=</span> <span class="n">pd</span><span class="p">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">c_m_1</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">labels</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">labels</span><span class="p">)</span>
<span class="n">c_m_1</span><span class="p">.</span><span class="n">index</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">'Output Class'</span>
<span class="n">c_m_1</span><span class="p">.</span><span class="n">columns</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">'Target Class'</span>
<span class="n">c_m_2</span> <span class="o">=</span> <span class="n">pd</span><span class="p">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">c_m_2</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="n">labels</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">labels</span><span class="p">)</span>
<span class="n">c_m_2</span><span class="p">.</span><span class="n">index</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">'Output Class'</span>
<span class="n">c_m_2</span><span class="p">.</span><span class="n">columns</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">'Target Class'</span>
<span class="n">c_m_1</span><span class="p">.</span><span class="n">info</span><span class="p">()</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="o">&lt;</span><span class="k">class</span> <span class="err">'</span><span class="nc">pandas</span><span class="p">.</span><span class="n">core</span><span class="p">.</span><span class="n">frame</span><span class="p">.</span><span class="n">DataFrame</span><span class="s">'&gt;
Index: 7 entries, Potato__Early_blight to precision_recall_accuracy
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Potato__Early_blight 7 non-null float64
1 Potato__Late_blight 7 non-null float64
2 Potato__healthy 7 non-null float64
3 Tomato__Early_blight 7 non-null float64
4 Tomato__Late_blight 7 non-null float64
5 Tomato__healthy 7 non-null float64
6 precision_recall_accuracy 7 non-null float64
dtypes: float64(7)
memory usage: 448.0+ bytes</span></code></pre></figure>
<h4 id="show-heatmap">Show heatmap</h4>
<p>Let’s show a heatmap depending on the number of classes.</p>
<p><em>color intensities varies yellow(low) to green(high)</em></p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">heatmap_1</span> <span class="o">=</span> <span class="n">sns</span><span class="p">.</span><span class="n">heatmap</span><span class="p">(</span><span class="n">c_m_1</span><span class="p">,</span> <span class="n">annot</span><span class="o">=</span><span class="n">annotation</span><span class="p">,</span> <span class="n">fmt</span><span class="o">=</span><span class="s">''</span><span class="p">,</span> <span class="n">cbar</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="s">'YlGn'</span><span class="p">)</span>
<span class="n">heatmap_1</span><span class="p">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">heatmap_1</span><span class="p">.</span><span class="n">get_xticklabels</span><span class="p">(),</span> <span class="n">rotation</span><span class="o">=</span><span class="mi">45</span><span class="p">,</span> <span class="n">horizontalalignment</span><span class="o">=</span><span class="s">'right'</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">title</span><span class="p">(</span><span class="s">'Confusion Matrix(number of classes)'</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span></code></pre></figure>
<p>Output:
<img src="/assets/images/2021-07-01-How-to-make-a-custom-confusion-matrix-on-matplotlib-with-heatmap-and-annotation/2.png" alt="Confusion Matrix(number of classes)" title="Confusion Matrix(number of classes)" />
Showing a heatmap depending on percentages:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">heatmap_2</span> <span class="o">=</span> <span class="n">sns</span><span class="p">.</span><span class="n">heatmap</span><span class="p">(</span><span class="n">c_m_2</span><span class="p">,</span> <span class="n">annot</span><span class="o">=</span><span class="n">annotation</span><span class="p">,</span> <span class="n">fmt</span><span class="o">=</span><span class="s">''</span><span class="p">,</span> <span class="n">cbar</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="s">'YlGn'</span><span class="p">)</span>
<span class="n">heatmap_2</span><span class="p">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">heatmap_2</span><span class="p">.</span><span class="n">get_xticklabels</span><span class="p">(),</span> <span class="n">rotation</span><span class="o">=</span><span class="mi">45</span><span class="p">,</span> <span class="n">horizontalalignment</span><span class="o">=</span><span class="s">'right'</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">title</span><span class="p">(</span><span class="s">'Confusion Matrix(percentages)'</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span></code></pre></figure>
<p>Output:
<img src="/assets/images/2021-07-01-How-to-make-a-custom-confusion-matrix-on-matplotlib-with-heatmap-and-annotation/3.png" alt="Confusion Matrix(percentages)" title="Confusion Matrix(percentages)" /></p>
<p>Let’s eliminate the color map for better view:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="n">heatmap</span> <span class="o">=</span> <span class="n">sns</span><span class="p">.</span><span class="n">heatmap</span><span class="p">(</span><span class="n">c_m_2</span><span class="p">,</span> <span class="n">annot</span><span class="o">=</span><span class="n">annotation</span><span class="p">,</span> <span class="n">fmt</span><span class="o">=</span><span class="s">''</span><span class="p">,</span> <span class="n">cbar</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="s">'YlGn'</span><span class="p">)</span>
<span class="n">heatmap</span><span class="p">.</span><span class="n">set_xticklabels</span><span class="p">(</span><span class="n">heatmap</span><span class="p">.</span><span class="n">get_xticklabels</span><span class="p">(),</span> <span class="n">rotation</span><span class="o">=</span><span class="mi">45</span><span class="p">,</span> <span class="n">horizontalalignment</span><span class="o">=</span><span class="s">'right'</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">title</span><span class="p">(</span><span class="s">'Confusion Matrix'</span><span class="p">)</span>
<span class="n">plt</span><span class="p">.</span><span class="n">show</span><span class="p">()</span></code></pre></figure>
<p>Output:
<img src="/assets/images/2021-07-01-How-to-make-a-custom-confusion-matrix-on-matplotlib-with-heatmap-and-annotation/1.png" alt="Confusion Matrix" title="Confusion Matrix" /></p>
<h4 id="evaluation">Evaluation</h4>
<p>Now we can evaluate our <strong>accuracy</strong>, <strong>precision</strong>, <strong>recall</strong> and <strong>support</strong> scores with <code class="language-plaintext highlighter-rouge">sklearn.metrics</code> module as follows:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="kn">import</span> <span class="n">classification_report</span>
<span class="k">print</span><span class="p">(</span><span class="n">classification_report</span><span class="p">(</span><span class="n">y_true</span><span class="p">,</span> <span class="n">y_pred</span><span class="p">,</span> <span class="n">labels</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">labels</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]))),</span> <span class="n">target_names</span><span class="o">=</span><span class="n">labels</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span></code></pre></figure>
<p>Output:</p>
<figure class="highlight"><pre><code class="language-python" data-lang="python"> <span class="n">precision</span> <span class="n">recall</span> <span class="n">f1</span><span class="o">-</span><span class="n">score</span> <span class="n">support</span>
<span class="n">Potato__Early_blight</span> <span class="mf">0.26</span> <span class="mf">0.22</span> <span class="mf">0.23</span> <span class="mi">172</span>
<span class="n">Potato__Late_blight</span> <span class="mf">0.11</span> <span class="mf">0.13</span> <span class="mf">0.12</span> <span class="mi">147</span>
<span class="n">Potato__healthy</span> <span class="mf">0.17</span> <span class="mf">0.17</span> <span class="mf">0.17</span> <span class="mi">173</span>
<span class="n">Tomato__Early_blight</span> <span class="mf">0.19</span> <span class="mf">0.17</span> <span class="mf">0.18</span> <span class="mi">173</span>
<span class="n">Tomato__Late_blight</span> <span class="mf">0.16</span> <span class="mf">0.18</span> <span class="mf">0.17</span> <span class="mi">152</span>
<span class="n">Tomato__healthy</span> <span class="mf">0.20</span> <span class="mf">0.19</span> <span class="mf">0.19</span> <span class="mi">183</span>
<span class="n">accuracy</span> <span class="mf">0.18</span> <span class="mi">1000</span>
<span class="n">macro</span> <span class="n">avg</span> <span class="mf">0.18</span> <span class="mf">0.18</span> <span class="mf">0.18</span> <span class="mi">1000</span>
<span class="n">weighted</span> <span class="n">avg</span> <span class="mf">0.18</span> <span class="mf">0.18</span> <span class="mf">0.18</span> <span class="mi">1000</span></code></pre></figure>
<p>Accuracy, Precision, Recall and Support scores are matched with ours.</p>
<p>This is the full code:
<script src="https://gist.github.com/rjarman/b2a5e00e65ba22554f2d1f4a07fc9532.js"></script>
A github repository can be found on this <a href="https://github.com/rjarman/visualizations">link</a>.</p></content><author><name>rafsun</name></author><category term="Machine Learning" /><category term="Visualization" /><category term="python" /><category term="matplotlib" /><category term="confusion matrix" /><category term="heatmap" /><summary type="html">A confusion matrix is also known as an error matrix or matching matrix1. Each row and each column of this matrix represents the actual class and the predicted class respectively. Here in this post, I have discussed how we can build a custom confusion matrix for multi-class classification with matplotlib in python. Here is the post outline: Characteristics of the custom confusion matrix Generate random data Configure matplotlib Generate confusion_matrix Some necessary calculation Make annotation Make customized confusion matrix Make DataFrame Show heatmap Evaluation</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.heaplinker.com/assets/images/2021-07-01-How-to-make-a-custom-confusion-matrix-on-matplotlib-with-heatmap-and-annotation/1.png" /><media:content medium="image" url="https://blog.heaplinker.com/assets/images/2021-07-01-How-to-make-a-custom-confusion-matrix-on-matplotlib-with-heatmap-and-annotation/1.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>