Computes the AUC explicitly using Numpy. Args: predictions: an ndarray with shape [N]. labels: an ndarray with shape [N]. weights: an ndarray with shape [N]. Returns: the area under the ROC curve.
(self, predictions, labels, weights)
| 1357 | self.assertAlmostEqual(1, auc.eval(), 6) |
| 1358 | |
| 1359 | def np_auc(self, predictions, labels, weights): |
| 1360 | """Computes the AUC explicitly using Numpy. |
| 1361 | |
| 1362 | Args: |
| 1363 | predictions: an ndarray with shape [N]. |
| 1364 | labels: an ndarray with shape [N]. |
| 1365 | weights: an ndarray with shape [N]. |
| 1366 | |
| 1367 | Returns: |
| 1368 | the area under the ROC curve. |
| 1369 | """ |
| 1370 | if weights is None: |
| 1371 | weights = np.ones(np.size(predictions)) |
| 1372 | is_positive = labels > 0 |
| 1373 | num_positives = np.sum(weights[is_positive]) |
| 1374 | num_negatives = np.sum(weights[~is_positive]) |
| 1375 | |
| 1376 | # Sort descending: |
| 1377 | inds = np.argsort(-predictions) |
| 1378 | |
| 1379 | sorted_labels = labels[inds] |
| 1380 | sorted_weights = weights[inds] |
| 1381 | is_positive = sorted_labels > 0 |
| 1382 | |
| 1383 | tp = np.cumsum(sorted_weights * is_positive) / num_positives |
| 1384 | return np.sum((sorted_weights * tp)[~is_positive]) / num_negatives |
| 1385 | |
| 1386 | @test_util.run_deprecated_v1 |
| 1387 | def testWithMultipleUpdates(self): |
no test coverage detected