
    0Ph              
          d Z ddlZddlmZmZmZmZ ddlmZm	Z	 ddl
ZddlmZ ddlmZ ddlmZmZmZmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& ddl'm(Z(m)Z) ddl*m+Z, ddl-m.Z. d Z/d Z0 e$dgg d e"eddd          dgdgdd          ddddd             Z1	 	 	 	 	 d-d#Z2d$ Z3d% Z4d& Z5 e6e1e3e4e5'          Z7d( Z8 G d) d*ee          Z9 G d+ d,ee.e9          Z:dS ).zHierarchical Agglomerative Clustering

These routines perform some hierarchical agglomerative clustering of some
input data.

Authors : Vincent Michel, Bertrand Thirion, Alexandre Gramfort,
          Gael Varoquaux
License: BSD 3 clause
    N)heapifyheappopheappushheappushpop)IntegralReal)sparse)connected_components   )BaseEstimatorClassNamePrefixFeaturesOutMixinClusterMixin_fit_context)DistanceMetric)METRIC_MAPPING64)_VALID_METRICSpaired_distances)check_array)IntFloatDict)
HasMethodsInterval
StrOptionsvalidate_params)_fix_connected_components)check_memoryvalidate_data   )_hierarchical_fast)AgglomerationTransformc                    | j         d         }|j         d         |k    s|j         d         |k    rt          d|j         d| j                   ||j        z   }t          j        |          st          j        |          }|j        dk    r|                                }t          |          \  }}|dk    r.t          j
        d|z  d           t          | ||||d	
          }||fS )aR  
    Fixes the connectivity matrix.

    The different steps are:

    - copies it
    - makes it symmetric
    - converts it to LIL if necessary
    - completes it if necessary.

    Parameters
    ----------
    X : array-like of shape (n_samples, n_features)
        Feature matrix representing `n_samples` samples to be clustered.

    connectivity : sparse matrix, default=None
        Connectivity matrix. Defines for each sample the neighboring samples
        following a given structure of the data. The matrix is assumed to
        be symmetric and only the upper triangular half is used.
        Default is `None`, i.e, the Ward algorithm is unstructured.

    affinity : {"euclidean", "precomputed"}, default="euclidean"
        Which affinity to use. At the moment `precomputed` and
        ``euclidean`` are supported. `euclidean` uses the
        negative squared Euclidean distance between points.

    Returns
    -------
    connectivity : sparse matrix
        The fixed connectivity matrix.

    n_connected_components : int
        The number of connected components in the graph.
    r   r   z%Wrong shape for connectivity matrix: z when X is lilzxthe number of connected components of the connectivity matrix is %d > 1. Completing it to avoid stopping the tree early.r   
stacklevelconnectivity)Xgraphn_connected_componentscomponent_labelsmetricmode)shape
ValueErrorTr	   issparse
lil_matrixformattolilr
   warningswarnr   )r%   r$   affinity	n_samplesr'   labelss         ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/cluster/_agglomerative.py_fix_connectivityr8   2   s5   F 
I!	))\-?-Bi-O-Oj!!!177,
 
 	
  ,.0L ?<(( 7(66 e###))++ &:,%G%G"F!!')?@ 		
 	
 	
 	
 1#9#
 
 
 ///    c                 N   ddl m} |                     t          j        d          } t          j        | j        j                  j        }|| j        | j        dk    <    || 	                                          }|
                                }d|j        |j        |k    <   t          j        |j        |j        |j        g          j        }	|	t          j        |	j        d         d          d	d	f         }	t!          j        |	          }
|
d	d	d	df                             t$                    }t          j        |t          j                  }t+          ||          D ](\  }\  }}|||k    r n||k     r|||<   ||k     r|||<   )|r|
d	d	df         }|||||fS ||||fS )
z
    Perform single linkage clustering on sparse data via the minimum
    spanning tree from scipy.sparse.csgraph, then using union-find to label.
    The parent array is then generated by walking through the tree.
    r   )minimum_spanning_treeFcopydtyper   	mergesortkindN)scipy.sparse.csgraphr;   astypenpfloat64finfodatar?   epstocsrtocoovstackrowcolr-   argsort_hierarchical_single_linkage_labelintarangeintp	enumerate)r$   r5   n_nodes
n_clustersr'   return_distancer;   epsilon_valuemst	mst_arraysingle_linkage_tree	children_parentileftright	distancess                    r7   _single_linkage_treerc   ~   s    ;:::::  &&rz&>>L H<#4#:;;;?M0=Ll'1,-  
 2 2 4 4
