Skip to content

Normalizers

Contains a collection of matrix normalizers, i.e., functions that take a matrix as input and return a normalized/stochastic matrix version of it.

normalization_same_eigenvec_centr(A)

Add a dummy node to the matrix and normalize it so that the row sums are equal to one. The dummy node is added in such a way that the first eigenvector of the normalized matrix is the same as the first eigenvector of the original matrix.

Parameters:

  • A (ndarray) –

    Matrix to be normalized.

Returns:

  • ndarray

    Normalized matrix.

Source code in pykda\normalizers.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def normalization_same_eigenvec_centr(A: np.ndarray) -> np.ndarray:
    """
    Add a dummy node to the matrix and normalize it so that the row sums are
    equal to one. The dummy node is added in such a way that the first
    eigenvector of the normalized matrix is the same as the first eigenvector
    of the original matrix.

    Parameters
    ----------
    A : np.ndarray
        Matrix to be normalized.

    Returns
    -------
    np.ndarray
        Normalized matrix.

    """

    rows_sums = A.sum(axis=1, keepdims=True)
    max_row_sum = np.max(rows_sums)

    A_with_dummy = expand_matrix_with_row_and_column(A)  # added at beginning
    eigvec_centr, eigval = eigenvec_centrality(A)
    A_with_dummy[[0], 1:] = eigvec_centr.T
    A_with_dummy[1:, [0]] = max_row_sum - rows_sums

    return standard_row_normalization(A_with_dummy)

normalization_with_self_loops(A)

Add self-loops to each node so that the row sums of A are equal. Afterward apply standard row normalization.

Parameters:

  • A (ndarray) –

    Non-negative matrix to be normalized.

Returns:

  • ndarray

    Normalized matrix.

Source code in pykda\normalizers.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def normalization_with_self_loops(A: np.ndarray) -> np.ndarray:
    """
    Add self-loops to each node so that the row sums of A are equal. Afterward
    apply standard row normalization.

    Parameters
    ----------
    A : np.ndarray
        Non-negative matrix to be normalized.

    Returns
    -------
    np.ndarray
        Normalized matrix.

    """

    assert is_nonnegative_matrix(A), "Ensure the matrix elements are >= 0."

    rows_sums = A.sum(axis=1, keepdims=True)
    max_row_sum = np.max(rows_sums)
    self_loops_matrix = np.diag((max_row_sum - rows_sums).flatten())

    return standard_row_normalization(A + self_loops_matrix)

standard_row_normalization(A)

Normalize the rows of a given matrix so that they sum up to one.

Parameters:

  • A (ndarray) –

    Non-negative matrix with positive row sums to be normalized.

Returns:

  • ndarray

    Normalized matrix.

Source code in pykda\normalizers.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def standard_row_normalization(A: np.ndarray) -> np.ndarray:
    """
    Normalize the rows of a given matrix so that they sum up to one.

    Parameters
    ----------
    A : np.ndarray
        Non-negative matrix with positive row sums to be normalized.

    Returns
    -------
    np.ndarray
        Normalized matrix.

    """

    assert is_nonnegative_matrix(A), "Ensure the matrix elements are >= 0."
    assert has_positive_row_sums(A), "Ensure all row sums are > 0."

    return A / A.sum(axis=1, keepdims=True)