PlotFunctions.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Jun 5 00:24:23 2021
  4. @author: 34123
  5. """
  6. import matplotlib.pyplot as plt
  7. import numpy as np
  8. import random
  9. from scipy.stats import multivariate_normal
  10. def plot_random_init_iris_sepal(df_full):
  11. sepal_df = df_full.iloc[:,0:2]
  12. sepal_df = np.array(sepal_df)
  13. m1 = random.choice(sepal_df)
  14. m2 = random.choice(sepal_df)
  15. m3 = random.choice(sepal_df)
  16. cov1 = np.cov(np.transpose(sepal_df))
  17. cov2 = np.cov(np.transpose(sepal_df))
  18. cov3 = np.cov(np.transpose(sepal_df))
  19. x1 = np.linspace(4,8,150)
  20. x2 = np.linspace(1.5,4.5,150)
  21. X, Y = np.meshgrid(x1,x2)
  22. Z1 = multivariate_normal(m1, cov1)
  23. Z2 = multivariate_normal(m2, cov2)
  24. Z3 = multivariate_normal(m3, cov3)
  25. # a new array of given shape and type, without initializing entries
  26. pos = np.empty(X.shape + (2,))
  27. pos[:, :, 0] = X; pos[:, :, 1] = Y
  28. plt.figure(figsize=(10,10))
  29. plt.scatter(sepal_df[:,0], sepal_df[:,1], marker='o')
  30. plt.contour(X, Y, Z1.pdf(pos), colors="r" ,alpha = 0.5)
  31. plt.contour(X, Y, Z2.pdf(pos), colors="b" ,alpha = 0.5)
  32. plt.contour(X, Y, Z3.pdf(pos), colors="g" ,alpha = 0.5)
  33. # making both the axis equal
  34. plt.axis('equal')
  35. plt.xlabel('Sepal Length', fontsize=16)
  36. plt.ylabel('Sepal Width', fontsize=16)
  37. plt.title('Initial Random Clusters(Sepal)', fontsize=22)
  38. plt.grid()
  39. plt.show()
  40. def plot_random_init_iris_petal(df_full):
  41. petal_df = df_full.iloc[:,2:4]
  42. petal_df = np.array(petal_df)
  43. m1 = random.choice(petal_df)
  44. m2 = random.choice(petal_df)
  45. m3 = random.choice(petal_df)
  46. cov1 = np.cov(np.transpose(petal_df))
  47. cov2 = np.cov(np.transpose(petal_df))
  48. cov3 = np.cov(np.transpose(petal_df))
  49. x1 = np.linspace(-1,7,150)
  50. x2 = np.linspace(-1,4,150)
  51. X, Y = np.meshgrid(x1,x2)
  52. Z1 = multivariate_normal(m1, cov1)
  53. Z2 = multivariate_normal(m2, cov2)
  54. Z3 = multivariate_normal(m3, cov3)
  55. pos = np.empty(X.shape + (2,))
  56. pos[:, :, 0] = X; pos[:, :, 1] = Y
  57. plt.figure(figsize=(10,10))
  58. plt.scatter(petal_df[:,0], petal_df[:,1], marker='o')
  59. plt.contour(X, Y, Z1.pdf(pos), colors="r" ,alpha = 0.5)
  60. plt.contour(X, Y, Z2.pdf(pos), colors="b" ,alpha = 0.5)
  61. plt.contour(X, Y, Z3.pdf(pos), colors="g" ,alpha = 0.5)
  62. plt.axis('equal')
  63. plt.xlabel('Petal Length', fontsize=16)
  64. plt.ylabel('Petal Width', fontsize=16)
  65. plt.title('Initial Random Clusters(Petal)', fontsize=22)
  66. plt.grid()
  67. plt.show()
  68. def plot_cluster_iris_sepal(df_full, labels, centers):
  69. # finding mode
  70. seto = max(set(labels[0:50]), key=labels[0:50].count) # 2
  71. vers = max(set(labels[50:100]), key=labels[50:100].count) # 1
  72. virg = max(set(labels[100:]), key=labels[100:].count) # 0
  73. # sepal
  74. s_mean_clus1 = np.array([centers[seto][0],centers[seto][1]])
  75. s_mean_clus2 = np.array([centers[vers][0],centers[vers][1]])
  76. s_mean_clus3 = np.array([centers[virg][0],centers[virg][1]])
  77. values = np.array(labels) #label
  78. # search all 3 species
  79. searchval_seto = seto
  80. searchval_vers = vers
  81. searchval_virg = virg
  82. # index of all 3 species
  83. ii_seto = np.where(values == searchval_seto)[0]
  84. ii_vers = np.where(values == searchval_vers)[0]
  85. ii_virg = np.where(values == searchval_virg)[0]
  86. ind_seto = list(ii_seto)
  87. ind_vers = list(ii_vers)
  88. ind_virg = list(ii_virg)
  89. sepal_df = df_full.iloc[:,0:2]
  90. seto_df = sepal_df[sepal_df.index.isin(ind_seto)]
  91. vers_df = sepal_df[sepal_df.index.isin(ind_vers)]
  92. virg_df = sepal_df[sepal_df.index.isin(ind_virg)]
  93. cov_seto = np.cov(np.transpose(np.array(seto_df)))
  94. cov_vers = np.cov(np.transpose(np.array(vers_df)))
  95. cov_virg = np.cov(np.transpose(np.array(virg_df)))
  96. sepal_df = np.array(sepal_df)
  97. x1 = np.linspace(4,8,150)
  98. x2 = np.linspace(1.5,4.5,150)
  99. X, Y = np.meshgrid(x1,x2)
  100. Z1 = multivariate_normal(s_mean_clus1, cov_seto)
  101. Z2 = multivariate_normal(s_mean_clus2, cov_vers)
  102. Z3 = multivariate_normal(s_mean_clus3, cov_virg)
  103. pos = np.empty(X.shape + (2,))
  104. pos[:, :, 0] = X; pos[:, :, 1] = Y
  105. plt.figure(figsize=(10,10))
  106. plt.scatter(sepal_df[:,0], sepal_df[:,1], marker='o')
  107. plt.contour(X, Y, Z1.pdf(pos), colors="r" ,alpha = 0.5)
  108. plt.contour(X, Y, Z2.pdf(pos), colors="b" ,alpha = 0.5)
  109. plt.contour(X, Y, Z3.pdf(pos), colors="g" ,alpha = 0.5)
  110. plt.axis('equal')
  111. plt.xlabel('Sepal Length', fontsize=16)
  112. plt.ylabel('Sepal Width', fontsize=16)
  113. plt.title('Final Clusters(Sepal)', fontsize=22)
  114. plt.grid()
  115. plt.show()
  116. def plot_cluster_iris_petal(df_full, labels, centers):
  117. # petal
  118. p_mean_clus1 = np.array([centers[seto][2],centers[seto][3]])
  119. p_mean_clus2 = np.array([centers[vers][2],centers[vers][3]])
  120. p_mean_clus3 = np.array([centers[virg][2],centers[virg][3]])
  121. petal_df = df_full.iloc[:,2:4]
  122. seto_df = petal_df[petal_df.index.isin(ind_seto)]
  123. vers_df = petal_df[petal_df.index.isin(ind_vers)]
  124. virg_df = petal_df[petal_df.index.isin(ind_virg)]
  125. cov_seto = np.cov(np.transpose(np.array(seto_df)))
  126. cov_vers = np.cov(np.transpose(np.array(vers_df)))
  127. cov_virg = np.cov(np.transpose(np.array(virg_df)))
  128. petal_df = np.array(petal_df)
  129. x1 = np.linspace(0.5,7,150)
  130. x2 = np.linspace(-1,4,150)
  131. X, Y = np.meshgrid(x1,x2)
  132. Z1 = multivariate_normal(p_mean_clus1, cov_seto)
  133. Z2 = multivariate_normal(p_mean_clus2, cov_vers)
  134. Z3 = multivariate_normal(p_mean_clus3, cov_virg)
  135. pos = np.empty(X.shape + (2,))
  136. pos[:, :, 0] = X; pos[:, :, 1] = Y
  137. plt.figure(figsize=(10,10))
  138. plt.scatter(petal_df[:,0], petal_df[:,1], marker='o')
  139. plt.contour(X, Y, Z1.pdf(pos), colors="r" ,alpha = 0.5)
  140. plt.contour(X, Y, Z2.pdf(pos), colors="b" ,alpha = 0.5)
  141. plt.contour(X, Y, Z3.pdf(pos), colors="g" ,alpha = 0.5)
  142. plt.axis('equal')
  143. plt.xlabel('Petal Length', fontsize=16)
  144. plt.ylabel('Petal Width', fontsize=16)
  145. plt.title('Final Clusters(Petal)', fontsize=22)
  146. plt.grid()
  147. plt.show()