Github user skonto commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3192#discussion_r98456054
  
    --- Diff: 
flink-libraries/flink-ml/src/main/scala/org/apache/flink/ml/clustering/KMeans.scala
 ---
    @@ -0,0 +1,263 @@
    +/*
    + * 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
    +
    +import 
org.apache.flink.api.java.functions.FunctionAnnotation.ForwardedFields
    +import org.apache.flink.api.scala.{DataSet, _}
    +import org.apache.flink.ml._
    +import org.apache.flink.ml.common.{LabeledVector, _}
    +import org.apache.flink.ml.math.Breeze._
    +import org.apache.flink.ml.math.{BLAS, Vector}
    +import org.apache.flink.ml.metrics.distances.EuclideanDistanceMetric
    +import org.apache.flink.ml.pipeline._
    +
    +
    +/**
    +  * Implements the KMeans algorithm which calculates cluster centroids 
based on set of training data
    +  * points and a set of k initial centroids.
    +  *
    +  * [[KMeans]] is a [[Predictor]] which needs to be trained on a set of 
data points and can then be
    +  * used to assign new points to the learned cluster centroids.
    +  *
    +  * The KMeans algorithm works as described on Wikipedia
    +  * (http://en.wikipedia.org/wiki/K-means_clustering):
    +  *
    +  * Given an initial set of k means m1(1),…,mk(1) (see below), the 
algorithm proceeds by alternating
    +  * between two steps:
    +  *
    +  * ===Assignment step:===
    +  *
    +  * Assign each observation to the cluster whose mean yields the least 
within-cluster sum  of
    +  * squares (WCSS). Since the sum of squares is the squared Euclidean 
distance, this is intuitively
    +  * the "nearest" mean. (Mathematically, this means partitioning the 
observations according to the
    +  * Voronoi diagram generated by the means).
    +  *
    +  * `S_i^(t) = { x_p : || x_p - m_i^(t) ||^2 ≤ || x_p - m_j^(t) ||^2 
\forall j, 1 ≤ j ≤ k}`,
    +  * where each `x_p`  is assigned to exactly one `S^{(t)}`, even if it 
could be assigned to two or
    +  * more of them.
    +  *
    +  * ===Update step:===
    +  *
    +  * Calculate the new means to be the centroids of the observations in the 
new clusters.
    +  *
    +  * `m^{(t+1)}_i = ( 1 / |S^{(t)}_i| ) \sum_{x_j \in S^{(t)}_i} x_j`
    +  *
    +  * Since the arithmetic mean is a least-squares estimator, this also 
minimizes the within-cluster
    +  * sum of squares (WCSS) objective.
    +  *
    +  * @example
    +  * {{{
    +  *       val trainingDS: DataSet[Vector] = 
env.fromCollection(Clustering.trainingData)
    +  *       val initialCentroids: DataSet[LabledVector] = 
env.fromCollection(Clustering.initCentroids)
    +  *
    +  *       val kmeans = KMeans()
    +  *         .setInitialCentroids(initialCentroids)
    +  *         .setNumIterations(10)
    +  *
    +  *       kmeans.fit(trainingDS)
    --- End diff --
    
    A sidenote. In general I am fine with pipelines API but in this case for 
example `train` would be more appropriate as the method instead of fit. 


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to