tveasey commented on code in PR #13604:
URL: https://github.com/apache/lucene/pull/13604#discussion_r1698334973


##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/codecs/quantization/KMeans.java:
##########
@@ -0,0 +1,348 @@
+/*
+ * 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.lucene.sandbox.codecs.quantization;
+
+import static 
org.apache.lucene.sandbox.codecs.quantization.SampleReader.createSampleReader;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Random;
+import java.util.Set;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.VectorUtil;
+import org.apache.lucene.util.hnsw.RandomAccessVectorValues;
+
+/** KMeans clustering algorithm for vectors */
+public class KMeans {
+  public static final int MAX_NUM_CENTROIDS = Short.MAX_VALUE; // 32767
+  public static final int DEFAULT_RESTARTS = 5;
+  public static final int DEFAULT_ITRS = 10;
+  public static final int DEFAULT_SAMPLE_SIZE = 100_000;
+
+  private final RandomAccessVectorValues.Floats vectors;
+  private final int numVectors;
+  private final int numCentroids;
+  private final Random random;
+  private final KmeansInitializationMethod initializationMethod;
+  private final int restarts;
+  private final int iters;
+
+  /**
+   * Cluster vectors into a given number of clusters
+   *
+   * @param vectors float vectors
+   * @param similarityFunction vector similarity function. For COSINE 
similarity, vectors must be
+   *     normalized.
+   * @param numClusters number of cluster to cluster vector into
+   * @return results of clustering: produced centroids and for each vector its 
centroid
+   * @throws IOException when if there is an error accessing vectors
+   */
+  public static Results cluster(
+      RandomAccessVectorValues.Floats vectors,
+      VectorSimilarityFunction similarityFunction,
+      int numClusters)
+      throws IOException {
+    return cluster(
+        vectors,
+        numClusters,
+        true,
+        42L,
+        KmeansInitializationMethod.PLUS_PLUS,
+        similarityFunction == VectorSimilarityFunction.COSINE,
+        DEFAULT_RESTARTS,
+        DEFAULT_ITRS,
+        DEFAULT_SAMPLE_SIZE);

Review Comment:
   This should probably also account for `MAX_NUM_CENTROIDS`: 3 vectors per 
cluster is too low. I would do something like `max(DEFAULT_SAMPLE_SIZE, 100 * 
numClusters)`. This extra constraint could be applied inside `cluster` though.



-- 
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...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to