
    \MhE                        d Z ddlZddgZej                            d           ej        d          dd
                        Zej                            d           ej        d          dd                        Zd Z	dS )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_attrs   labelc                    ddl }ddl}t          j        |           }t	          | |          \  }}|j        d         dk    rt          j        d| d          |j        d         }|j        d         }	|                    ||	f          }
|                    d          }d||dk    <   |j	        
                    |j	                            d|z  d                    }||z                                  }d||dddf         <   |                    ||	f          }d||dddf         |dddf         f<   t          |          D ]
}||
z  |z   }
||                    |
d                                                   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                   g/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/algorithms/node_classification.pyr   r      s   Z 
 ##A(J77FJ|A!GGGG
 
 	
 
I #I
)Y'((A eeemmGGGqL
	BIOOS7]QOGGHHA	
QAAfQQQTlO
)Y'((A$%AfQQQTlF111a4L !8__  UaKbiii**+22444    Gz?c                    ddl }ddl}t          j        |           }t	          | |          \  }}|j        d         dk    rt          j        d| d          |j        d         }	|j        d         }
|                    |	|
f          }|                    d          }d||dk    <   |	                    |j
                            |j
                            d|z  d                              }|||z  |z  z  }|                    |	|
f          }d|z
  ||dddf         |dddf         f<   t          |          D ]
}||z  |z   }||                    |d                                                   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/   r0   r1   s                    r2   r   r   l   s   ^ 
 ##A(J77FJ|A!GGGG
 
 	
 
I #I
)Y'((A eeemmGGGqL	$$RY__cGma_%P%PQQ	R	RB"q&BA
)Y'((A$%IAfQQQTlF111a4L !8__  UaKbiii**+22444r3   c                    ddl }g }i }d}t          |                     d                    D ]H\  }}||d         v r9|d         |         }||vr
|||<   |dz  }|                    |||         g           I|                    |          }|                    d t          |                                d           D                       }	||	fS )	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                     g | ]\  }}|S  r<   ).0r	   r1   s      r2   
<listcomp>z#_get_label_info.<locals>.<listcomp>   s    OOO85!OOOr3   c                     | d         S )Nr   r<   )xs    r2   <lambda>z!_get_label_info.<locals>.<lambda>   s
    1 r3   )key)r   	enumeratenodesappendarraysorteditems)
r#   r   r%   r(   label_to_idlidinr	   r)   s
             r2   r   r      s    " FK
C!''t',,-- 3 311aD$EK''%(E"qMM1k%01222XXfFOOvk&7&7&9&9~~NNNOOO J Jr3   )r   r	   )r4   r   r	   )
__doc__networkxr   __all__utilsnot_implemented_for_dispatchabler   r   r   r<   r3   r2   <module>rS      s    2     >
? j))\***H5 H5 H5 +* *)H5V j))\***I5 I5 I5 +* *)I5X!  !  !  !  ! r3   