MCPcopy Create free account
hub / github.com/PythonOT/POT / cost_normalization

Function cost_normalization

ot/utils.py:450–492  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

448
449
450def 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
495def dots(*args):

Callers 3

fitMethod · 0.85
transformMethod · 0.85
inverse_transformMethod · 0.85

Calls 4

get_backendFunction · 0.85
medianMethod · 0.45
maxMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected