code: # -*- coding: utf-8 -*-
  """
  Created on Sun Dec 15 10:58:44 2019
  
  @author: DELL
  """
  
  from __future__ import print_function, division
  
  import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  
  
  
  def d(u, v):
      diff = u - v
      return diff.dot(diff)
  
  
  def get_data(limit=None):
      print("Reading in and transforming data...")
      df = pd.read_csv('train.csv')
      data = df.values
      np.random.shuffle(data)
      X = data[:, 1:] / 255.0 # data is from 0..255
      Y = data[:, 0]
      if limit is not None:
          X, Y = X[:limit], Y[:limit]
      return X, Y
  
  def plot_k_means(X, K, max_iter=5, beta=3.0, show_plots=False):
      N, D = X.shape
      # R = np.zeros((N, K))
      exponents = np.empty((N, K))
  
      # initialize M to random
      initial_centers = np.random.choice(N, K, replace=False)
      M = X[initial_centers]
  
  
      k = 0
      for i in range(max_iter):
          k += 1
          # step 1: determine assignments / resposibilities
          # is this inefficient?
          for k in range(K):
              for n in range(N):
                  exponents[n,k] = np.exp(-beta*d(M[k], X[n]))
          R = exponents / exponents.sum(axis=1, keepdims=True)
  
  
          # step 2: recalculate means
  
          for k in range(K):
               M[k] = R[:,k].dot(X) / R[:,k].sum()
      return M, R
  
  
  
  
  
  
  
  
  def main():
      # mnist data
      X, Y = get_data(1000)
  
      # simple data
      # X = get_simple_data()
      # Y = np.array([0]*300 + [1]*300 + [2]*300)
  
      print("Number of data points:", len(Y))
      M, R = plot_k_means(X, len(set(Y)))
      # Exercise: Try different values of K and compare the evaluation metrics
      
      # they should look like digits
      for k in range(len(M)):
          im = M[k].reshape(28, 28)
          plt.imshow(im, cmap='Blues')
          plt.show()
  
  
  if __name__ == "__main__":
      main()
apis: numpy.random.shuffle numpy.empty pandas.read_csv numpy.random.choice matplotlib.pyplot.imshow matplotlib.pyplot.show