def spherical_k_means(<EOL>X,<EOL>n_clusters,<EOL>sample_weight=None,<EOL>init="<STR_LIT>",<EOL>n_init=<NUM_LIT:10>,<EOL>max_iter=<NUM_LIT>,<EOL>verbose=False,<EOL>tol=<NUM_LIT>,<EOL>random_state=None,<EOL>copy_x=True,<EOL>n_jobs=<NUM_LIT:1>,<EOL>algorithm="<STR_LIT>",<EOL>return_n_iter=False,<EOL>):
if n_init <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % n_init<EOL>)<EOL><DEDENT>random_state = check_random_state(random_state)<EOL>if max_iter <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % max_iter<EOL>)<EOL><DEDENT>best_inertia = np.infty<EOL>order = "<STR_LIT:C>" if copy_x else None<EOL>X = check_array(<EOL>X, accept_sparse="<STR_LIT>", dtype=[np.float64, np.float32], order=order, copy=copy_x<EOL>)<EOL>if _num_samples(X) < n_clusters:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>" % (_num_samples(X), n_clusters)<EOL>)<EOL><DEDENT>tol = _tolerance(X, tol)<EOL>if hasattr(init, "<STR_LIT>"):<EOL><INDENT>init = check_array(init, dtype=X.dtype.type, order="<STR_LIT:C>", copy=True)<EOL>_validate_center_shape(X, n_clusters, init)<EOL>if n_init != <NUM_LIT:1>:<EOL><INDENT>warnings.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % n_init,<EOL>RuntimeWarning,<EOL>stacklevel=<NUM_LIT:2>,<EOL>)<EOL>n_init = <NUM_LIT:1><EOL><DEDENT><DEDENT>x_squared_norms = row_norms(X, squared=True)<EOL>if n_jobs == <NUM_LIT:1>:<EOL><INDENT>for it in range(n_init):<EOL><INDENT>labels, inertia, centers, n_iter_ = _spherical_kmeans_single_lloyd(<EOL>X,<EOL>n_clusters,<EOL>sample_weight,<EOL>max_iter=max_iter,<EOL>init=init,<EOL>verbose=verbose,<EOL>tol=tol,<EOL>x_squared_norms=x_squared_norms,<EOL>random_state=random_state,<EOL>)<EOL>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>best_n_iter = n_iter_<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>seeds = random_state.randint(np.iinfo(np.int32).max, size=n_init)<EOL>results = Parallel(n_jobs=n_jobs, verbose=<NUM_LIT:0>)(<EOL>delayed(_spherical_kmeans_single_lloyd)(<EOL>X,<EOL>n_clusters,<EOL>sample_weight,<EOL>max_iter=max_iter,<EOL>init=init,<EOL>verbose=verbose,<EOL>tol=tol,<EOL>x_squared_norms=x_squared_norms,<EOL>random_state=seed,<EOL>)<EOL>for seed in seeds<EOL>)<EOL>labels, inertia, centers, n_iters = zip(*results)<EOL>best = np.argmin(inertia)<EOL>best_labels = labels[best]<EOL>best_inertia = inertia[best]<EOL>best_centers = centers[best]<EOL>best_n_iter = n_iters[best]<EOL><DEDENT>if return_n_iter:<EOL><INDENT>return best_centers, best_labels, best_inertia, best_n_iter<EOL><DEDENT>else:<EOL><INDENT>return best_centers, best_labels, best_inertia<EOL><DEDENT>
Modified from sklearn.cluster.k_means_.k_means.