@@ -0,0 +1,3145 @@
+import numpy as np%0Aimport typing as tg%0A%0Aimport sklearn.cluster as sklc%0Afrom sklearn.cluster import _hierarchical%0Afrom sklearn.cluster.hierarchical import _TREE_BUILDERS, _hc_cut%0Afrom sklearn.externals import six%0Afrom sklearn.externals.joblib import Memory%0Afrom sklearn.utils import check_array%0A%0A%0Aclass AgglomerativeClustering(sklc.AgglomerativeClustering):%0A    def __init__(self, *args, **kwargs):%0A        super().__init__(*args, **kwargs)%0A%0A    def fit(self, X: np.ndarray, y: np.ndarray=None, *args, **kwargs) -%3E tg.Optional:%0A        %22%22%22Fit the hierarchical clustering on the data Parameter%0A        ----------%0A        X : array-like, shape = %5Bn_samples, n_features%5D The samples a.k.a. observations.%0A%0A        Returns%0A        -------%0A        self%0A        %22%22%22%0A        X = check_array(X, ensure_min_samples=2, estimator=self)%0A        memory = self.memory%0A        if isinstance(memory, six.string_types):%0A            memory = Memory(cachedir=memory, verbose=0)%0A%0A        if self.n_clusters %3C= 0:%0A            raise ValueError(%22n_clusters should be an integer greater than 0.%22 %22 %25s was provided.%22 %25 str(self.n_clusters))%0A%0A        if self.linkage == %22ward%22 and self.affinity != %22euclidean%22:%0A            raise ValueError(%22%25s was provided as affinity. Ward can only %22 %22work with euclidean distances.%22 %25 (self.affinity, ))%0A%0A        if self.linkage not in _TREE_BUILDERS:%0A            raise ValueError(%22Unknown linkage type %25s.%22 %22Valid options are %25s%22 %25 (self.linkage, _TREE_BUILDERS.keys()))%0A        tree_builder = _TREE_BUILDERS%5Bself.linkage%5D%0A%0A        connectivity = self.connectivity%0A        if self.connectivity is not None:%0A            if callable(self.connectivity):%0A                connectivity = self.connectivity(X)%0A            connectivity = check_array(connectivity, accept_sparse=%5B'csr', 'coo', 'lil'%5D)%0A%0A        n_samples = len(X)%0A        compute_full_tree = self.compute_full_tree%0A        if self.connectivity is None:%0A            compute_full_tree = True%0A        if compute_full_tree == 'auto':%0A            # Early stopping is likely to give a speed up only for%0A            # a large number of clusters. The actual threshold%0A            # implemented here is heuristic%0A            compute_full_tree = self.n_clusters %3C max(100, .02 * n_samples)%0A        n_clusters = self.n_clusters%0A        if compute_full_tree:%0A            n_clusters = None%0A        # Construct the tree%0A        if self.linkage != 'ward':%0A            kwargs%5B'linkage'%5D = self.linkage%0A            kwargs%5B'affinity'%5D = self.affinity%0A        self.children_, self.n_components_, self.n_leaves_, parents = memory.cache(tree_builder)(X, connectivity, n_components=self.n_components, n_clusters=n_clusters, **kwargs)%0A        # Cut the tree%0A        if compute_full_tree:%0A            self.labels_ = _hc_cut(self.n_clusters, self.children_, self.n_leaves_)%0A        else:%0A            labels = _hierarchical.hc_get_heads(parents, copy=False)%0A            # copy to avoid holding a reference on the original array%0A            labels = np.copy(labels%5B:n_samples%5D)%0A            # Reasign cluster numbers%0A            self.labels_ = np.searchsorted(np.unique(labels), labels)%0A        return self%0A

rewrite fit function of sklearn.cluster.AgglomerativeClustering to pass parameters to tree_builder