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) |
302 | K_pred = len(pred_classes) |
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 |