r"""Apply normalization to the loss matrix Parameters ---------- C : ndarray, shape (n1, n2) The cost matrix to normalize. norm : str Type of normalization from 'median', 'max', 'log', 'loglog'. Any other value do not normalize. Returns ------- C
(C, norm=None, return_value=False, value=None)
| 448 | |
| 449 | |
| 450 | def cost_normalization(C, norm=None, return_value=False, value=None): |
| 451 | r"""Apply normalization to the loss matrix |
| 452 | |
| 453 | Parameters |
| 454 | ---------- |
| 455 | C : ndarray, shape (n1, n2) |
| 456 | The cost matrix to normalize. |
| 457 | norm : str |
| 458 | Type of normalization from 'median', 'max', 'log', 'loglog'. Any |
| 459 | other value do not normalize. |
| 460 | |
| 461 | Returns |
| 462 | ------- |
| 463 | C : ndarray, shape (`n1`, `n2`) |
| 464 | The input cost matrix normalized according to given norm. |
| 465 | """ |
| 466 | |
| 467 | nx = get_backend(C) |
| 468 | |
| 469 | if norm is None: |
| 470 | pass |
| 471 | elif norm == "median": |
| 472 | if value is None: |
| 473 | value = nx.median(C) |
| 474 | C /= value |
| 475 | elif norm == "max": |
| 476 | if value is None: |
| 477 | value = nx.max(C) |
| 478 | C /= float(value) |
| 479 | elif norm == "log": |
| 480 | C = nx.log(1 + C) |
| 481 | elif norm == "loglog": |
| 482 | C = nx.log(1 + nx.log(1 + C)) |
| 483 | else: |
| 484 | raise ValueError( |
| 485 | "Norm %s is not a valid option.\n" |
| 486 | "Valid options are:\n" |
| 487 | "median, max, log, loglog" % norm |
| 488 | ) |
| 489 | if return_value: |
| 490 | return C, value |
| 491 | else: |
| 492 | return C |
| 493 | |
| 494 | |
| 495 | def dots(*args): |
no test coverage detected