code: def test_weighted(self):
      d = 32
      sigma = 0.1
  
      # Data is naturally clustered in 10 clusters.
      # 5 clusters have 100 points
      # 5 clusters have 10 points
      # run k-means with 5 clusters
  
      ccent = faiss.randn((10, d), 123)
      faiss.normalize_L2(ccent)
      x = [
          ccent[i] + sigma * faiss.randn((100, d), 1234 + i)
          for i in range(5)
      ]
      x += [
          ccent[i] + sigma * faiss.randn((10, d), 1234 + i)
          for i in range(5, 10)
      ]
      x = np.vstack(x)
  
      clus = faiss.Clustering(d, 5)
      index = faiss.IndexFlatL2(d)
      clus.train(x, index)
      cdis1, perm1 = index.search(ccent, 1)
  
      # distance^2 of ground-truth centroids to clusters
      cdis1_first = cdis1[:5].sum()
      cdis1_last = cdis1[5:].sum()
  
      # now assign weight 0.1 to the 5 first clusters and weight 10
      # to the 5 last ones and re-run k-means
      weights = np.ones(100 * 5 + 10 * 5, dtype="float32")
      weights[: 100 * 5] = 0.1
      weights[100 * 5 :] = 10
  
      clus = faiss.Clustering(d, 5)
      index = faiss.IndexFlatL2(d)
      clus.train(x, index, weights=weights)
      cdis2, perm2 = index.search(ccent, 1)
  
      # distance^2 of ground-truth centroids to clusters
      cdis2_first = cdis2[:5].sum()
      cdis2_last = cdis2[5:].sum()
  
      print(cdis1_first, cdis1_last)
      print(cdis2_first, cdis2_last)
  
      # with the new clustering, the last should be much (*2) closer
      # to their centroids
      self.assertGreater(cdis1_last, cdis1_first * 2)
      self.assertGreater(cdis2_first, cdis2_last * 2)
  
imports: from __future__ import absolute_import, division, print_function
  import numpy
  import faiss
  import unittest
  import array
  from common_faiss_tests import get_dataset_2
label_desc: Use image node_id 4 for calling the TestClustering obj's underlying member method code with example usage: obj.test_weighted() without return types