zarray.append(p.current_position[2]) import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xarray, yarray, zarray, zdir=u'z', s=20, c='b', marker='o') plt.show() # printParticles() while i < max_iterations: for j, p in enumerate(particles): fitness = k_means.execute(p.current_position.tolist()) error = 1-fitness if fitness > p.fitness: p.fitness = fitness p.best_position = p.current_position if fitness > global_best.fitness: global_best = p v = p.velocity + velocity_parameter_1 * random.uniform(1, 10) * (p.best_position - p.current_position) \ + velocity_parameter_2 * random.uniform(1, 10) * (global_best.current_position - p.current_position) p.current_position = p.current_position + v i += 1
__author__ = 'felipe' from kmeans import KMeans from random import uniform km = KMeans() for num_clusters in range(2, 31): average_precision = 0 best_precision = 0 worst_precision = 9999999999999 for execs in range(0, 3532): params = [num_clusters, ] for j in range(0, num_clusters): for i in range(0, km.dimens): params.extend([uniform(km.mini[i], km.maxi[i])]) precision = km.execute(params) average_precision += precision best_precision = max(precision, best_precision) worst_precision = min(precision, worst_precision) # print(execs) average_precision /= 3532 print(str(num_clusters) + "," + str(average_precision) + "," + str(best_precision) + "," + str(worst_precision))