最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用python中的matplotlib打印混淆矩陣實(shí)例

 更新時(shí)間:2020年06月16日 09:46:55   作者:Kun Li  
這篇文章主要介紹了利用python中的matplotlib打印混淆矩陣實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

前面說過混淆矩陣是我們在處理分類問題時(shí),很重要的指標(biāo),那么如何更好的把混淆矩陣給打印出來呢,直接做表或者是前端可視化,小編曾經(jīng)就嘗試過用前端(D5)做出來,然后截圖,顯得不那么好看。。

代碼:

import itertools
import matplotlib.pyplot as plt
import numpy as np
 
def plot_confusion_matrix(cm, classes,
       normalize=False,
       title='Confusion matrix',
       cmap=plt.cm.Blues):
 """
 This function prints and plots the confusion matrix.
 Normalization can be applied by setting `normalize=True`.
 """
 if normalize:
  cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
  print("Normalized confusion matrix")
 else:
  print('Confusion matrix, without normalization')
 
 print(cm)
 
 plt.imshow(cm, interpolation='nearest', cmap=cmap)
 plt.title(title)
 plt.colorbar()
 tick_marks = np.arange(len(classes))
 plt.xticks(tick_marks, classes, rotation=45)
 plt.yticks(tick_marks, classes)
 
 fmt = '.2f' if normalize else 'd'
 thresh = cm.max() / 2.
 for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
  plt.text(j, i, format(cm[i, j], fmt),
     horizontalalignment="center",
     color="white" if cm[i, j] > thresh else "black")
 
 plt.tight_layout()
 plt.ylabel('True label')
 plt.xlabel('Predicted label')
 plt.show()
 # plt.savefig('confusion_matrix',dpi=200)
 
cnf_matrix = np.array([
 [4101, 2, 5, 24, 0],
 [50, 3930, 6, 14, 5],
 [29, 3, 3973, 4, 0],
 [45, 7, 1, 3878, 119],
 [31, 1, 8, 28, 3936],
])
 
class_names = ['Buildings', 'Farmland', 'Greenbelt', 'Wasteland', 'Water']
 
# plt.figure()
# plot_confusion_matrix(cnf_matrix, classes=class_names,
#      title='Confusion matrix, without normalization')
 
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
      title='Normalized confusion matrix')

在放矩陣位置,放一下你的混淆矩陣就可以,當(dāng)然可視化混淆矩陣這一步也可以直接在模型運(yùn)行中完成。

補(bǔ)充知識(shí):混淆矩陣(Confusion matrix)的原理及使用(scikit-learn 和 tensorflow)

原理

在機(jī)器學(xué)習(xí)中, 混淆矩陣是一個(gè)誤差矩陣, 常用來可視化地評估監(jiān)督學(xué)習(xí)算法的性能. 混淆矩陣大小為 (n_classes, n_classes) 的方陣, 其中 n_classes 表示類的數(shù)量. 這個(gè)矩陣的每一行表示真實(shí)類中的實(shí)例, 而每一列表示預(yù)測類中的實(shí)例 (Tensorflow 和 scikit-learn 采用的實(shí)現(xiàn)方式). 也可以是, 每一行表示預(yù)測類中的實(shí)例, 而每一列表示真實(shí)類中的實(shí)例 (Confusion matrix From Wikipedia 中的定義). 通過混淆矩陣, 可以很容易看出系統(tǒng)是否會(huì)弄混兩個(gè)類, 這也是混淆矩陣名字的由來.

混淆矩陣是一種特殊類型的列聯(lián)表(contingency table)或交叉制表(cross tabulation or crosstab). 其有兩維 (真實(shí)值 "actual" 和 預(yù)測值 "predicted" ), 這兩維都具有相同的類("classes")的集合. 在列聯(lián)表中, 每個(gè)維度和類的組合是一個(gè)變量. 列聯(lián)表以表的形式, 可視化地表示多個(gè)變量的頻率分布.

