fork download
  1. from sklearn.cluster import Birch
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. np.random.seed(12)
  6. p1 = np.random.randint(5,21,110)
  7. p2 = np.random.randint(20,30,120)
  8. p3 = np.random.randint(8,21,90)
  9.  
  10. data = np.array(np.concatenate([p1, p2, p3]))
  11. x_range = range(len(data))
  12. x = np.array(list(zip(x_range, data))).reshape(len(x_range), 2)
  13.  
  14. plt.scatter(x[:,0], x[:,1])
  15. plt.show()
  16.  
  17. bclust=Birch(branching_factor=100, threshold=.5).fit(x)
  18. print(bclust)
  19.  
  20. labels = bclust.predict(x)
  21.  
  22. plt.scatter(x[:,0], x[:,1], c=labels)
  23. plt.show()
Success #stdin #stdout 0.88s 94716KB
stdin
Standard input is empty
stdout
Birch(branching_factor=100, compute_labels=True, copy=True, n_clusters=3,
   threshold=0.5)