| @@ 397-418 (lines=22) @@ | ||
| 394 | # dist = euclidean_distance(data[index_point], centers[index]); # Slow solution |
|
| 395 | dist = euclidean_distance_sqrt(self.__pointer_data[index_point], centers[index]); # Fast solution |
|
| 396 | ||
| 397 | if ( (dist < dist_optim) or (index is 0)): |
|
| 398 | index_optim = index; |
|
| 399 | dist_optim = dist; |
|
| 400 | ||
| 401 | clusters[index_optim].append(index_point); |
|
| 402 | ||
| 403 | return clusters; |
|
| 404 | ||
| 405 | ||
| 406 | def __update_centers(self, clusters): |
|
| 407 | """! |
|
| 408 | @brief Updates centers of clusters in line with contained objects. |
|
| 409 | ||
| 410 | @param[in] clusters (list): Clusters that contain indexes of objects from data. |
|
| 411 | ||
| 412 | @return (list) Updated centers. |
|
| 413 | ||
| 414 | """ |
|
| 415 | ||
| 416 | centers = [[] for i in range(len(clusters))]; |
|
| 417 | dimension = len(self.__pointer_data[0]) |
|
| 418 | ||
| 419 | for index in range(len(clusters)): |
|
| 420 | point_sum = [0.0] * dimension; |
|
| 421 | ||
| @@ 155-173 (lines=19) @@ | ||
| 152 | return clusters; |
|
| 153 | ||
| 154 | ||
| 155 | def __update_centers(self): |
|
| 156 | """! |
|
| 157 | @brief Calculate centers of clusters in line with contained objects. |
|
| 158 | ||
| 159 | @return (list) Updated centers as list of centers. |
|
| 160 | ||
| 161 | """ |
|
| 162 | ||
| 163 | centers = [[] for i in range(len(self.__clusters))]; |
|
| 164 | ||
| 165 | for index in range(len(self.__clusters)): |
|
| 166 | point_sum = [0] * len(self.__pointer_data[0]); |
|
| 167 | ||
| 168 | for index_point in self.__clusters[index]: |
|
| 169 | point_sum = list_math_addition(point_sum, self.__pointer_data[index_point]); |
|
| 170 | ||
| 171 | centers[index] = list_math_division_number(point_sum, len(self.__clusters[index])); |
|
| 172 | ||
| 173 | return centers; |
|
| 174 | ||