使用混淆矩陣( scikit-learn 和 Tensorflow)

下面先介紹在 scikit-learn 和 tensorflow 中計(jì)算混淆矩陣的 API (Application Programming Interface) 接口函數(shù), 然后在一個(gè)示例中, 使用這兩個(gè) API 函數(shù).

scikit-learn 混淆矩陣函數(shù) sklearn.metrics.confusion_matrix API 接口

skearn.metrics.confusion_matrix(
 y_true, # array, Gound true (correct) target values
 y_pred, # array, Estimated targets as returned by a classifier
 labels=None, # array, List of labels to index the matrix.
 sample_weight=None # array-like of shape = [n_samples], Optional sample weights
)

在 scikit-learn 中, 計(jì)算混淆矩陣用來評估分類的準(zhǔn)確度.

按照定義, 混淆矩陣 C 中的元素 Ci,j 等于真實(shí)值為組 i , 而預(yù)測為組 j 的觀測數(shù)(the number of observations). 所以對于二分類任務(wù), 預(yù)測結(jié)果中, 正確的負(fù)例數(shù)(true negatives, TN)為 C0,0; 錯(cuò)誤的負(fù)例數(shù)(false negatives, FN)為 C1,0; 真實(shí)的正例數(shù)為 C1,1; 錯(cuò)誤的正例數(shù)為 C0,1.

如果 labels 為 None, scikit-learn 會(huì)把在出現(xiàn)在 y_true 或 y_pred 中的所有值添加到標(biāo)記列表 labels 中, 并排好序.

Tensorflow 混淆矩陣函數(shù) tf.confusion_matrix API 接口

tf.confusion_matrix(
 labels, # 1-D Tensor of real labels for the classification task
 predictions, # 1-D Tensor of predictions for a givenclassification
 num_classes=None, # The possible number of labels the classification task can have
 dtype=tf.int32, # Data type of the confusion matrix 
 name=None, # Scope name
 weights=None, # An optional Tensor whose shape matches predictions
)

Tensorflow tf.confusion_matrix 中的 num_classes 參數(shù)的含義, 與 scikit-learn sklearn.metrics.confusion_matrix 中的 labels 參數(shù)相近, 是與標(biāo)記有關(guān)的參數(shù), 表示類的總個(gè)數(shù), 但沒有列出具體的標(biāo)記值. 在 Tensorflow 中一般是以整數(shù)作為標(biāo)記, 如果標(biāo)記為字符串等非整數(shù)類型, 則需先轉(zhuǎn)為整數(shù)表示. 如果 num_classes 參數(shù)為 None, 則把 labels 和 predictions 中的最大值 + 1, 作為num_classes 參數(shù)值.

tf.confusion_matrix 的 weights 參數(shù)和 sklearn.metrics.confusion_matrix 的 sample_weight 參數(shù)的含義相同, 都是對預(yù)測值進(jìn)行加權(quán), 在此基礎(chǔ)上, 計(jì)算混淆矩陣單元的值.

使用示例

#!/usr/bin/env python
# -*- coding: utf8 -*-
"""
Author: klchang
Description: 
  A simple example for tf.confusion_matrix and sklearn.metrics.confusion_matrix.
Date: 2018.9.8
"""
from __future__ import print_function
import tensorflow as tf
import sklearn.metrics
 
y_true = [1, 2, 4]
y_pred = [2, 2, 4]
 
# Build graph with tf.confusion_matrix operation
sess = tf.InteractiveSession()
op = tf.confusion_matrix(y_true, y_pred)
op2 = tf.confusion_matrix(y_true, y_pred, num_classes=6, dtype=tf.float32, weights=tf.constant([0.3, 0.4, 0.3]))
# Execute the graph
print ("confusion matrix in tensorflow: ")
print ("1. default: \n", op.eval())
print ("2. customed: \n", sess.run(op2))
sess.close()
 
