Skip to content

FeaturesNormalizedTransform

FeaturesNormalizedTransform(attributes: List[str] = ['node_features'])

Bases: BaseTransform

Methods:

Name Description
process

Normalizes the attributes given in the attributes to sum up to one

Source code in mlx_graphs/transforms/normalize_features.py
def __init__(self, attributes: List[str] = ["node_features"]):
    self.attributes = attributes

process

process(
    data: Union[GraphData, GraphDataBatch],
) -> Union[GraphData, GraphDataBatch]

Normalizes the attributes given in the attributes to sum up to one

Parameters:

Name Type Description Default
data Union[GraphData, GraphDataBatch]

A GraphData object with node/edge features

required

Returns:

Type Description
Union[GraphData, GraphDataBatch]

A GraphData object with normalized features (either edges/node

Union[GraphData, GraphDataBatch]

features unless specified in the transform)

Example:

from mlx_graphs.datasets import EllipticBitcoinDataset
from mlx_graphs.transforms import FeaturesNormalizedTransform

dataset = EllipticBitcoinDataset(transform=FeaturesNormalizedTransform())
# All the node features (if present) for this graph are normalized
Source code in mlx_graphs/transforms/normalize_features.py
def process(
    self, data: Union[GraphData, GraphDataBatch]
) -> Union[GraphData, GraphDataBatch]:
    """Normalizes the attributes given in the attributes to sum up to one

    Args:
        data: A GraphData object with node/edge features

    Returns:
        A GraphData object with normalized features (either edges/node
        features unless specified in the transform)

    Example:

    .. code-block:: python

        from mlx_graphs.datasets import EllipticBitcoinDataset
        from mlx_graphs.transforms import FeaturesNormalizedTransform

        dataset = EllipticBitcoinDataset(transform=FeaturesNormalizedTransform())
        # All the node features (if present) for this graph are normalized
    """
    if isinstance(data, GraphData):
        for attribute in self.attributes:
            array = getattr(data, attribute, None)
            if array is not None and array.size > 0:
                array = array - mx.min(array)
                sum_val = mx.sum(array, axis=-1, keepdims=True)
                setattr(data, attribute, array / mx.clip(sum_val, 1.0, None))
    return data

from mlx_graphs.transforms import FeaturesNormalizedTransform · mlx_graphs/transforms/normalize_features.py