Advertisement
11eimilia11

LEOSTUF2

Dec 5th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 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/wine.csv')
  61. wine2 = pd.read_csv('C:/Users/Auricelia/Desktop/DataSetsML/wine.csv')
  62.  
  63. del wine2['Class']
  64. del wine2['delete']
  65.  
  66. wine2 = np.array(wine2)
  67.  
  68. wine = fun.normalizar(wine)
  69.  
  70. del wine[0]
  71. del wine[13]
  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. for y in range(50):
  94.     print(y)
  95.     distance = []
  96.     h = randint(1,10)
  97.     rng = np.random.RandomState(h)
  98.  
  99.     for algumacoisa in range(20):
  100.         centers, labels, distance = fun.find_clusters(wine, KCLUSTERS, rng,100)
  101.         retorno = WCSS(distance)
  102.  
  103.     population = generatepopulation(wine, 20, KCLUSTERS,rng)
  104.  
  105.     print(len(population))
  106.  
  107.     p = pso(20, wcssGenetic, -1000, 1000, 36, 100, init= population)
  108.     array = np.array(p.get_Gbest())
  109.     array = np.split(array,3)
  110.  
  111.     cen, lbl, dis = fun.find_clustersGENETIC(wine, KCLUSTERS, 100, array)
  112.  
  113.     ret = WCSS(dis)
  114.  
  115.     # num = retorno[len(retorno) - 1]
  116.     # dictionary[h] = num
  117.  
  118. #
  119. # print("Distancias" ,distance)
  120. print("Labels", labels)
  121. # print("centers", centers)
  122. # print("Quantas vezes rodou", len(distance))
  123.  
  124. wine = pd.DataFrame(wine)
  125.  
  126. wine['Class'] = labels
  127.  
  128. print(wine)
  129.  
  130.  
  131. dictionary = sorted(dictionary.items(), key=lambda x: x[1])
  132. dictionary2 = sorted(dictionary2.items(), key=lambda x: x[1])
  133.  
  134. # print("\n\n\n\n",dictionary)
  135. # print("\n\n\n\n",dictionary2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement