Advertisement
ZergRushA

rt-ai-8

Dec 16th, 2022
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. '''
  2. import numpy as np
  3. import seaborn as sns
  4. import matplotlib.pyplot as plt
  5. from sklearn.cluster import KMeans
  6. from sklearn.datasets import make_blobs
  7. sns.set()
  8.  
  9. 𝑋 = 𝑛𝑝.𝑎𝑟𝑟𝑎𝑦([[5,3],[10,15],[15,12],[24,10],[30,45],[85,70],[71,80],[60,78],[55,52],[80,91]])
  10. print(X.shape)
  11. kmeans = KMeans(n_clusters=6, random_state=4)
  12. kmeans.fit(X)
  13. y_kmeans = kmeans.predict(X)
  14. print(y_kmeans)
  15.  
  16. centers = kmeans.cluster_centers_
  17. print(len(centers))
  18.  
  19. X,y_true = make_blobs(n_samples=6, centers=6, cluster_std=0.4, random_state=4)
  20. plt.scatter(X[:,0], X[:, 1], c=np.unique(y_kmeans), s=50, cmap="summer")
  21. plt.scatter(centers[:,0], centers[:,1], c="blue", s=100, alpha=0.9)
  22. plt.show()
  23.  
  24.  
  25.  
  26.  
  27. import numpy as np
  28. import seaborn as sns
  29. import matplotlib.pyplot as plt
  30.  
  31. from sklearn.datasets import load_iris
  32. from sklearn.cluster import KMeans
  33. sns.set()
  34.  
  35. iris = load_iris()
  36. x = iris.data
  37. print(x.shape)
  38. kmeans = KMeans(n_clusters=10, random_state=4)
  39. y_kmeans = kmeans.fit_predict(x)
  40. print(kmeans.cluster_centers_.shape)
  41.  
  42. #Visualising the clusters
  43. plt.scatter(x[y_kmeans == 0, 0], x[y_kmeans == 0, 1], s = 100, c = 'purple', label = 'setosa')
  44. plt.scatter(x[y_kmeans == 1, 0], x[y_kmeans == 1, 1], s = 100, c = 'orange', label = 'versicolour')
  45. plt.scatter(x[y_kmeans == 2, 0], x[y_kmeans == 2, 1], s = 100, c = 'green', label = 'virginica')
  46.  
  47. #Plotting the centroids of the clusters
  48. plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:,1], s = 100, c = 'red', label = 'centroids')
  49.  
  50. plt.legend()
  51. plt.show()
  52.  
  53.  
  54.  
  55.  
  56. import numpy as np
  57. import seaborn as sns
  58. import matplotlib.pyplot as plt
  59. import scipy.cluster.hierarchy as shc
  60.  
  61. from sklearn.datasets import load_iris
  62. from sklearn.cluster import AgglomerativeClustering
  63. sns.set()
  64.  
  65. iris = load_iris()
  66. data = iris.data[:, 0:2]
  67. plt.figure(figsize=(10,7),dpi=180)
  68. plt.title("IRIS DENDROGRAM")
  69. dend = shc.dendrogram(shc.linkage(data,method='ward'))
  70.  
  71. cluster = AgglomerativeClustering(n_clusters=5, affinity='euclidean', linkage="ward")
  72. cluster.fit_predict(data)
  73. plt.figure(figsize=(10,7))
  74. plt.scatter(data[:,0],data[:,1],c=cluster.labels_,cmap='rainbow')
  75. plt.show()
  76.  
  77. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement