Ë
    D^(hE  ã                   óþ   — d Z ddlZddgZej                  j                  d«       ej                  d¬«      dd„«       «       Zej                  j                  d«       ej                  d¬«      dd	„«       «       Zd
„ Z	y)a  This module provides the functions for node classification problem.

The functions in this module are not imported
into the top level `networkx` namespace.
You can access these functions by importing
the `networkx.algorithms.node_classification` modules,
then accessing the functions as attributes of `node_classification`.
For example:

  >>> from networkx.algorithms import node_classification
  >>> G = nx.path_graph(4)
  >>> G.edges()
  EdgeView([(0, 1), (1, 2), (2, 3)])
  >>> G.nodes[0]["label"] = "A"
  >>> G.nodes[3]["label"] = "B"
  >>> node_classification.harmonic_function(G)
  ['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).
é    NÚharmonic_functionÚlocal_and_global_consistencyÚdirectedÚ
label_name)Ú
node_attrsc                 ó¾  — ddl }ddl}t        j                  | «      }t	        | |«      \  }}|j
                  d   dk(  rt        j                  d|› d�«      ‚|j
                  d   }|j
                  d   }	|j                  ||	f«      }
|j                  d¬«      }d||dk(  <   |j                  j                  |j                  j                  d|z  d¬«      «      }||z  j                  «       }d||dd…df   <   |j                  ||	f«      }d||dd…df   |dd…df   f<   t        |«      D ]
  }||
z  |z   }
Œ ||j                  |
d¬«         j                  «       S )	aˆ  Node classification by Harmonic function

    Function for computing Harmonic function algorithm by Zhu et al.

    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   Nú*No node on the input graph is labeled by 'ú'.©Úaxisé   ç      ð?©Úoffsets)ÚnumpyÚscipyÚnxÚto_scipy_sparse_arrayÚ_get_label_infoÚshapeÚNetworkXErrorÚzerosÚsumÚsparseÚ	csr_arrayÚdiagsÚtolilÚrangeÚargmaxÚtolist)ÚGÚmax_iterr   ÚnpÚspÚXÚlabelsÚ
label_dictÚ	n_samplesÚ	n_classesÚFÚdegreesÚDÚPÚBÚ_s                   úe/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/networkx/algorithms/node_classification.pyr   r      sj  € óZ Ûä
× Ñ  Ó#€AÜ(¨¨JÓ7Ñ€FˆJà‡|�|�A�˜!ÒÜ×ÑØ8¸¸ÀBÐGó
ð 	
ð —‘˜‘
€IØ× Ñ  Ñ#€IØ
�‰�)˜YÐ'Ó(€Að �e‰e˜ˆe‹m€GØ€GˆG�q‰LÑà
�	‰	×Ñ˜BŸI™IŸO™O¨S°7©]ÀQ˜OÓGÓH€AØ	
ˆQ‰�‰‹€AØ€A€fŠQ�ˆT�l�Oà
�‰�)˜YÐ'Ó(€AØ$%€A€fŠQ�ˆT�l�Fš1˜a˜4‘LÐ Ñ!ä�8‹_ò ˆØ�‰U�a‰K‰ðð �b—i‘i ¨�iÓ*Ñ+×2Ñ2Ó4Ð4ó    c                 óº  — ddl }ddl}t        j                  | «      }t	        | |«      \  }}|j
                  d   dk(  rt        j                  d|› d�«      ‚|j
                  d   }	|j
                  d   }
|j                  |	|
f«      }|j                  d¬«      }d||dk(  <   |j                  |j                  j                  |j                  j                  d|z  d¬«      «      «      }|||z  |z  z  }|j                  |	|
f«      }d|z
  ||dd…df   |dd…df   f<   t        |«      D ]
  }||z  |z   }Œ ||j                  |d¬«         j                  «       S )	uï  Node classification by Local and Global Consistency

    Function for computing Local and global consistency algorithm by Zhou et al.

    Parameters
    ----------
    G : NetworkX Graph
    alpha : float
        Clamping factor
    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.local_and_global_consistency(G)
    >>> predicted
    ['A', 'A', 'B', 'B']

    References
    ----------
    Zhou, D., Bousquet, O., Lal, T. N., Weston, J., & SchÃ¶lkopf, B. (2004).
    Learning with local and global consistency.
    Advances in neural information processing systems, 16(16), 321-328.
    r   Nr	   r
   r   r   r   r   )r   r   r   r   r   r   r   r   r   Úsqrtr   r   r   r   r   r    )r!   Úalphar"   r   r#   r$   r%   r&   r'   r(   r)   r*   r+   ÚD2r-   r.   r/   s                    r0   r   r   l   si  € ó^ Ûä
× Ñ  Ó#€AÜ(¨¨JÓ7Ñ€FˆJà‡|�|�A�˜!ÒÜ×ÑØ8¸¸ÀBÐGó
ð 	
ð —‘˜‘
€IØ× Ñ  Ñ#€IØ
�‰�)˜YÐ'Ó(€Að �e‰e˜ˆe‹m€GØ€GˆG�q‰LÑà	�‰�—‘×$Ñ$ R§Y¡Y§_¡_°c¸G±mÈa _Ó%PÓQÓ	R€BØ�"�q‘&˜B‘Ñ€Aà
�‰�)˜YÐ'Ó(€AØ$%¨¡I€A€fŠQ�ˆT�l�Fš1˜a˜4‘LÐ Ñ!ä�8‹_ò ˆØ�‰U�a‰K‰ðð �b—i‘i ¨�iÓ*Ñ+×2Ñ2Ó4Ð4r1   c                 óv  — ddl }g }i }d}t        | j                  d¬«      «      D ]9  \  }}||d   v sŒ|d   |   }||vr
|||<   |dz  }|j                  |||   g«       Œ; |j	                  |«      }|j	                  t        |j                  «       d„ ¬«      D ��	cg c]  \  }}	|‘Œ	 c}	}«      }
||
fS c c}	}w )aÄ  Get and return information of labels from the input graph

    Parameters
    ----------
    G : Network X graph
    label_name : string
        Name of the target label

    Returns
    -------
    labels : numpy array, shape = [n_labeled_samples, 2]
        Array of pairs of labeled node ID and label ID
    label_dict : numpy array, shape = [n_classes]
        Array of labels
        i-th element contains the label corresponding label ID `i`
    r   NT)Údatar   c                 ó   — | d   S )Nr   © )Úxs    r0   ú<lambda>z!_get_label_info.<locals>.<lambda>Ù   s
   € ÈÈ1É€ r1   )Úkey)r   Ú	enumerateÚnodesÚappendÚarrayÚsortedÚitems)r!   r   r#   r&   Úlabel_to_idÚlidÚiÚnÚlabelr/   r'   s              r0   r   r   º   s×   € ó" à€FØ€KØ
€CÜ˜!Ÿ'™' t˜'Ó,Ó-ò 3‰ˆˆ1Ø˜˜1™ÒØ�a‘D˜Ñ$ˆEØ˜KÑ'Ø%(�˜EÑ"Ø�q‘�Ø�M‰M˜1˜k¨%Ñ0Ð1Õ2ð3ð �X‰X�fÓ€FØ—‘Ü% k×&7Ñ&7Ó&9¹~ÔN×O‘8�5˜!ŠÓOó€Jð �JÐÐùó 	Ps   ÂB5
)é   rG   )g®Gáz®ï?rH   rG   )
Ú__doc__Únetworkxr   Ú__all__ÚutilsÚnot_implemented_forÚ_dispatchabler   r   r   r9   r1   r0   ú<module>rO      s•   ðñó2 àÐ >Ð
?€ð ‡�×Ñ˜jÓ)Ø€×Ñ˜\Ô*òH5ó +ó *ðH5ðV ‡�×Ñ˜jÓ)Ø€×Ñ˜\Ô*òI5ó +ó *ðI5óX! r1   