def fit(self, data):
"""
Run K-Means on data n_init times.
Parameters
----------
data: numpy array
Returns
-------
No value is returned.
Function sets the following two object params:
self.labels_
self.cluster_centers_
"""
data = np.array(data)
labels, cluster_centers = [], []
for i in range(self.n_init):
if not self.warm_start:
self.cluster_centers_ = None
self._global_covar_matrices = None
self._inv_covar_matrices = None
self._fit(data)
labels += [self.labels_]
cluster_centers += [self.cluster_centers_]
self.inertias_ += [self._inertia(data)]
self.log_likelihoods_ += [self.log_likelihood(data)]
best_idx = np.argmin(self.inertias_)
self.labels_ = labels[best_idx]
self.all_labels_ = labels
self.best_log_likelihood_ = self.log_likelihoods_[best_idx]
self.best_inertia_ = self.inertias_[best_idx]
self.cluster_centers_ = cluster_centers[best_idx]
if self.verbose == 1:
print('fit: n_clusters: {}, label bin count: {}'.format(self.n_clusters, np.bincount(self.labels_, minlength=self.n_clusters)))
评论列表
文章目录