o
    ˷e+
                     @   s@   d Z ddlZddlmZ ddlmZ dgZedd
d	dZdS )zFunction for computing Harmonic function algorithm by Zhu et al.

References
----------
Zhu, X., Ghahramani, Z., & Lafferty, J. (2003, August).
Semi-supervised learning using gaussian fields and harmonic functions.
In ICML (Vol. 3, pp. 912-919).
    N)_get_label_info)not_implemented_forharmonic_functiondirected   labelc                 C   s(  ddl }ddl}ddl}t| }t| |\}}|jd dkr(td| d|jd }	|jd }
||	|
f}|j	dd}d||dk< |j
|j
jd| dd}||  }d||dddf < ||	|
f}d||dddf |dddf f< t|D ]}|| | }q||j|dd  S )	aB  Node classification by Harmonic function

    Parameters
    ----------
    G : NetworkX Graph
    max_iter : int
        maximum number of iterations allowed
    label_name : string
        name of target labels to predict

    Returns
    -------
    predicted : list
        List of length ``len(G)`` with the predicted labels for each node.

    Raises
    ------
    NetworkXError
        If no nodes in `G` have attribute `label_name`.

    Examples
    --------
    >>> from networkx.algorithms import node_classification
    >>> G = nx.path_graph(4)
    >>> G.nodes[0]["label"] = "A"
    >>> G.nodes[3]["label"] = "B"
    >>> G.nodes(data=True)
    NodeDataView({0: {'label': 'A'}, 1: {}, 2: {}, 3: {'label': 'B'}})
    >>> G.edges()
    EdgeView([(0, 1), (1, 2), (2, 3)])
    >>> predicted = node_classification.harmonic_function(G)
    >>> predicted
    ['A', 'A', 'B', 'B']

    References
    ----------
    Zhu, X., Ghahramani, Z., & Lafferty, J. (2003, August).
    Semi-supervised learning using gaussian fields and harmonic functions.
    In ICML (Vol. 3, pp. 912-919).
    r   Nz*No node on the input graph is labeled by 'z'.)axis   g      ?)offsets)numpyscipyscipy.sparsenxto_scipy_sparse_arrayr   shapeNetworkXErrorzerossumsparse	csr_arraydiagstolilrangeargmaxtolist)Gmax_iter
label_namenpspr   Xlabels
label_dict	n_samples	n_classesFdegreesDPB_ r+   b/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/algorithms/node_classification/hmn.pyr      s,   *



$)r   r   )	__doc__networkxr   -networkx.algorithms.node_classification.utilsr   networkx.utils.decoratorsr   __all__r   r+   r+   r+   r,   <module>   s    