title: How to apply Boolean Matrix Factorization to clustering problems?
body: <p><strong>Problem Statement and Implementation:</strong>
  I have a boolean matrix that has the data of users and items. If a user has bought the item then the value is 1, if not it is 0. I have about 10,000 users(rows) and 20,000 items(columns). I need to group similar users who have bought similar items. I have implemented this in K-modes. But, the silhouette score is less. That is, the users are not grouped well enough.  </p>
  
  <p><strong>Recommended solution:</strong>
  So, I was recommended to use <em>Boolean Matrix factorization(BMF) because k-means/modes is only usable if clusters have convex shapes and every point belongs to exactly one cluster. BMF can compute clusters with overlap (might not be necessarily important for your application) but it also identifies the features which are important for creating one cluster together with the data points belonging to one cluster. Especially if you have a high-dimensional feature space, it is unlikely that the similarity between points is expressed over the whole feature space. BMF identifies the feature space in which the points form a cluster.</em></p>
  
  <p><strong>Question:</strong>
  I have used the BMF packages from python. But I am not sure how to proceed with the results of the algorithm. Could somebody please suggest how I can proceed to forming clusters, name the clusters, and check which users are in which cluster using the Boolean Matrix Factorization? </p>
  
  <p><strong>K-modes implementation</strong></p>
  
  <pre><code>#method 1: CAO initialisation, with clusters 2
  km_cao = KModes(n_clusters=2, init = "Cao", n_init = 1, verbose=1)
  fitClusters_cao = km_cao.fit_predict(pivot_table)
  fitClusters_cao
  
  clusterCentroidsDf = pd.DataFrame(km_cao.cluster_centroids_)
  clusterCentroidsDf.columns = pivot_table.columns
  clusterCentroidsDf
  
  #method 2: Huang initialization, with clusters 2
  km_huang = KModes(n_clusters=2, init = "Huang", n_init = 1, verbose=1)
  fitClusters_huang = km_huang.fit_predict(pivot_table)
  fitClusters_huang
  
  </code></pre>
  
  <p><strong>Boolean Matrix Factorization tried so far</strong>
  Pivot_table has the boolean matrix. </p>
  
  <pre><code>def __fact_factor(X):
      """
      Return dense factorization factor, so that output is printed nice if factor is sparse.
  
      :param X: Factorization factor.
      :type X: :class:`scipy.sparse` of format csr, csc, coo, bsr, dok, lil, dia or :class:`numpy.matrix`
      """
      return X.todense() if sp.isspmatrix(X) else X
  
  
  bmf = nimfa.Bmf(pivot_table.values, seed="nndsvd", rank=10, max_iter=12, initialize_only=True, lambda_w=1.1, lambda_h=1.1)
  bmf_fit = bmf()
  
  print("=================================================================================================")
  print("Factorization method:", bmf_fit.fit)
  print("Initialization method:", bmf_fit.fit.seed)
  print("Basis matrix W: ")
  print(__fact_factor(bmf_fit.basis()))
  print("Actual number of iterations: ", bmf_fit.summary('none')['n_iter'])
  # We can access sparseness measure directly through fitted model.
  # fit.fit.sparseness()
  print("Sparseness basis: %7.4f, Sparseness mixture: %7.4f" % (bmf_fit.summary('none')['sparseness'][0], bmf_fit.summary('none')['sparseness'][1]))
  # We can access explained variance directly through fitted model.
  # fit.fit.evar()
  print("Explained variance: ", bmf_fit.summary('none')['evar'])
  # We can access residual sum of squares directly through fitted model.
  # fit.fit.rss()
  print("Residual sum of squares: ", bmf_fit.summary('none')['rss'])
  # There are many more ... but just cannot print out everything =] and some measures need additional data or more runs
  # e.g. entropy, predict, purity, coph_cor, consensus, select_features, score_features, connectivity
  print("=================================================================================================")
  
  </code></pre>
  
  <p>The output for the above code is : </p>
  
  <pre><code>=================================================================================================
  Factorization method: bmf
  Initialization method: nndsvd
  Basis matrix W: 
  [[1.66625229e+00 1.39154537e+00 1.49951683e+00 1.28801329e+00
    1.30824274e+00 1.49929241e+00 1.49805989e+00 1.45501586e+00
    1.46423781e+00 1.46044597e+00]
   [1.39418572e-03 1.56074288e+00 1.63243924e+00 1.32752612e+00
    1.33121227e+00 1.36615553e+00 1.40162330e+00 1.33175641e+00
    1.35845014e+00 1.37581247e+00].......
  .....
  Actual number of iterations:  12
  Sparseness basis:  0.1438, Sparseness mixture:  0.2611
  Explained variance:  0.7680056334943757
  Residual sum of squares:  1426.7653540095896
  
  
  </code></pre>