How to use 'confusion matrix sklearn' in Python

Every line of 'confusion matrix sklearn' 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.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
17def numpy(self):
18 return self._matrix.numpy()

Use secure code every time

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

273def 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.
278 Results are identical (and similar computation time) to:
279 "sklearn.metrics.confusion_matrix"
281 However, this function avoids the dependency on sklearn.
283 Parameters
284 ----------
285 y : np.array 1d
286 Contains labels.
287 Assumes s and y contains the same distinct set of labels.
289 s : np.array 1d
290 Contains labels.
291 Assumes s and y contains the same distinct set of labels.
293 Returns
294 -------
295 confusion_matrix : np.array (2D)
296 matrix of confusion counts with true on rows and pred on columns.'''
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)))
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
310 return result

Related snippets