動手畫混淆矩陣(Confusion Matrix)(含程式碼)
import numpy as np
import matplotlib.pyplot as plt
class DrawConfusionMatrix:
def __init__(self, labels_name, normalize=True):
"""
normalize :是否設元素為百分比形式
"""
self.normalize = normalize
self.labels_name = labels_name
self.num_classes = len(labels_name)
self.matrix = np.zeros((self.num_classes, self.num_classes), dtype="float32")
def update(self, predicts, labels):
"""
:param predicts: 一維預測向量, eg : array([0,5,1,6,3,...],dtype=int64)
:param labels: 一維標籤向量: eg : array([0,5,0,6,2,...],dtype=int64)
:return:
"""
for predict, label in zip(predicts, labels):
self.matrix[predict, label] += 1
def getMatrix(self,normalize=True):
"""
根據傳入的 normalize 判斷要進行 percent 的轉換,
如果 normalize 為 True ,外匯跟單gendan5.com則矩陣元素轉換為百分比形式,
如果 normalize 為 False ,則矩陣元素就為數量
Returns: 返回一個以百分比或者數量為元素的矩陣
"""
if normalize:
per_sum = self.matrix.sum(axis=1) # 計算每行的和,用於百分比計算
for i in range(self.num_classes):
self.matrix[i] =(self.matrix[i] / per_sum[i]) # 百分比轉換
self.matrix=np.around(self.matrix, 2) # 保留 2 位小數點
self.matrix[np.isnan(self.matrix)] = 0 # 可能存在 NaN ,將其設為 0
return self.matrix
def drawMatrix(self):
self.matrix = self.getMatrix(self.normalize)
plt.imshow(self.matrix, cmap=plt.cm.Blues) # 僅畫出顏色格子,沒有值
plt.title("Normalized confusion matrix") # title
plt.xlabel("Predict label")
plt.ylabel("Truth label")
plt.yticks(range(self.num_classes), self.labels_name) # y 軸標籤
plt.xticks(range(self.num_classes), self.labels_name, rotation=45) # x 軸標籤
for x in range(self.num_classes):
for y in range(self.num_classes):
value = float(format('%.2f' % self.matrix[y, x])) # 數值處理
plt.text(x, y, value, verticalalignment='center', horizontalalignment='center') # 寫值
plt.tight_layout() # 自動調整子圖引數,使之填充整個影像區域
plt.colorbar() # 色條
plt.savefig('./ConfusionMatrix.png', bbox_inches='tight') # bbox_inches='tight' 可確保標籤資訊顯示全
plt.show()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2909904/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 混淆矩陣矩陣
- 【矩陣乘法】Matrix Power Series矩陣
- confusion_matrix函式的使用函式
- Cellular Matrix 蜂窩矩陣(一)矩陣
- goldengate 認證矩陣matrixGo矩陣
- Spark Distributed matrix 分散式矩陣Spark分散式矩陣
- HDU 4920 Matrix multiplication(矩陣相乘)矩陣
- Python Numpy的陣列array和矩陣matrixPython陣列矩陣
- [CareerCup] 1.7 Set Matrix Zeroes 矩陣賦零矩陣
- HDU 4965 Fast Matrix Calculation(矩陣快速冪)AST矩陣
- (原創)一般矩陣 Matrix類矩陣
- 非科班程式設計師才不知道的矩陣Matrix程式設計師矩陣
- 旋轉矩陣(Rotate Matrix)的性質分析矩陣
- flutter佈局-5-Matrix4矩陣變換Flutter矩陣
- Flutter 程式碼混淆 混淆Dart程式碼FlutterDart
- 張量(Tensor)、標量(scalar)、向量(vector)、矩陣(matrix)矩陣
- POJ 3233 Matrix Power Series(矩陣+二分)矩陣
- 矩陣分解(MF)方法及程式碼矩陣
- NMF 非負矩陣分解(Non-negative Matrix Factorization)實踐矩陣
- 【程式碼混淆】react-native 程式碼混淆React
- windows 7藍色畫面程式碼含義大全Windows
- 生成螺旋矩陣(方陣、矩陣)矩陣
- ios加固,ios程式碼混淆,ios程式碼混淆工具, iOS原始碼混淆使用說明詳解iOS原始碼
- [CareerCup] 11.6 Search a 2D Matrix 搜尋一個二維矩陣矩陣
- 手把手教你將矩陣&概率畫成圖矩陣
- 高偏差、高方差、低精確率與低召回率、混淆矩陣矩陣
- 51微控制器—矩陣鍵盤-程式碼矩陣
- Android程式碼混淆&元件化混淆方案Android元件化
- 鄰接矩陣、度矩陣矩陣
- 鴻蒙程式碼配置混淆鴻蒙
- Android程式碼混淆Android
- iOS安全/程式碼混淆iOS
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- MPI矩陣向量乘法程式碼《並行程式設計導論》矩陣並行行程程式設計
- Python程式碼混淆工具,Python原始碼保密、加密、混淆Python原始碼加密
- 矩陣矩陣
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 程式碼混淆工具ipaguard:如何使用ipaguard保護和混淆iOS應用程式程式碼iOS