5
5C ))++C +,CHSX&'	37CGSX6779I "*Y[^+FFFIJI (=iHH#AAArrE*11#66I Ywbg...F%i;;  =D%!a7llE'>>F4L7??F5M O'1-	0)VYNN,i??r9   
array-like)rd   sparse matrixNr`   closedboolean)r%   r$   rW   rX   Tprefer_skip_nested_validationFr$   rW   rX   c                ,
   t          j        |           } | j        dk    rt          j        | d          } | j        \  }}|ddlm} |t          j        dd           t          j	        | d	
          } |
                    |           }|ddddf                             t           j                  }|r|dddf         }	|d|d|	fS |d|dfS t          | |d          \  }}
|	d|z  dz
  }n"||k    rt          d||fz            d|z  |z
  }g }g g t          |j                  D ]c\  }                    |           fd|D             }|                    t'          |          gz                                 |           dt          j        |t           j        d          }t          j        t           j        d          t          j        |d          }d|d|<   t          j        ||fd          }| |d|<   t          j        t'          |          t           j        d          t1          j        |||           t5          t7          |                    t9                     t          j        |t           j                  }t          j        |t>                    }g }|rt          j        ||z
            }	t          j        |t>          d          }tA          ||          D ]Ê	 tC                    \  }}}||         r	||         rn%c||<   ||<   |                    ||f           dx||<   ||<   |r||	|z
  <   ||         ||         z   |<   ||         ||         z   |<   g |"                    d           d|<   t1          j#        |         ||           t1          j#        |         ||           fdD                                             t          j        t           j        d          t          j        j        t           j        d          }|"                               t'          |          }t          j        |t           j        d          t1          j        |||           fdtA          |          D              |}d |D             }t          j        |          }|rt          j$        d|	z            }	||
|||	fS ||
||fS )a@  Ward clustering based on a Feature matrix.

    Recursively merges the pair of clusters that minimally increases
    within-cluster variance.

    The inertia matrix uses a Heapq-based representation.

    This is the structured version, that takes into account some topological
    structure between samples.

    Read more in the :ref:`User Guide <hierarchical_clustering>`.

    Parameters
    ----------
    X : array-like of shape (n_samples, n_features)
        Feature matrix representing `n_samples` samples to be clustered.

    connectivity : {array-like, sparse matrix}, default=None
        Connectivity matrix. Defines for each sample the neighboring samples
        following a given structure of the data. The matrix is assumed to
        be symmetric and only the upper triangular half is used.
        Default is None, i.e, the Ward algorithm is unstructured.

    n_clusters : int, default=None
        `n_clusters` should be less than `n_samples`.  Stop early the
        construction of the tree at `n_clusters.` This is useful to decrease
        computation time if the number of clusters is not small compared to the
        number of samples. In this case, the complete tree is not computed, thus
        the 'children' output is of limited use, and the 'parents' output should
        rather be used. This option is valid only when specifying a connectivity
        matrix.

    return_distance : bool, default=False
        If `True`, return the distance between the clusters.

    Returns
    -------
    children : ndarray of shape (n_nodes-1, 2)
        The children of each non-leaf node. Values less than `n_samples`
        correspond to leaves of the tree which are the original samples.
        A node `i` greater than or equal to `n_samples` is a non-leaf
        node and has children `children_[i - n_samples]`. Alternatively
        at the i-th iteration, children[i][0] and children[i][1]
        are merged to form node `n_samples + i`.

    n_connected_components : int
        The number of connected components in the graph.

    n_leaves : int
        The number of leaves in the tree.

    parents : ndarray of shape (n_nodes,) or None
        The parent of each node. Only returned when a connectivity matrix
        is specified, elsewhere 'None' is returned.

    distances : ndarray of shape (n_nodes-1,)
        Only returned if `return_distance` is set to `True` (for compatibility).
        The distances between the centers of the nodes. `distances[i]`
        corresponds to a weighted Euclidean distance between
        the nodes `children[i, 1]` and `children[i, 2]`. If the nodes refer to
        leaves of the tree, then `distances[i]` is their unweighted Euclidean
        distance. Distances are updated in the following way
        (from scipy.hierarchy.linkage):

        The new entry :math:`d(u,v)` is computed as follows,

        .. math::

           d(u,v) = \sqrt{\frac{|v|+|s|}
                               {T}d(v,s)^2
                        + \frac{|v|+|t|}
                               {T}d(v,t)^2
                        - \frac{|v|}
                               {T}d(s,t)^2}

        where :math:`u` is the newly joined cluster consisting of
        clusters :math:`s` and :math:`t`, :math:`v` is an unused
        cluster in the forest, :math:`T=|v|+|s|+|t|`, and
        :math:`|*|` is the cardinality of its argument. This is also
        known as the incremental algorithm.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.cluster import ward_tree
    >>> X = np.array([[1, 2], [1, 4], [1, 0],
    ...               [4, 2], [4, 4], [4, 0]])
    >>> children, n_connected_components, n_leaves, parents = ward_tree(X)
    >>> children
    array([[0, 1],
           [3, 5],
           [2, 6],
           [4, 7],
           [8, 9]])
    >>> n_connected_components
    1
    >>> n_leaves
    6
    r   r   Nr   	hierarchyPartial build of the tree is implemented only for structured clustering (i.e. with explicit connectivity). The algorithm will build the full tree and only retain the lower branches required for the specified number of clustersr   r"   W)requirements	euclideanr4   z]Cannot provide more clusters than samples. %i n_clusters was asked, and there are %i samples.c                      g | ]
}|k     |S  rw   ).0r_   inds     r7   
<listcomp>zward_tree.<locals>.<listcomp>[  s    )))QSqr9   C)r?   order)r|   r>   TFc                 F    g | ]}|                                        S rw   )append)rx   rN   Aks     r7   rz   zward_tree.<locals>.<listcomp>  s)    ///c3q		///r9   c           	      N    g | ]!}t          |         |         f          "S rw   )r   )rx   idx	coord_colinertiainir   s     r7   rz   zward_tree.<locals>.<listcomp>  s2    XXXc'CHa38	9	9XXXr9   c                 $    g | ]}|d d d         S )Nrn   rw   )rx   cs     r7   rz   zward_tree.<locals>.<listcomp>  s"    ***A$$B$***r9   g       @)%rE   asarrayndimreshaper+   scipy.clusterrp   r2   r3   requirewardrD   rT   r8   r,   rU   rowsr~   extendlenarrayzerosemptyrF   rP   compute_ward_distlistzipr   rS   onesboolranger   fill_get_parentssqrt)r%   r$   rW   rX   r5   
n_featuresrp   outr]   rb   r'   rV   	coord_rowrM   	moments_1	moments_2r^   	used_nodechildrennot_visitedinertr_   jn_additionsn_leavesr   r   ry   r   r   r   s                            @@@@@@r7   	ward_treer      s   Z 	
1Av{{Jq'""GIz++++++!M; 
 
 
 
 Jqs+++nnQ2A2J%%bg..	 	1AAAqD	IaD);;aD00+<	<+, , ,(L( i-!#	!!()45  
 i-*, II
Al/00  S	 *)))#)))HH	
 	
 	
 	"'===I"'===I ,,,IIjyj':.c:::IIjyjhs9~~RZsCCCG#Iy)YPWXXX3w	95566GG Ywbg...Ft,,,IH 2HWy011	(7$c:::K 9g&& "Y "Y	!'**KE1a| 	! 	  !!q	6!9A&++	!y| 	-',Ia)m$ !|il2	! |il2	! 	A"1Q4FKHHH"1Q4FKHHH/////Y////	HYbgSAAA	HY_BG3GGG	q)nnh{"*C@@@'	9iTWXXX 	YXXXXXXU;EWEWXXXXX H*****Hx!!H BGC)O,,	/69LL/6AAr9   completert   c           
        % t          j        |           } | j        dk    rt          j        | d          } | j        \  }}t
          j        t
          j        dd}	 ||         }	n;# t          $ r.}
t          d|
                                d|d          |
d}
~
ww xY w|dk    r8t          j        t          j        | d	                     rt          d
          |ddlm} |t          j        dd           |dk    rb| j        d         | j        d         k    rt          d| j                   t          j        | j        d         d          \  }}| ||f         } nX|dk    rd}nO|dv rd}nHt#          |          r9 ||           } t          j        | j        d         d          \  }}| ||f         } |dk    r|dk    rt#          |          s|t$          v rt'          j        |          }t          j        | t           j                  } t          j        | |          }|t          j        |j        d         d          ddf         }t          j        |          }n|                    | ||          }|ddddf                             t:          d          }|r|dddf         }|d|d|fS |d|dfS t=          | ||          \  }}|                                }|j         |j!        k    }|j         |         |_         |j!        |         |_!        |j"        |         |_"        ~|dk    r4| |j         |j!        f                             t           j#        d          }n(tI          | |j                  | |j!                 |          }||_"        |	d|z  dz
  }n||k    sJ d|z  |z
  }|dk    rtK          ||||||          S |rt          j&        ||z
            }t          j&        |tN                    }tQ                      }|)                                }tU          tW          |j"        |j,                            D ]\  %\  }}t[          t          j        |t           j.                  t          j        |t           j#                            |%<   |/                    %fd tW          ||          D                        ~ta          |           t          j1        |t           j.                  }t          j2        |t           j.                  }g }tg          ||          D ]}	 ti          |          }||j5                 r||j6                 rn+|j5        }|j6        }|r|j7        |||z
  <   |x||<   ||<   |8                    ||f           ||         }||         } || z   ||<   dx||<   ||<    |	||         ||         |||           }!|!D ]E\  }"}#||"         8                    ||#           ts          |t          j:        |#||"                     F|!||<   dx||<   ||<   |}$t          j;        |          ddddd"f         }|r|||$||fS |||$|fS )#a  Linkage agglomerative clustering based on a Feature matrix.

    The inertia matrix uses a Heapq-based representation.

    This is the structured version, that takes into account some topological
    structure between samples.

    Read more in the :ref:`User Guide <hierarchical_clustering>`.

    Parameters
    ----------
    X : array-like of shape (n_samples, n_features)
        Feature matrix representing `n_samples` samples to be clustered.

    connectivity : sparse matrix, default=None
        Connectivity matrix. Defines for each sample the neighboring samples
        following a given structure of the data. The matrix is assumed to
        be symmetric and only the upper triangular half is used.
        Default is `None`, i.e, the Ward algorithm is unstructured.

    n_clusters : int, default=None
        Stop early the construction of the tree at `n_clusters`. This is
        useful to decrease computation time if the number of clusters is
        not small compared to the number of samples. In this case, the
        complete tree is not computed, thus the 'children' output is of
        limited use, and the 'parents' output should rather be used.
        This option is valid only when specifying a connectivity matrix.

    linkage : {"average", "complete", "single"}, default="complete"
        Which linkage criteria to use. The linkage criterion determines which
        distance to use between sets of observation.
            - "average" uses the average of the distances of each observation of
              the two sets.
            - "complete" or maximum linkage uses the maximum distances between
              all observations of the two sets.
            - "single" uses the minimum of the distances between all
              observations of the two sets.

    affinity : str or callable, default='euclidean'
        Which metric to use. Can be 'euclidean', 'manhattan', or any
        distance known to paired distance (see metric.pairwise).

    return_distance : bool, default=False
        Whether or not to return the distances between the clusters.

    Returns
    -------
    children : ndarray of shape (n_nodes-1, 2)
        The children of each non-leaf node. Values less than `n_samples`
        correspond to leaves of the tree which are the original samples.
        A node `i` greater than or equal to `n_samples` is a non-leaf
        node and has children `children_[i - n_samples]`. Alternatively
        at the i-th iteration, children[i][0] and children[i][1]
        are merged to form node `n_samples + i`.

    n_connected_components : int
        The number of connected components in the graph.

    n_leaves : int
        The number of leaves in the tree.

    parents : ndarray of shape (n_nodes, ) or None
        The parent of each node. Only returned when a connectivity matrix
        is specified, elsewhere 'None' is returned.

    distances : ndarray of shape (n_nodes-1,)
        Returned when `return_distance` is set to `True`.

        distances[i] refers to the distance between children[i][0] and
        children[i][1] when they are merged.

    See Also
    --------
    ward_tree : Hierarchical clustering with ward linkage.
    r   rm   N)r   averagesinglez1Unknown linkage option, linkage should be one of z, but z
 was givencosine)axisz;Cosine affinity cannot be used when X contains zero vectorsr   ro   rq   r   r"   precomputedz6Distance matrix should be square, got matrix of shape )r   l2rt   )l1	manhattan	cityblockr   r>   r@   rA   )methodr)   Fr<   ru   )r)   c              3   V   K   | ]#\  }}|k     t          j        ||          V  $d S N)rP   WeightedEdge)rx   rdry   s      r7   	<genexpr>zlinkage_tree.<locals>.<genexpr>  sK       
 
6:aPQTWPWPWM&q#q11PWPWPWPW
 
r9   Trn   )<rE   r   r   r   r+   rP   	max_mergeaverage_mergeKeyErrorr,   keysanyr   rp   r2   r3   triu_indicescallabler   r   
get_metricascontiguousarraydoublemst_linkage_corerO   r-   single_linkage_labellinkagerD   rR   r8   rK   rM   rN   rH   rF   r   rc   r   objectr   r1   rU   r   r   r   rT   r   r   rS   r   r   r   abweightr~   r   r   r   )&r%   r$   rW   r   r4   rX   r5   r   linkage_choices	join_funcerp   r_   r   dist_metricrZ   r   r]   rb   r'   	diag_maskrV   r   r   rH   rM   r^   r   r   r   edgen_in_jr   rN   r   r   ry   s&                                        @r7   linkage_treer     s   f 	
1Av{{Jq'""GIz "+ . O
#G,		   j##%%%%www0
 
 	 8qq(9(9(9'9 : :VWWW++++++!M; 
 
 
 
 }$$ wqzQWQZ'' VQWVV   ?171:333DAq!Q$AA"HH,,,"HHh 	A?171:333DAq!Q$AxM))X&& *,,, )3H==K $Qbi888A0K@@Cbjq<<<aaa?@C  4S99CC##Agh#GGC2A2J%%c%66	 	<AAAqD	IaD);;!Y,,+<	<(, , ,(L(  %%''L L$44I#'	2L#'	2L$))4L=  l&(889@@RW@XX		 %l<#3!4X
 
 
	 "Li-!#Y&&&&i-*,(#"
 
 	
  2HWy011	
'''AffG  %%''L%c,*;\=N&O&OPP 
 
[dCJs"'***BJt2:,N,N,N
 
#
 	 
 
 
 
>A#tnn
 
 
 	
 	
 	
 	
 	G Ywbg...Frw///IH 9g&&  	7##D  Ytv%6 	 FF 	3'+{Ia)m$ !!q	F1IAllSy	!&++	!y| IadAaD)S#>>	 	E 	EFCcFMM!Q Wm8AsCCDDDD!!qtt H x!!!!!TTrT'*H M/69LL+Xv==s   A$ $
B.)BBc                  $    d|d<   t          | i |S )Nr   r   r   argskwargss     r7   _complete_linkager     s    "F9((((r9   c                  $    d|d<   t          | i |S )Nr   r   r   r   s     r7   _average_linkager     s    !F9((((r9   c                  $    d|d<   t          | i |S )Nr   r   r   r   s     r7   _single_linkager     s     F9((((r9   )r   r   r   r   c                    | |k    rt          d|  d| d          t          |d                   dz    g}t          | dz
            D ]B}||d          |z
           }t          ||d                     t	          ||d                     Ct          j        |t
          j                  }t          |          D ]\  }}||t          j
        | ||          <    |S )a  Function cutting the ward tree for a given number of clusters.

    Parameters
    ----------
    n_clusters : int or ndarray
        The number of clusters to form.

    children : ndarray of shape (n_nodes-1, 2)
        The children of each non-leaf node. Values less than `n_samples`
        correspond to leaves of the tree which are the original samples.
        A node `i` greater than or equal to `n_samples` is a non-leaf
        node and has children `children_[i - n_samples]`. Alternatively
        at the i-th iteration, children[i][0] and children[i][1]
        are merged to form node `n_samples + i`.

    n_leaves : int
        Number of leaves of the tree.

    Returns
    -------
    labels : array [n_samples]
        Cluster labels for each point.
    z+Cannot extract more clusters than samples: z% clusters were given for a tree with z leaves.rn   r   r   r>   )r,   maxr   r   r   rE   r   rT   rU   rP   _hc_get_descendent)	rW   r   r   nodes_these_childrenlabelr_   nodes	            r7   _hc_cutr     s"   0 HSS S@HS S S
 
 	
 8B<  1$%&E:>"" / /!58)h"67**+++EN1--....HXRW---EU## O O4MNm.uhIIJJLr9   c                       e Zd ZU dZ eeddd          dg e ee          dhz            e	ge
 ed          dgdd	e	dg ed
h          dg e ee                                                    g eeddd          dgdgdZeed<   	 ddddd
dddddZ ed          dd            Zd Zd fd	Z xZS )AgglomerativeClusteringa  
    Agglomerative Clustering.

    Recursively merges pair of clusters of sample data; uses linkage distance.

    Read more in the :ref:`User Guide <hierarchical_clustering>`.

    Parameters
    ----------
    n_clusters : int or None, default=2
        The number of clusters to find. It must be ``None`` if
        ``distance_threshold`` is not ``None``.

    metric : str or callable, default="euclidean"
        Metric used to compute the linkage. Can be "euclidean", "l1", "l2",
        "manhattan", "cosine", or "precomputed". If linkage is "ward", only
        "euclidean" is accepted. If "precomputed", a distance matrix is needed
        as input for the fit method. If connectivity is None, linkage is
        "single" and affinity is not "precomputed" any valid pairwise distance
        metric can be assigned.

        .. versionadded:: 1.2

    memory : str or object with the joblib.Memory interface, default=None
        Used to cache the output of the computation of the tree.
        By default, no caching is done. If a string is given, it is the
        path to the caching directory.

    connectivity : array-like, sparse matrix, or callable, default=None
        Connectivity matrix. Defines for each sample the neighboring
        samples following a given structure of the data.
        This can be a connectivity matrix itself or a callable that transforms
        the data into a connectivity matrix, such as derived from
        `kneighbors_graph`. Default is ``None``, i.e, the
        hierarchical clustering algorithm is unstructured.

        For an example of connectivity matrix using
        :class:`~sklearn.neighbors.kneighbors_graph`, see
        :ref:`sphx_glr_auto_examples_cluster_plot_agglomerative_clustering.py`.

    compute_full_tree : 'auto' or bool, default='auto'
        Stop early the construction of the tree at ``n_clusters``. This is
        useful to decrease computation time if the number of clusters is not
        small compared to the number of samples. This option is useful only
        when specifying a connectivity matrix. Note also that when varying the
        number of clusters and using caching, it may be advantageous to compute
        the full tree. It must be ``True`` if ``distance_threshold`` is not
        ``None``. By default `compute_full_tree` is "auto", which is equivalent
        to `True` when `distance_threshold` is not `None` or that `n_clusters`
        is inferior to the maximum between 100 or `0.02 * n_samples`.
        Otherwise, "auto" is equivalent to `False`.

    linkage : {'ward', 'complete', 'average', 'single'}, default='ward'
        Which linkage criterion to use. The linkage criterion determines which
        distance to use between sets of observation. The algorithm will merge
        the pairs of cluster that minimize this criterion.

        - 'ward' minimizes the variance of the clusters being merged.
        - 'average' uses the average of the distances of each observation of
          the two sets.
        - 'complete' or 'maximum' linkage uses the maximum distances between
          all observations of the two sets.
        - 'single' uses the minimum of the distances between all observations
          of the two sets.

        .. versionadded:: 0.20
            Added the 'single' option

        For examples comparing different `linkage` criteria, see
        :ref:`sphx_glr_auto_examples_cluster_plot_linkage_comparison.py`.

    distance_threshold : float, default=None
        The linkage distance threshold at or above which clusters will not be
        merged. If not ``None``, ``n_clusters`` must be ``None`` and
        ``compute_full_tree`` must be ``True``.

        .. versionadded:: 0.21

    compute_distances : bool, default=False
        Computes distances between clusters even if `distance_threshold` is not
        used. This can be used to make dendrogram visualization, but introduces
        a computational and memory overhead.

        .. versionadded:: 0.24

        For an example of dendrogram visualization, see
        :ref:`sphx_glr_auto_examples_cluster_plot_agglomerative_dendrogram.py`.

    Attributes
    ----------
    n_clusters_ : int
        The number of clusters found by the algorithm. If
        ``distance_threshold=None``, it will be equal to the given
        ``n_clusters``.

    labels_ : ndarray of shape (n_samples)
        Cluster labels for each point.

    n_leaves_ : int
        Number of leaves in the hierarchical tree.

    n_connected_components_ : int
        The estimated number of connected components in the graph.

        .. versionadded:: 0.21
            ``n_connected_components_`` was added to replace ``n_components_``.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    children_ : array-like of shape (n_samples-1, 2)
        The children of each non-leaf node. Values less than `n_samples`
        correspond to leaves of the tree which are the original samples.
        A node `i` greater than or equal to `n_samples` is a non-leaf
        node and has children `children_[i - n_samples]`. Alternatively
        at the i-th iteration, children[i][0] and children[i][1]
        are merged to form node `n_samples + i`.

    distances_ : array-like of shape (n_nodes-1,)
        Distances between nodes in the corresponding place in `children_`.
        Only computed if `distance_threshold` is used or `compute_distances`
        is set to `True`.

    See Also
    --------
    FeatureAgglomeration : Agglomerative clustering but for features instead of
        samples.
    ward_tree : Hierarchical clustering with ward linkage.

    Examples
    --------
    >>> from sklearn.cluster import AgglomerativeClustering
    >>> import numpy as np
    >>> X = np.array([[1, 2], [1, 4], [1, 0],
    ...               [4, 2], [4, 4], [4, 0]])
    >>> clustering = AgglomerativeClustering().fit(X)
    >>> clustering
    AgglomerativeClustering()
    >>> clustering.labels_
    array([1, 1, 1, 0, 0, 0])
    r   Nr`   rf   r   cacherd   re   autorh   r   )rW   r)   memoryr$   compute_full_treer   distance_thresholdcompute_distances_parameter_constraintsr   rt   r   F)r)   r   r$   r   r   r   r   c                v    || _         || _        || _        || _        || _        || _        || _        || _        d S r   )rW   r   r   r$   r   r   r)   r   )	selfrW   r)   r   r$   r   r   r   r   s	            r7   __init__z AgglomerativeClustering.__init__  sF     %"4(!2!2r9   Tri   c                 P    t          | |d          }|                     |          S )a   Fit the hierarchical clustering from features, or distance matrix.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features) or                 (n_samples, n_samples)
            Training instances to cluster, or distances between instances if
            ``metric='precomputed'``.

        y : Ignored
            Not used, present here for API consistency by convention.

        Returns
        -------
        self : object
            Returns the fitted instance.
        r   )ensure_min_samples)r   _fit)r   r%   ys      r7   fitzAgglomerativeClustering.fit  s(    & $a888yy||r9   c                    t          | j                  }| j        du | j        du z  st	          d          | j        | j        st	          d          | j        dk    r"| j        dk    rt	          | j         d          t          | j                 }| j	        }| j	        <t          | j	                  r| 	                    |          }t          |g d          }t          |          }| j        }| j	        d	}|d
