Advertisement
11eimilia11

init stuff

Dec 6th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. import pandas as pd
  2. from Lista02 import FuncoesML as fun
  3. from sklearn.cluster import KMeans
  4. import numpy as np
  5. from random import randint
  6. from Lista03.pso import pso
  7. from sklearn.metrics import pairwise_distances
  8.  
  9. KCLUSTERS = 3
  10.  
  11. #FUNCTIONS ---------------------------------------------------------------------
  12.  
  13. def WCSS(distance):
  14.  
  15.     retorno = []
  16.     for x in distance:
  17.         soma = 0
  18.         for z in x:
  19.             soma += z**2
  20.         retorno.append(soma)
  21.  
  22.     return retorno
  23.  
  24. def break_vectors(vector):
  25.  
  26.     global KCLUSTERS
  27.     retorno =np.split(vector,KCLUSTERS)
  28.     return retorno
  29.  
  30. def wcssGenetic(centers):
  31.  
  32.     global KCLUSTERS
  33.     array = break_vectors(centers)
  34.  
  35.     kmeans = KMeans(KCLUSTERS,init=pd.DataFrame(array),max_iter=1,n_init=1)
  36.     kmeans.fit(pd.DataFrame(wine))
  37.  
  38.     return kmeans.inertia_
  39.  
  40.  
  41. def generatepopulation(X,numberpopu, K, rng):
  42.  
  43.     population = []
  44.  
  45.     for x in range(numberpopu):
  46.         first = rng.permutation(X.shape[0])[:K]
  47.         population.append(np.concatenate(X[first]))
  48.  
  49.     return population
  50.  
  51.  
  52. # def pso_algorithm(base, rng,K):
  53. #     population = generatepopulation(base, 20, K,rng)
  54. #
  55. #     p = pso(20, wcssGenetic, -1000, 1000, 36, 100, init= population)
  56. #     return p.get_Gbest()
  57.  
  58. #FUNCTIONS ---------------------------------------------------------------------
  59.  
  60. wine = pd.read_csv('C:/Users/Auricelia/Desktop/DataSetsML/glass.csv')
  61. wine2 = pd.read_csv('C:/Users/Auricelia/Desktop/DataSetsML/glass.csv')
  62.  
  63. del wine2['Class']
  64. del wine2['ID']
  65.  
  66. wine2 = np.array(wine2)
  67.  
  68. wine = fun.normalizar(wine)
  69.  
  70. del wine[0]
  71. del wine[10]
  72.  
  73. wine = np.array(wine)
  74.  
  75.  
  76. winedataframe = pd.DataFrame(wine)
  77.  
  78.  
  79. dictionary = {}
  80. dictionary2 = {}
  81.  
  82. for y2 in range(0,50):
  83.  
  84.     distance2 = []
  85.     h2 = randint(1,10000)
  86.     rng2 = np.random.RandomState(h2)
  87.     centers2, labels2, distance2 = fun.find_clusters(wine2, KCLUSTERS, rng2,100)
  88.     retorno2 = WCSS(distance2)
  89.  
  90.     num2 = retorno2[len(retorno2) - 1]
  91.     dictionary2[h2] = num2
  92.  
  93.  
  94. resultsKmeans = []
  95. resultKmeansHybrid = []
  96. for y in range(10):
  97.  
  98.     resultparcial = []
  99.     distance = []
  100.     h = randint(1,10)
  101.     rng = np.random.RandomState(h)
  102.  
  103.     for algumacoisa in range(20):
  104.         centers, labels, distance = fun.find_clusters(wine, KCLUSTERS, rng,100)
  105.         retorno = WCSS(distance)
  106.         resultparcial.append(retorno[len(retorno) - 1])
  107.  
  108.     resultparcial = np.sort(resultparcial)
  109.  
  110.     resultsKmeans.append(resultparcial[0])
  111.  
  112.     population = generatepopulation(wine, 20, KCLUSTERS,rng)
  113.  
  114.  
  115.     p = pso(20, wcssGenetic, 0, 500, 27, 100, init= population)
  116.     array = np.array(p.get_Gbest())
  117.     array = np.split(array,3)
  118.  
  119.     cen, lbl, dis = fun.find_clustersGENETIC(wine, KCLUSTERS, 100, array)
  120.  
  121.     ret = WCSS(dis)
  122.     resultKmeansHybrid.append(ret[len(ret) - 1])
  123.  
  124.  
  125.  
  126.     # num = retorno[len(retorno) - 1]
  127.     # dictionary[h] = num
  128.  
  129. #
  130. # print("Distancias" ,distance)
  131.  
  132. resultKmeansHybrid = np.sort(resultKmeansHybrid)
  133. resultsKmeans = np.sort(resultsKmeans)
  134. print("Media Hibrido" , np.mean(resultKmeansHybrid))
  135. print("Media sem hibrido" , np.mean(resultsKmeans))
  136. print(resultKmeansHybrid)
  137. print("Nao hibrido", resultsKmeans)
  138. # print("centers", centers)
  139. # print("Quantas vezes rodou", len(distance))
  140.  
  141. wine = pd.DataFrame(wine)
  142. #
  143. wine['Class'] = labels
  144. #
  145. print(wine)
  146.  
  147.  
  148. # dictionary = sorted(dictionary.items(), key=lambda x: x[1])
  149. # dictionary2 = sorted(dictionary2.items(), key=lambda x: x[1])
  150.  
  151. # print("\n\n\n\n",dictionary)
  152. # print("\n\n\n\n",dictionary2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement