本文整理匯總了Python中sklearn.cluster.k_means方法的典型用法代碼示例。如果您正苦於以下問題:Python cluster.k_means方法的具體用法?Python cluster.k_means怎麽用?Python cluster.k_means使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sklearn.cluster
的用法示例。
在下文中一共展示了cluster.k_means方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_k_means_non_collapsed
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import k_means [as 別名]
def test_k_means_non_collapsed():
# Check k_means with a bad initialization does not yield a singleton
# Starting with bad centers that are quickly ignored should not
# result in a repositioning of the centers to the center of mass that
# would lead to collapsed centers which in turns make the clustering
# dependent of the numerical unstabilities.
my_X = np.array([[1.1, 1.1], [0.9, 1.1], [1.1, 0.9], [0.9, 1.1]])
array_init = np.array([[1.0, 1.0], [5.0, 5.0], [-5.0, -5.0]])
km = KMeans(init=array_init, n_clusters=3, random_state=42, n_init=1)
km.fit(my_X)
# centers must not been collapsed
assert_equal(len(np.unique(km.labels_)), 3)
centers = km.cluster_centers_
assert_true(np.linalg.norm(centers[0] - centers[1]) >= 0.1)
assert_true(np.linalg.norm(centers[0] - centers[2]) >= 0.1)
assert_true(np.linalg.norm(centers[1] - centers[2]) >= 0.1)
示例2: kmeans
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import k_means [as 別名]
def kmeans(xs, k):
assert xs.ndim == 2
try:
from sklearn.cluster import k_means
_, labels, _ = k_means(xs.astype('float64'), k)
except ImportError:
from scipy.cluster.vq import kmeans2
_, labels = kmeans2(xs, k, missing='raise')
return labels
示例3: _spectral_clustering
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import k_means [as 別名]
def _spectral_clustering(self):
affinity_matrix_ = check_symmetric(self.affinity_matrix_)
random_state = check_random_state(self.random_state)
laplacian = sparse.csgraph.laplacian(affinity_matrix_, normed=True)
_, vec = sparse.linalg.eigsh(sparse.identity(laplacian.shape[0]) - laplacian,
k=self.n_clusters, sigma=None, which='LA')
embedding = normalize(vec)
_, self.labels_, _ = cluster.k_means(embedding, self.n_clusters,
random_state=random_state, n_init=self.n_init)
示例4: test_k_means
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import k_means [as 別名]
def test_k_means(self):
iris = datasets.load_iris()
df = pdml.ModelFrame(iris)
result = df.cluster.k_means(3, random_state=self.random_state)
expected = cluster.k_means(iris.data, 3, random_state=self.random_state)
self.assertEqual(len(result), 3)
self.assert_numpy_array_almost_equal(result[0], expected[0])
self.assertIsInstance(result[1], pdml.ModelSeries)
tm.assert_index_equal(result[1].index, df.index)
tm.assert_numpy_array_equal(result[1].values, expected[1])
self.assertAlmostEqual(result[2], expected[2])
示例5: _kmeans_initialization
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import k_means [as 別名]
def _kmeans_initialization(self, residuals):
"""Computes k-means with k = 2 to find the initial components (rows or columns) of a new layer/bicluster."""
_, labels, _ = k_means(residuals, n_clusters=2, n_init=self.initialization_iterations, init='random', n_jobs=1)
count0, count1 = np.bincount(labels)
if count0 <= count1:
return np.where(labels == 0)[0]
return np.where(labels == 1)[0]
示例6: test_k_means_function
# 需要導入模塊: from sklearn import cluster [as 別名]
# 或者: from sklearn.cluster import k_means [as 別名]
def test_k_means_function():
# test calling the k_means function directly
# catch output
old_stdout = sys.stdout
sys.stdout = StringIO()
try:
cluster_centers, labels, inertia = k_means(X, n_clusters=n_clusters,
verbose=True)
finally:
sys.stdout = old_stdout
centers = cluster_centers
assert_equal(centers.shape, (n_clusters, n_features))
labels = labels
assert_equal(np.unique(labels).shape[0], n_clusters)
# check that the labels assignment are perfect (up to a permutation)
assert_equal(v_measure_score(true_labels, labels), 1.0)
assert_greater(inertia, 0.0)
# check warning when centers are passed
assert_warns(RuntimeWarning, k_means, X, n_clusters=n_clusters,
init=centers)
# to many clusters desired
assert_raises(ValueError, k_means, X, n_clusters=X.shape[0] + 1)