Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. from pyclustering.cluster.clarans import clarans;
  2. from pyclustering.utils import timedcall;
  3. from sklearn import datasets
  4.  
  5. #import iris dataset from sklearn library
  6. iris = datasets.load_iris();
  7.  
  8. #get the iris data. It has 4 features, 3 classes and 150 data points.
  9. data = iris.data
  10.  
  11. """!
  12. The pyclustering library clarans implementation requires
  13. list of lists as its input dataset.
  14. Thus we convert the data from numpy array to list.
  15. """
  16. data = data.tolist()
  17.  
  18. #get a glimpse of dataset
  19. print("A peek into the dataset : ",data[:4])
  20.  
  21.  
  22. """!
  23. @brief Constructor of clustering algorithm CLARANS.
  24. @details The higher the value of maxneighbor, the closer is CLARANS to K-Medoids, and the longer is each search of a local minima.
  25.  
  26. @param[in] data: Input data that is presented as list of points (objects), each point should be represented by list or tuple.
  27. @param[in] number_clusters: amount of clusters that should be allocated.
  28. @param[in] numlocal: the number of local minima obtained (amount of iterations for solving the problem).
  29. @param[in] maxneighbor: the maximum number of neighbors examined.
  30. """
  31. clarans_instance = clarans(data, 3, 6, 4);
  32.  
  33. #calls the clarans method 'process' to implement the algortihm
  34. (ticks, result) = timedcall(clarans_instance.process);
  35. print("Execution time : ", ticks, "\n");
  36.  
  37. #returns the clusters
  38. clusters = clarans_instance.get_clusters();
  39.  
  40. #returns the mediods
  41. medoids = clarans_instance.get_medoids();
  42.  
  43.  
  44. print("Index of the points that are in a cluster : ",clusters)
  45. print("The target class of each datapoint : ",iris.target)
  46. print("The index of medoids that algorithm found to be best : "medoids)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement