Github user rnowling commented on a diff in the pull request: https://github.com/apache/spark/pull/2906#discussion_r19267797 --- Diff: mllib/src/main/scala/org/apache/spark/mllib/clustering/HierarchicalClusteringModel.scala --- @@ -0,0 +1,79 @@ +/* + * 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.spark.mllib.clustering + +import breeze.linalg.{DenseVector => BDV, Vector => BV, norm => breezeNorm} +import org.apache.spark.api.java.JavaRDD +import org.apache.spark.mllib.linalg.Vector +import org.apache.spark.rdd.RDD + +/** + * this class is used for the model of the hierarchical clustering + * + * @param clusterTree a cluster as a tree node + * @param trainTime the milliseconds for executing a training + * @param predictTime the milliseconds for executing a prediction + * @param isTrained if the model has been trained, the flag is true + */ +class HierarchicalClusteringModel private ( + val clusterTree: ClusterTree, + var trainTime: Int, + var predictTime: Int, + var isTrained: Boolean) extends Serializable { + + def this(clusterTree: ClusterTree) = this(clusterTree, 0, 0, false) + + def getClusters(): Array[ClusterTree] = clusterTree.getClusters().toArray + + def getCenters(): Array[Vector] = getClusters().map(_.center) + + /** + * Predicts the closest cluster of each point + */ + def predict(vector: Vector): Int = { + // TODO Supports distance metrics other Euclidean distance metric + val metric = (bv1: BV[Double], bv2: BV[Double]) => breezeNorm(bv1 - bv2, 2.0) + this.clusterTree.assignClusterIndex(metric)(vector) + } + + /** + * Predicts the closest cluster of each point + */ + def predict(data: RDD[Vector]): RDD[(Int, Vector)] = { + val startTime = System.currentTimeMillis() // to measure the execution time + + // TODO Supports distance metrics other Euclidean distance metric + val metric = (bv1: BV[Double], bv2: BV[Double]) => breezeNorm(bv1 - bv2, 2.0) + val centers = getClusters().map(_.center.toBreeze) + val treeRoot = this.clusterTree + val closestClusterIndexFinder = treeRoot.assignClusterIndex(metric) _ + data.sparkContext.broadcast(closestClusterIndexFinder) + val predicted = data.map(point => (closestClusterIndexFinder(point), point)) --- End diff -- I don't think you're using the broadcast variable correctly: http://spark.apache.org/docs/latest/programming-guide.html#broadcast-variables
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org