| @@ 86-106 (lines=21) @@ | ||
| 83 | ||
| 84 | """ |
|
| 85 | ||
| 86 | if (self.__ccore is True): |
|
| 87 | self.__clusters = wrapper.kmedians(self.__pointer_data, self.__medians, self.__tolerance); |
|
| 88 | self.__medians = self.__update_medians(); |
|
| 89 | ||
| 90 | else: |
|
| 91 | changes = float('inf');
|
|
| 92 | ||
| 93 | stop_condition = self.__tolerance * self.__tolerance; # Fast solution |
|
| 94 | #stop_condition = self.__tolerance; # Slow solution |
|
| 95 | ||
| 96 | # Check for dimension |
|
| 97 | if (len(self.__pointer_data[0]) != len(self.__medians[0])): |
|
| 98 | raise NameError('Dimension of the input data and dimension of the initial cluster medians must be equal.');
|
|
| 99 | ||
| 100 | while (changes > stop_condition): |
|
| 101 | self.__clusters = self.__update_clusters(); |
|
| 102 | updated_centers = self.__update_medians(); # changes should be calculated before asignment |
|
| 103 | ||
| 104 | changes = max([euclidean_distance_sqrt(self.__medians[index], updated_centers[index]) for index in range(len(updated_centers))]); # Fast solution |
|
| 105 | ||
| 106 | self.__medians = updated_centers; |
|
| 107 | ||
| 108 | ||
| 109 | def get_clusters(self): |
|
| @@ 83-103 (lines=21) @@ | ||
| 80 | ||
| 81 | """ |
|
| 82 | ||
| 83 | if (self.__ccore is True): |
|
| 84 | self.__clusters = wrapper.kmeans(self.__pointer_data, self.__centers, self.__tolerance); |
|
| 85 | self.__centers = self.__update_centers(); |
|
| 86 | else: |
|
| 87 | changes = float('inf');
|
|
| 88 | ||
| 89 | stop_condition = self.__tolerance * self.__tolerance; # Fast solution |
|
| 90 | #stop_condition = self.__tolerance; # Slow solution |
|
| 91 | ||
| 92 | # Check for dimension |
|
| 93 | if (len(self.__pointer_data[0]) != len(self.__centers[0])): |
|
| 94 | raise NameError('Dimension of the input data and dimension of the initial cluster centers must be equal.');
|
|
| 95 | ||
| 96 | while (changes > stop_condition): |
|
| 97 | self.__clusters = self.__update_clusters(); |
|
| 98 | updated_centers = self.__update_centers(); # changes should be calculated before asignment |
|
| 99 | ||
| 100 | #changes = max([euclidean_distance(self.__centers[index], updated_centers[index]) for index in range(len(self.__centers))]); # Slow solution |
|
| 101 | changes = max([euclidean_distance_sqrt(self.__centers[index], updated_centers[index]) for index in range(len(updated_centers))]); # Fast solution |
|
| 102 | ||
| 103 | self.__centers = updated_centers; |
|
| 104 | ||
| 105 | ||
| 106 | def get_clusters(self): |
|
| @@ 88-106 (lines=19) @@ | ||
| 85 | ||
| 86 | """ |
|
| 87 | ||
| 88 | if (self.__ccore is True): |
|
| 89 | self.__clusters = wrapper.kmedoids(self.__pointer_data, self.__medoids, self.__tolerance); |
|
| 90 | self.__medoids = self.__update_medoids(); |
|
| 91 | ||
| 92 | else: |
|
| 93 | self.__medoids = [ self.__pointer_data[medoid_index] for medoid_index in self.__medoids ]; |
|
| 94 | ||
| 95 | changes = float('inf');
|
|
| 96 | ||
| 97 | stop_condition = self.__tolerance * self.__tolerance; # Fast solution |
|
| 98 | #stop_condition = self.__tolerance; # Slow solution |
|
| 99 | ||
| 100 | while (changes > stop_condition): |
|
| 101 | self.__clusters = self.__update_clusters(); |
|
| 102 | updated_medoids = self.__update_medoids(); # changes should be calculated before asignment |
|
| 103 | ||
| 104 | changes = max([euclidean_distance_sqrt(self.__medoids[index], updated_medoids[index]) for index in range(len(updated_medoids))]); # Fast solution |
|
| 105 | ||
| 106 | self.__medoids = updated_medoids; |
|
| 107 | ||
| 108 | ||
| 109 | def get_clusters(self): |
|