本文整理汇总了Python中scipy.cluster.vq.kmeans2函数的典型用法代码示例。如果您正苦于以下问题:Python kmeans2函数的具体用法?Python kmeans2怎么用?Python kmeans2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了kmeans2函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _init_responsibilities
def _init_responsibilities( self, data ):
'''
Intialise responsibilities via k-means clustering.
'''
a_1 = np.asarray( data.a['normal'], dtype=np.float64 )
b_1 = np.asarray( data.b['normal'], dtype=np.float64 )
p_1 = a_1 / ( a_1 + b_1 )
a_2 = np.asarray( data.a['tumour'], dtype=np.float64 )
b_2 = np.asarray( data.b['tumour'], dtype=np.float64 )
p_2 = a_2 / ( a_2 + b_2 )
shape = ( data.nrows, 9 )
responsibilities = np.zeros( shape )
init_centers = np.array( ( 1., 0.5, 0. ) )
cluster_centers_1, labels_1 = kmeans2( p_1, init_centers, minit='matrix' )
cluster_centers_2, labels_2 = kmeans2( p_2, init_centers, minit='matrix' )
labels = 3 * labels_1 + labels_2
for id in range( 9 ):
index = labels == id
responsibilities[index, id] = 1.
self.responsibilities = responsibilities
示例2: _discover_centroids
def _discover_centroids(self, dataset_input):
self.centroids, labels = kmeans2(dataset_input, self.n_centroids)
while np.unique(labels).shape[0] != self.n_centroids:
# print "Empty cluster found. Retrying kmeans.."
self.centroids, labels = kmeans2(dataset_input, self.n_centroids)
return (self.centroids, labels)
示例3: kMeansCluster
def kMeansCluster(x, k, trials):
"""kMeansCluster performs k means clustering on a dataset
:param x: a data object (must contain field 'data')
:type x: dict
:param k: the number of centroids to cluster to
:type k: int
:param trials: the number of times to run kmeans2 (will be run with both 'random'
and 'points'. The best of the two trials will be used.
:type trials: int
:returns: a dictionary with keys idx and cents.
idx is the group number for each protein (in the orde given in the x data object
cents is a list of rowVectors with the centroids for each cluster
"""
data = x['data']
centsR, idxR = scv.kmeans2(data.copy(), k, iter=trials, minit='random')
centsP, idxP = scv.kmeans2(data.copy(), k, iter=trials, minit='points')
distR = calcDistortion(centsR, idxR, data)
distP = calcDistortion(centsP, idxP, data)
if distR > distP:
centsR = centsP
idxR = idxP
distR = distP
return {'idx': idxR, 'cents':centsR}
示例4: getKmeans
def getKmeans(a, k, threshold=1, iter=40, thresh=1e-05, minit="random", missing="warn"):
"""input : a, k threshold
output : atk
"""
if minit == "matrix":
seeds, k = k, len(k)
a.k = k # initialise (could move it to __init__ but not bothered for the moment)
height, width = a.matrix.shape
pixels = a.matrix > threshold
print "width, height:", width, height # debug
print "sum of relevant pixels:", sum(sum(pixels)) # debug
dataPoints = [[(i, j) for i in range(width) if pixels[j, i]] for j in range(height)]
dataPoints = sum(dataPoints, [])
dataPoints = np.array(dataPoints)
print dataPoints[:20]
if minit == "matrix":
a.centroids = kmeans2(data=dataPoints, k=seeds, iter=iter, thresh=thresh, minit=minit, missing=missing)
else:
a.centroids = kmeans2(data=dataPoints, k=k, iter=iter, thresh=thresh, minit=minit, missing=missing)
a.data = dataPoints
resultPattern = ma.zeros((height, width))
resultPattern.mask = True
resultPattern.fill_value = -999
for i in range(len(dataPoints)):
resultPattern[dataPoints[i][1], dataPoints[i][0]] = a.centroids[1][i]
resultPattern = dbz(
name="Clustering for %s with %d clusters" % (a.name, k + 1), matrix=resultPattern, vmin=0, vmax=k
)
atk = {"centroids": a.centroids, "data": a.data, "pattern": resultPattern}
return atk
示例5: RunClustering
def RunClustering(self,N,vector,K0):
data = vector.reshape(N**2,3)
import scipy.cluster.vq as vq
resmap,indexmap = vq.kmeans2(data,K0,iter=50,minit='random')
newresmap,indexmap = vq.kmeans2(data,resmap,iter=50,minit='matrix')
self.indexmap = indexmap.reshape(N,N)
self.CheckTopology(N)
示例6: cluster
def cluster(dataArray):
warnings.filterwarnings('error')
bestKmeans=None
#Gross code to handle warning from numpy for an empty cluster
while bestKmeans is None:
try:
bestKmeans, bestMapping=kmeans2(dataArray, 5)
except:
pass
minDB=DaviesBouldinIndex(bestKmeans, bestMapping, dataArray).getDBindex()
for numClusters in range(5,11):
kmeans=None
while kmeans is None:
try:
kmeans, mapping=kmeans2(dataArray, numClusters)
except:
pass
#print "Valid cluster created with numClusters:%i." % numClusters
db=DaviesBouldinIndex(kmeans, mapping, dataArray).getDBindex()
if db<minDB:
minDB=db
bestKmeans=kmeans
bestMapping=mapping
return bestKmeans, minDB, bestMapping
示例7: test_kmeans2_simple
def test_kmeans2_simple(self):
initc = np.concatenate(([[X[0]], [X[1]], [X[2]]]))
code = initc.copy()
code1 = kmeans2(X, code, iter=1)[0]
code2 = kmeans2(X, code, iter=2)[0]
assert_array_almost_equal(code1, CODET1)
assert_array_almost_equal(code2, CODET2)
示例8: test_kmeans2_simple
def test_kmeans2_simple(self):
initc = np.concatenate(([[X[0]], [X[1]], [X[2]]]))
for tp in np.array, np.matrix:
code1 = kmeans2(tp(X), tp(initc), iter=1)[0]
code2 = kmeans2(tp(X), tp(initc), iter=2)[0]
assert_array_almost_equal(code1, CODET1)
assert_array_almost_equal(code2, CODET2)
示例9: test_kmeans2_rank1
def test_kmeans2_rank1(self):
data = TESTDATA_2D
data1 = data[:, 0]
initc = data1[:3]
code = initc.copy()
kmeans2(data1, code, iter=1)[0]
kmeans2(data1, code, iter=2)[0]
示例10: train
def train(self,white=False):
'''
each train change everything
'''
if (white):
self.centroids,self.labels=kmeans2(whiten(self.X),self.K,minit='random', missing='warn')
else:
self.centroids,self.labels=kmeans2(self.X,self.K,minit='random', missing='warn')
示例11: test_kmeans2_empty
def test_kmeans2_empty(self):
"""Ticket #505."""
try:
kmeans2([], 2)
raise AssertionError("This should not succeed.")
except ValueError, e:
# OK, that's what we expect
pass
示例12: test_kmeans2_rank1
def test_kmeans2_rank1(self):
data = np.fromfile(DATAFILE1, sep=", ")
data = data.reshape((200, 2))
data1 = data[:, 0]
initc = data1[:3]
code = initc.copy()
kmeans2(data1, code, iter=1)[0]
kmeans2(data1, code, iter=2)[0]
示例13: test_kmeans2_simple
def test_kmeans2_simple(self):
"""Testing simple call to kmeans2 and its results."""
initc = np.concatenate(([[X[0]], [X[1]], [X[2]]]))
code = initc.copy()
code1 = kmeans2(X, code, iter=1)[0]
code2 = kmeans2(X, code, iter=2)[0]
assert_array_almost_equal(code1, CODET1)
assert_array_almost_equal(code2, CODET2)
示例14: test_kmeans2_rank1
def test_kmeans2_rank1(self):
"""Testing simple call to kmeans2 with rank 1 data."""
data = np.fromfile(DATAFILE1, sep=", ")
data = data.reshape((200, 2))
data1 = data[:, 0]
data2 = data[:, 1]
initc = data1[:3]
code = initc.copy()
code1 = kmeans2(data1, code, iter=1)[0]
code2 = kmeans2(data1, code, iter=2)[0]
示例15: test_kmeans_lost_cluster
def test_kmeans_lost_cluster(self):
# This will cause kmean to have a cluster with no points.
data = np.fromfile(DATAFILE1, sep=", ")
data = data.reshape((200, 2))
initk = np.array([[-1.8127404, -0.67128041], [2.04621601, 0.07401111], [-2.31149087, -0.05160469]])
kmeans(data, initk)
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
kmeans2(data, initk, missing="warn")
assert_raises(ClusterError, kmeans2, data, initk, missing="raise")