# Use sklearn.metrics.confusion_matrix function
print ("\nconfusion matrix in scikit-learn: ")
print ("1. default: \n", sklearn.metrics.confusion_matrix(y_true, y_pred))
print ("2. customed: \n", sklearn.metrics.confusion_matrix(y_true, y_pred, labels=range(6), sample_weight=[0.3, 0.4, 0.3]))

以上這篇利用python中的matplotlib打印混淆矩陣實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解mac python+selenium+Chrome 簡單案例

    詳解mac python+selenium+Chrome 簡單案例

    這篇文章主要介紹了詳解mac python+selenium+Chrome 簡單案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Python?requirements.txt的具體使用

    Python?requirements.txt的具體使用

    requirements.txt文件是項(xiàng)目的依賴包及其對應(yīng)版本號(hào)的信息列表,本文主要介紹了Python?requirements.txt的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Python實(shí)現(xiàn)Logger打印功能的方法詳解

    Python實(shí)現(xiàn)Logger打印功能的方法詳解

    最近工作中遇到了打印的需求,通過查找相關(guān)的資料發(fā)現(xiàn)Python中Logger可以很好的實(shí)現(xiàn)打印,所以下面這篇文章主要給大家介紹了關(guān)于Python如何實(shí)現(xiàn)Logger打印功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-09-09
  • python繪制詞云圖最全教程(自定義png形狀、指定字體、顏色)

    python繪制詞云圖最全教程(自定義png形狀、指定字體、顏色)

    詞云圖是一種直觀的方式來展示文本數(shù)據(jù),它易于理解,能展示出詞語的頻率使用情況,對于文本分析非常有用,這篇文章主要給大家介紹了python繪制詞云圖(自定義png形狀、指定字體、顏色)的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • 一步步教你用python連接oracle數(shù)據(jù)庫

    一步步教你用python連接oracle數(shù)據(jù)庫

    oracle作為最強(qiáng)大的數(shù)據(jù)庫,Python也提供了足夠的支持。不過與其他數(shù)據(jù)庫略有不同,下面這篇文章主要給大家介紹了關(guān)于如何使用python連接oracle數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Python之site-packages目錄的位置

    Python之site-packages目錄的位置

    這篇文章主要介紹了Python之site-packages目錄的位置,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Python超詳細(xì)講解內(nèi)存管理機(jī)制

    Python超詳細(xì)講解內(nèi)存管理機(jī)制

    本章主要介紹Pyhon的內(nèi)存管理,以Pyhon的計(jì)數(shù)機(jī)制作為引入,介紹Pyhon的內(nèi)存管理方式,感興趣的朋友來看看吧
    2022-06-06
  • CPython中處理is與is not語句示例詳解

    CPython中處理is與is not語句示例詳解

    這篇文章主要為大家介紹了CPython中處理is與is not語句示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • python通用數(shù)據(jù)庫操作工具 pydbclib的使用簡介

    python通用數(shù)據(jù)庫操作工具 pydbclib的使用簡介

    這篇文章主要介紹了python通用數(shù)據(jù)庫操作工具 pydbclib的使用簡介,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Python中的 enum 模塊源碼詳析

    Python中的 enum 模塊源碼詳析

    這篇文章主要給大家介紹了關(guān)于Python中 enum 模塊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01

最新評論

镇安县| 麻栗坡县| 双鸭山市| 望谟县| 绥阳县| 湘潭市| 象山县| 平湖市| 台北市| 哈密市| 尉犁县| 梁山县| 大宁县| 筠连县| 石楼县| 益阳市| 通州区| 稻城县| 本溪市| 越西县| 化州市| 文水县| 孝感市| 类乌齐县| 昌邑市| 外汇| 乐清市| 黄陵县| 松阳县| 浮梁县| 安顺市| 陵川县| 平乐县| 阿坝县| 宁都县| 三亚市| 老河口市| 巴林右旗| 广灵县| 高安市| 芮城县|