def _spherical_kmeans_single_lloyd(<EOL>X,<EOL>n_clusters,<EOL>sample_weight=None,<EOL>max_iter=<NUM_LIT>,<EOL>init="<STR_LIT>",<EOL>verbose=False,<EOL>x_squared_norms=None,<EOL>random_state=None,<EOL>tol=<NUM_LIT>,<EOL>precompute_distances=True,<EOL>):
random_state = check_random_state(random_state)<EOL>sample_weight = _check_sample_weight(X, sample_weight)<EOL>best_labels, best_inertia, best_centers = None, None, None<EOL>centers = _init_centroids(<EOL>X, n_clusters, init, random_state=random_state, x_squared_norms=x_squared_norms<EOL>)<EOL>if verbose:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>distances = np.zeros(shape=(X.shape[<NUM_LIT:0>],), dtype=X.dtype)<EOL>for i in range(max_iter):<EOL><INDENT>centers_old = centers.copy()<EOL>labels, inertia = _labels_inertia(<EOL>X,<EOL>sample_weight,<EOL>x_squared_norms,<EOL>centers,<EOL>precompute_distances=precompute_distances,<EOL>distances=distances,<EOL>)<EOL>if sp.issparse(X):<EOL><INDENT>centers = _k_means._centers_sparse(<EOL>X, sample_weight, labels, n_clusters, distances<EOL>)<EOL><DEDENT>else:<EOL><INDENT>centers = _k_means._centers_dense(<EOL>X, sample_weight, labels, n_clusters, distances<EOL>)<EOL><DEDENT>centers = normalize(centers)<EOL>if verbose:<EOL><INDENT>print("<STR_LIT>" % (i, inertia))<EOL><DEDENT>if best_inertia is None or inertia < best_inertia:<EOL><INDENT>best_labels = labels.copy()<EOL>best_centers = centers.copy()<EOL>best_inertia = inertia<EOL><DEDENT>center_shift_total = squared_norm(centers_old - centers)<EOL>if center_shift_total <= tol:<EOL><INDENT>if verbose:<EOL><INDENT>print(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (i, center_shift_total, tol)<EOL>)<EOL><DEDENT>break<EOL><DEDENT><DEDENT>if center_shift_total > <NUM_LIT:0>:<EOL><INDENT>best_labels, best_inertia = _labels_inertia(<EOL>X,<EOL>sample_weight,<EOL>x_squared_norms,<EOL>best_centers,<EOL>precompute_distances=precompute_distances,<EOL>distances=distances,<EOL>)<EOL><DEDENT>return best_labels, best_inertia, best_centers, i + <NUM_LIT:1><EOL>
Modified from sklearn.cluster.k_means_.k_means_single_lloyd.