Every line of 'confusion matrix python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

By copying the Snyk Code Snippets you agree to

this disclaimer

16 @property 17 def numpy(self): 18 return self._matrix.numpy()

Important

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

25 def __init__(self, classes): 26 self.cm = None 27 self.classes = classes

273 def confusion_matrix(true, pred): 274 '''Implements a confusion matrix for true labels 275 and predicted labels. true and pred MUST BE the same length 276 and have the same distinct set of class labels represtented. 277 278 Results are identical (and similar computation time) to: 279 "sklearn.metrics.confusion_matrix" 280 281 However, this function avoids the dependency on sklearn. 282 283 Parameters 284 ---------- 285 y : np.array 1d 286 Contains labels. 287 Assumes s and y contains the same distinct set of labels. 288 289 s : np.array 1d 290 Contains labels. 291 Assumes s and y contains the same distinct set of labels. 292 293 Returns 294 ------- 295 confusion_matrix : np.array (2D) 296 matrix of confusion counts with true on rows and pred on columns.''' 297 298 assert(len(true) == len(pred)) 299 true_classes = np.unique(true) 300 pred_classes = np.unique(pred) 301 K_true = len(true_classes) # Number of classes in true 302 K_pred = len(pred_classes) # Number of classes in pred 303 map_true = dict(zip(true_classes, range(K_true))) 304 map_pred = dict(zip(pred_classes, range(K_pred))) 305 306 result = np.zeros((K_true, K_pred)) 307 for i in range(len(true)): 308 result[map_true[true[i]]][map_pred[pred[i]]] += 1 309 310 return result