k    r&| j        d	}n| j        t          dd|z            k     }| j        }|rd}i }| j        dk    r| j        |d<   | j        |d<   | j        }	|	dup| j        }
 |                    |          |f|||
d|}|dd         \  | _        | _        | _        }|
r|d         | _        | j        &t)          j        | j        |	k              dz   | _        n| j        | _        |r&t/          | j        | j        | j                  | _        n^t3          j        |d          }t)          j        |d|                   }t)          j        t)          j        |          |          | _        | S )ah  Fit without validation

        Parameters
        ----------
        X : ndarray of shape (n_samples, n_features) or (n_samples, n_samples)
            Training instances to cluster, or distances between instances if
            ``metric='precomputed'``.

        Returns
        -------
        self : object
            Returns the fitted instance.
        Nz_Exactly one of n_clusters and distance_threshold has to be set, and the other needs to be None.z<compute_full_tree must be True if distance_threshold is set.r   rt   zE was provided as metric. Ward can only work with euclidean distances.)csrcoor!   )accept_sparseTr   d   g{Gz?r   r4   rk      rn   r   Fr<   )r   r   rW   r   r,   r   r   r)   _TREE_BUILDERSr$   r   r   r   r   r   r   r]   n_connected_components_	n_leaves_
distances_rE   count_nonzeron_clusters_r   labels_rP   hc_get_headsr=   searchsortedunique)r   r%   r   tree_builderr$   r5   r   rW   r   r   rX   r   parentsr6   s                 r7   r   zAgglomerativeClustering._fit  s    dk**D(T-D-LM 	$   ".t7M.N   <6!!dk[&@&@; 1 1 1  
 &dl3(()** 4#0033&,A,A,A  L FF	 2$ $&&&2$(!!
 %)Oc#ti?O6P6P$P!_
 	J <6!! $F9!%F:!4-T9Td>T(fll<((
%!+	
 

 
 
 SVQBS
O5t~w  	&!"gDO". 4F!FGG!K   $D  	F"4#3T^T^TTDLL"/eDDDFWVJYJ/00F?29V+<+<fEEDLr9   c                 H    t                                          ||          S )a  Fit and return the result of each sample's clustering assignment.

        In addition to fitting, this method also return the result of the
        clustering assignment for each sample in the training set.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features) or                 (n_samples, n_samples)
            Training instances to cluster, or distances between instances if
            ``affinity='precomputed'``.

        y : Ignored
            Not used, present here for API consistency by convention.

        Returns
        -------
        labels : ndarray of shape (n_samples,)
            Cluster labels.
        )superfit_predictr   r%   r   	__class__s      r7   r  z#AgglomerativeClustering.fit_predictE  s    * ww""1a(((r9   r   r   )__name__
__module____qualname____doc__r   r   r   setr   r   strr   r   r   r   r   dict__annotations__r   r   r   r   r  __classcell__r  s   @r7   r   r     s        T Tn  x!T&AAA4HJss>**m_<==
 

7++T2%$G(j&22I>Jss>#6#6#8#899::;'xafEEEtL'[$ $D     3  3 3 3 3 3* \555   65*d d dL) ) ) ) ) ) ) ) ) )r9   r   c                       e Zd ZU dZ eeddd          dg e ee          dhz            e	ge
 ed          dgdd	e	dg ed
h          dg e ee                                                    ge	g eeddd          dgdgd	Zeed<   	 ddddd
dej        ddd fdZ ed          d fd	            Zed             Z xZS )FeatureAgglomerationa  Agglomerate features.

    Recursively merges pair of clusters of features.

    Refer to
    :ref:`sphx_glr_auto_examples_cluster_plot_feature_agglomeration_vs_univariate_selection.py`
    for an example comparison of :class:`FeatureAgglomeration` strategy with a
    univariate feature selection strategy (based on ANOVA).

    Read more in the :ref:`User Guide <hierarchical_clustering>`.

    Parameters
    ----------
    n_clusters : int or None, default=2
        The number of clusters to find. It must be ``None`` if
        ``distance_threshold`` is not ``None``.

    metric : str or callable, default="euclidean"
        Metric used to compute the linkage. Can be "euclidean", "l1", "l2",
        "manhattan", "cosine", or "precomputed". If linkage is "ward", only
        "euclidean" is accepted. If "precomputed", a distance matrix is needed
        as input for the fit method.

        .. versionadded:: 1.2

    memory : str or object with the joblib.Memory interface, default=None
        Used to cache the output of the computation of the tree.
        By default, no caching is done. If a string is given, it is the
        path to the caching directory.

    connectivity : array-like, sparse matrix, or callable, default=None
        Connectivity matrix. Defines for each feature the neighboring
        features following a given structure of the data.
        This can be a connectivity matrix itself or a callable that transforms
        the data into a connectivity matrix, such as derived from
        `kneighbors_graph`. Default is `None`, i.e, the
        hierarchical clustering algorithm is unstructured.

    compute_full_tree : 'auto' or bool, default='auto'
        Stop early the construction of the tree at `n_clusters`. This is useful
        to decrease computation time if the number of clusters is not small
        compared to the number of features. This option is useful only when
        specifying a connectivity matrix. Note also that when varying the
        number of clusters and using caching, it may be advantageous to compute
        the full tree. It must be ``True`` if ``distance_threshold`` is not
        ``None``. By default `compute_full_tree` is "auto", which is equivalent
        to `True` when `distance_threshold` is not `None` or that `n_clusters`
        is inferior to the maximum between 100 or `0.02 * n_samples`.
        Otherwise, "auto" is equivalent to `False`.

    linkage : {"ward", "complete", "average", "single"}, default="ward"
        Which linkage criterion to use. The linkage criterion determines which
        distance to use between sets of features. The algorithm will merge
        the pairs of cluster that minimize this criterion.

        - "ward" minimizes the variance of the clusters being merged.
        - "complete" or maximum linkage uses the maximum distances between
          all features of the two sets.
        - "average" uses the average of the distances of each feature of
          the two sets.
        - "single" uses the minimum of the distances between all features
          of the two sets.

    pooling_func : callable, default=np.mean
        This combines the values of agglomerated features into a single
        value, and should accept an array of shape [M, N] and the keyword
        argument `axis=1`, and reduce it to an array of size [M].

    distance_threshold : float, default=None
        The linkage distance threshold at or above which clusters will not be
        merged. If not ``None``, ``n_clusters`` must be ``None`` and
        ``compute_full_tree`` must be ``True``.

        .. versionadded:: 0.21

    compute_distances : bool, default=False
        Computes distances between clusters even if `distance_threshold` is not
        used. This can be used to make dendrogram visualization, but introduces
        a computational and memory overhead.

        .. versionadded:: 0.24

    Attributes
    ----------
    n_clusters_ : int
        The number of clusters found by the algorithm. If
        ``distance_threshold=None``, it will be equal to the given
        ``n_clusters``.

    labels_ : array-like of (n_features,)
        Cluster labels for each feature.

    n_leaves_ : int
        Number of leaves in the hierarchical tree.

    n_connected_components_ : int
        The estimated number of connected components in the graph.

        .. versionadded:: 0.21
            ``n_connected_components_`` was added to replace ``n_components_``.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    children_ : array-like of shape (n_nodes-1, 2)
        The children of each non-leaf node. Values less than `n_features`
        correspond to leaves of the tree which are the original samples.
        A node `i` greater than or equal to `n_features` is a non-leaf
        node and has children `children_[i - n_features]`. Alternatively
        at the i-th iteration, children[i][0] and children[i][1]
        are merged to form node `n_features + i`.

    distances_ : array-like of shape (n_nodes-1,)
        Distances between nodes in the corresponding place in `children_`.
        Only computed if `distance_threshold` is used or `compute_distances`
        is set to `True`.

    See Also
    --------
    AgglomerativeClustering : Agglomerative clustering samples instead of
        features.
    ward_tree : Hierarchical clustering with ward linkage.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn import datasets, cluster
    >>> digits = datasets.load_digits()
    >>> images = digits.images
    >>> X = np.reshape(images, (len(images), -1))
    >>> agglo = cluster.FeatureAgglomeration(n_clusters=32)
    >>> agglo.fit(X)
    FeatureAgglomeration(n_clusters=32)
    >>> X_reduced = agglo.transform(X)
    >>> X_reduced.shape
    (1797, 32)
    r   Nr`   rf   r   r   rd   re   r   rh   r   )	rW   r)   r   r$   r   r   pooling_funcr   r   r   r   rt   r   F)r)   r   r$   r   r   r  r   r   c          
      h    t                                          ||||||||	           || _        d S )N)rW   r   r$   r   r   r)   r   r   )r  r   r  )r   rW   r)   r   r$   r   r   r  r   r   r  s             r7   r   zFeatureAgglomeration.__init__  sN     	!%/1/ 	 		
 		
 		
 )r9   Tri   c                     t          | |d          }t                                          |j                   | j        | _        | S )aa  Fit the hierarchical clustering on the data.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            The data.

        y : Ignored
            Not used, present here for API consistency by convention.

        Returns
        -------
        self : object
            Returns the transformer.
        r   )ensure_min_features)r   r  r   r-   r  _n_features_outr  s      r7   r   zFeatureAgglomeration.fit  sA    " $q999QS#/r9   c                     t           )zAFit and return the result of each sample's clustering assignment.)AttributeError)r   s    r7   r  z FeatureAgglomeration.fit_predict0  s
     r9   r  r   )r  r  r  r  r   r   r   r  r   r   r  r   r   r   r   r   r  r  rE   meanr   r   r   propertyr  r  r  s   @r7   r  r  ]  s        P Pf  x!T&AAA4HJss>**m_<==
 

7++T2%$G(j&22I>Jss>#6#6#8#899::;!
'xafEEEtL'[$ $D   " )  W) ) ) ) ) ) )2 \555     65*   X    r9   r  )NNr   rt   F);r  r2   heapqr   r   r   r   numbersr   r   numpyrE   scipyr	   rC   r
   baser   r   r   r   metricsr   metrics._dist_metricsr   metrics.pairwiser   r   utilsr   utils._fast_dictr   utils._param_validationr   r   r   r   utils.graphr   utils.validationr   r    r   rP   _feature_agglomerationr   r8   rc   r   r   r   r   r   r  r   r   r   r  rw   r9   r7   <module>r6     sg     9 9 9 9 9 9 9 9 9 9 9 9 " " " " " " " "           5 5 5 5 5 5            % $ $ $ $ $ 4 4 4 4 4 4 ? ? ? ? ? ? ? ?       + + + + + +            4 3 3 3 3 3 : : : : : : : : 2 1 1 1 1 1 : : : : : :I0 I0 I0X5@ 5@ 5@x ^===x!T&AAA4H%;	  #'   "&$ gB gB gB gB gBZ Q> Q> Q> Q>j) ) )
) ) )
) ) )
 		  , , ,dK) K) K) K) K)lM K) K) K)\
V V V V V#%;=TV V V V Vr9   