lindong28 commented on code in PR #148: URL: https://github.com/apache/flink-ml/pull/148#discussion_r961348986
########## flink-ml-lib/src/main/java/org/apache/flink/ml/clustering/agglomerativeclustering/AgglomerativeClusteringParams.java: ########## @@ -0,0 +1,108 @@ +/* + * 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. + */ + +package org.apache.flink.ml.clustering.agglomerativeclustering; + +import org.apache.flink.ml.common.param.HasDistanceMeasure; +import org.apache.flink.ml.common.param.HasFeaturesCol; +import org.apache.flink.ml.common.param.HasPredictionCol; +import org.apache.flink.ml.param.BooleanParam; +import org.apache.flink.ml.param.DoubleParam; +import org.apache.flink.ml.param.IntParam; +import org.apache.flink.ml.param.Param; +import org.apache.flink.ml.param.ParamValidators; +import org.apache.flink.ml.param.StringParam; + +/** + * Params of {@link AgglomerativeClustering}. + * + * @param <T> The class type of this instance. + */ +public interface AgglomerativeClusteringParams<T> + extends HasDistanceMeasure<T>, HasFeaturesCol<T>, HasPredictionCol<T> { + Param<Integer> NUM_CLUSTERS = + new IntParam("numClusters", "The max number of clusters to create.", 2); + + Param<Double> DISTANCE_THRESHOLD = + new DoubleParam( + "distanceThreshold", + "Threshold to decide whether two clusters should be merged.", + null); + + String LINKAGE_WARD = "ward"; + String LINKAGE_COMPLETE = "complete"; + String LINKAGE_SINGLE = "single"; + String LINKAGE_AVERAGE = "average"; + /** + * 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> + */ + Param<String> LINKAGE = + new StringParam( + "linkage", + "Criterion for computing distance between two clusters.", Review Comment: Would it be useful to mention `The algorithm will merge the pairs of cluster that minimize this criterion` similar to sklearn's doc? ########## flink-ml-lib/src/main/java/org/apache/flink/ml/clustering/agglomerativeclustering/AgglomerativeClusteringParams.java: ########## @@ -0,0 +1,108 @@ +/* + * 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. + */ + +package org.apache.flink.ml.clustering.agglomerativeclustering; + +import org.apache.flink.ml.common.param.HasDistanceMeasure; +import org.apache.flink.ml.common.param.HasFeaturesCol; +import org.apache.flink.ml.common.param.HasPredictionCol; +import org.apache.flink.ml.param.BooleanParam; +import org.apache.flink.ml.param.DoubleParam; +import org.apache.flink.ml.param.IntParam; +import org.apache.flink.ml.param.Param; +import org.apache.flink.ml.param.ParamValidators; +import org.apache.flink.ml.param.StringParam; + +/** + * Params of {@link AgglomerativeClustering}. + * + * @param <T> The class type of this instance. + */ +public interface AgglomerativeClusteringParams<T> + extends HasDistanceMeasure<T>, HasFeaturesCol<T>, HasPredictionCol<T> { + Param<Integer> NUM_CLUSTERS = + new IntParam("numClusters", "The max number of clusters to create.", 2); + + Param<Double> DISTANCE_THRESHOLD = + new DoubleParam( + "distanceThreshold", + "Threshold to decide whether two clusters should be merged.", + null); + + String LINKAGE_WARD = "ward"; + String LINKAGE_COMPLETE = "complete"; + String LINKAGE_SINGLE = "single"; + String LINKAGE_AVERAGE = "average"; + /** + * 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. Review Comment: Would the following be more consistent with the options described above: ``` the average distance between all observations of the two clusters. ``` ########## flink-ml-lib/src/main/java/org/apache/flink/ml/clustering/agglomerativeclustering/AgglomerativeClusteringParams.java: ########## @@ -0,0 +1,108 @@ +/* + * 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. + */ + +package org.apache.flink.ml.clustering.agglomerativeclustering; + +import org.apache.flink.ml.common.param.HasDistanceMeasure; +import org.apache.flink.ml.common.param.HasFeaturesCol; +import org.apache.flink.ml.common.param.HasPredictionCol; +import org.apache.flink.ml.param.BooleanParam; +import org.apache.flink.ml.param.DoubleParam; +import org.apache.flink.ml.param.IntParam; +import org.apache.flink.ml.param.Param; +import org.apache.flink.ml.param.ParamValidators; +import org.apache.flink.ml.param.StringParam; + +/** + * Params of {@link AgglomerativeClustering}. + * + * @param <T> The class type of this instance. + */ +public interface AgglomerativeClusteringParams<T> + extends HasDistanceMeasure<T>, HasFeaturesCol<T>, HasPredictionCol<T> { + Param<Integer> NUM_CLUSTERS = + new IntParam("numClusters", "The max number of clusters to create.", 2); + + Param<Double> DISTANCE_THRESHOLD = + new DoubleParam( + "distanceThreshold", + "Threshold to decide whether two clusters should be merged.", + null); + + String LINKAGE_WARD = "ward"; + String LINKAGE_COMPLETE = "complete"; + String LINKAGE_SINGLE = "single"; + String LINKAGE_AVERAGE = "average"; + /** + * 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. Review Comment: cluster -> clusters ########## 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 Review Comment: bottom up -> bottom-up ########## flink-ml-lib/src/main/java/org/apache/flink/ml/clustering/agglomerativeclustering/AgglomerativeClusteringParams.java: ########## @@ -0,0 +1,108 @@ +/* + * 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. + */ + +package org.apache.flink.ml.clustering.agglomerativeclustering; + +import org.apache.flink.ml.common.param.HasDistanceMeasure; +import org.apache.flink.ml.common.param.HasFeaturesCol; +import org.apache.flink.ml.common.param.HasPredictionCol; +import org.apache.flink.ml.param.BooleanParam; +import org.apache.flink.ml.param.DoubleParam; +import org.apache.flink.ml.param.IntParam; +import org.apache.flink.ml.param.Param; +import org.apache.flink.ml.param.ParamValidators; +import org.apache.flink.ml.param.StringParam; + +/** + * Params of {@link AgglomerativeClustering}. + * + * @param <T> The class type of this instance. + */ +public interface AgglomerativeClusteringParams<T> + extends HasDistanceMeasure<T>, HasFeaturesCol<T>, HasPredictionCol<T> { + Param<Integer> NUM_CLUSTERS = + new IntParam("numClusters", "The max number of clusters to create.", 2); + + Param<Double> DISTANCE_THRESHOLD = + new DoubleParam( + "distanceThreshold", + "Threshold to decide whether two clusters should be merged.", + null); + + String LINKAGE_WARD = "ward"; + String LINKAGE_COMPLETE = "complete"; + String LINKAGE_SINGLE = "single"; + String LINKAGE_AVERAGE = "average"; + /** + * 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 Review Comment: It seems inconsistent to have `merged one` for this option but not the options described below. How about this: `ward: the sum of variance of the two clusters` ########## 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: Maybe provide path to the scripts that visualize the 2nd output. ########## 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 Review Comment: distance measure -> distance measures -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org