Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- from Lista02 import FuncoesML as fun
- from sklearn.cluster import KMeans
- import numpy as np
- from random import randint
- from Lista03.pso import pso
- from sklearn.metrics import pairwise_distances
- KCLUSTERS = 3
- #FUNCTIONS ---------------------------------------------------------------------
- def WCSS(distance):
- retorno = []
- for x in distance:
- soma = 0
- for z in x:
- soma += z**2
- retorno.append(soma)
- return retorno
- def break_vectors(vector):
- global KCLUSTERS
- retorno =np.split(vector,KCLUSTERS)
- return retorno
- def wcssGenetic(centers):
- global KCLUSTERS
- array = break_vectors(centers)
- kmeans = KMeans(KCLUSTERS,init=pd.DataFrame(array),max_iter=1,n_init=1)
- kmeans.fit(pd.DataFrame(wine))
- return kmeans.inertia_
- def generatepopulation(X,numberpopu, K, rng):
- population = []
- for x in range(numberpopu):
- first = rng.permutation(X.shape[0])[:K]
- population.append(np.concatenate(X[first]))
- return population
- # def pso_algorithm(base, rng,K):
- # population = generatepopulation(base, 20, K,rng)
- #
- # p = pso(20, wcssGenetic, -1000, 1000, 36, 100, init= population)
- # return p.get_Gbest()
- #FUNCTIONS ---------------------------------------------------------------------
- wine = pd.read_csv('C:/Users/Auricelia/Desktop/DataSetsML/wine.csv')
- wine2 = pd.read_csv('C:/Users/Auricelia/Desktop/DataSetsML/wine.csv')
- del wine2['Class']
- del wine2['delete']
- wine2 = np.array(wine2)
- wine = fun.normalizar(wine)
- del wine[0]
- del wine[13]
- wine = np.array(wine)
- winedataframe = pd.DataFrame(wine)
- dictionary = {}
- dictionary2 = {}
- for y2 in range(0,50):
- distance2 = []
- h2 = randint(1,10000)
- rng2 = np.random.RandomState(h2)
- centers2, labels2, distance2 = fun.find_clusters(wine2, KCLUSTERS, rng2,100)
- retorno2 = WCSS(distance2)
- num2 = retorno2[len(retorno2) - 1]
- dictionary2[h2] = num2
- resultsKmeans = []
- resultKmeansHybrid = []
- for y in range(10):
- resultparcial = []
- distance = []
- h = randint(1,10)
- rng = np.random.RandomState(h)
- for algumacoisa in range(20):
- centers, labels, distance = fun.find_clusters(wine, KCLUSTERS, rng,100)
- retorno = WCSS(distance)
- resultparcial.append(retorno[len(retorno) - 1])
- resultparcial = np.sort(resultparcial)
- resultsKmeans.append(resultparcial[0])
- population = generatepopulation(wine, 20, KCLUSTERS,rng)
- p = pso(20, wcssGenetic, 0, 500, 36, 100, init= population)
- array = np.array(p.get_Gbest())
- array = np.split(array,3)
- cen, lbl, dis = fun.find_clustersGENETIC(wine, KCLUSTERS, 100, array)
- ret = WCSS(dis)
- resultKmeansHybrid.append(ret[len(ret) - 1])
- # num = retorno[len(retorno) - 1]
- # dictionary[h] = num
- #
- # print("Distancias" ,distance)
- resultKmeansHybrid = np.sort(resultKmeansHybrid)
- resultsKmeans = np.sort(resultsKmeans)
- print("Media Hibrido" , np.mean(resultKmeansHybrid))
- print("Media sem hibrido" , np.mean(resultsKmeans))
- print(resultKmeansHybrid)
- print("Nao hibrido", resultsKmeans)
- # print("centers", centers)
- # print("Quantas vezes rodou", len(distance))
- # wine = pd.DataFrame(wine)
- #
- # wine['Class'] = labels
- #
- # print(wine)
- # dictionary = sorted(dictionary.items(), key=lambda x: x[1])
- # dictionary2 = sorted(dictionary2.items(), key=lambda x: x[1])
- # print("\n\n\n\n",dictionary)
- # print("\n\n\n\n",dictionary2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement