zhipeng93 commented on code in PR #148: URL: https://github.com/apache/flink-ml/pull/148#discussion_r961545453
########## flink-ml-python/pyflink/ml/lib/clustering/agglomerativeclustering.py: ########## @@ -0,0 +1,135 @@ +################################################################################ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +################################################################################ +import typing + +from pyflink.ml.core.param import Param, StringParam, IntParam, FloatParam, \ + BooleanParam, ParamValidators +from pyflink.ml.core.wrapper import JavaWithParams +from pyflink.ml.lib.clustering.common import JavaClusteringAlgoOperator +from pyflink.ml.lib.param import HasDistanceMeasure, HasFeaturesCol, HasPredictionCol + + +class _AgglomerativeClusteringParams( + JavaWithParams, + HasDistanceMeasure, + HasFeaturesCol, + HasPredictionCol +): + """ + Params for :class:`AgglomerativeClustering`. + """ + NUM_CLUSTERS: Param[int] = IntParam("num_clusters", + "The max number of clusters to create.", + 2) + + DISTANCE_THRESHOLD: Param[float] = \ + FloatParam("distance_threshold", + "Threshold to decide whether two clusters should be merged.", + None) + + """ + Supported options to compute the distance between two clusters. + <ul> + <li>ward: difference between the sum of the variance of the two clusters and the merged one. + <li>complete: the maximum distance between all observations of the two clusters. + <li>single: the minimum distance between all observations of the two cluster. + <li>average: the average of the distance of all observations of the two cluster. + </ul> + """ + LINKAGE: Param[str] = StringParam( + "linkage", + "Criterion for computing distance between two clusters.", + "ward", + ParamValidators.in_array( + ["ward", "complete", "single", "average"])) + + COMPUTE_FULL_TREE: Param[bool] = BooleanParam( + "compute_full_tree", + "Whether computes the full tree after convergence.", + False, + ParamValidators.not_null()) + + def __init__(self, java_params): + super(_AgglomerativeClusteringParams, self).__init__(java_params) + + def set_num_clusters(self, value: int): + return typing.cast(_AgglomerativeClusteringParams, self.set(self.NUM_CLUSTERS, value)) + + def get_num_clusters(self) -> int: + return self.get(self.NUM_CLUSTERS) + + def set_distance_threshold(self, value: float): + return typing.cast(_AgglomerativeClusteringParams, self.set(self.DISTANCE_THRESHOLD, value)) + + def get_distance_threshold(self) -> float: + return self.get(self.DISTANCE_THRESHOLD) + + def set_linkage(self, value: str): + return typing.cast(_AgglomerativeClusteringParams, self.set(self.LINKAGE, value)) + + def get_linkage(self) -> str: + return self.get(self.LINKAGE) + + def set_compute_full_tree(self, value: bool): + return typing.cast(_AgglomerativeClusteringParams, self.set(self.COMPUTE_FULL_TREE, value)) + + def get_compute_full_tree(self) -> bool: + return self.get(self.COMPUTE_FULL_TREE) + + @property + def num_clusters(self): + return self.get_num_clusters() + + @property + def distance_threshold(self): + return self.get_distance_threshold() + + @property + def linkage(self): + return self.get_linkage() + + @property + def compute_full_tree(self): + return self.get_compute_full_tree() + + +class AgglomerativeClustering(JavaClusteringAlgoOperator, _AgglomerativeClusteringParams): + """ + An AlgoOperator that performs a hierarchical clustering using a bottom up approach. Each + observation starts in its own cluster and the clusters are merged together one by one. + Users can choose different strategies to merge two clusters by setting + {@link AgglomerativeClusteringParams#LINKAGE} and different distance measure by setting + {@link AgglomerativeClusteringParams#DISTANCE_MEASURE}. + + <p>The output contains two tables. The first one assigns one cluster Id for each data point. + The second one contains the information of merging two clusters at each step. The data format + of the merging information is (clusterId1, clusterId2, distance, sizeOfMergedCluster). Review Comment: Thanks for the comment. An example script to visualize the merge info is added [1]. It is also mentioned in the user doc [2]. [1] flink-ml-dist/src/main/flink-ml-bin/bin/agglomerativeclustering-visualize.py [2] docs/content/docs/operators/clustering/agglomerativeclustering.md -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
