当前位置: 首页>>代码示例>>Python>>正文


Python vq.kmeans2函数代码示例

本文整理汇总了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
开发者ID:aroth85,项目名称:joint-snv-mix.release,代码行数:29,代码来源:latent_variables.py

示例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)
开发者ID:fakedrake,项目名称:beethoven,代码行数:7,代码来源:rbfn.py

示例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}
开发者ID:joeydavis,项目名称:clusteringModule,代码行数:27,代码来源:clusteringModule.py

示例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
开发者ID:rainly,项目名称:armor,代码行数:32,代码来源:clustering.py

示例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)
开发者ID:mattbierbaum,项目名称:cuda-plasticity,代码行数:7,代码来源:Clustering.py

示例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
开发者ID:mtriff,项目名称:YelpDataSetChallenge,代码行数:28,代码来源:cluster.py

示例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)
开发者ID:Arasz,项目名称:scipy,代码行数:8,代码来源:test_vq.py

示例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)
开发者ID:dyao-vu,项目名称:meta-core,代码行数:8,代码来源:test_vq.py

示例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]
开发者ID:dyao-vu,项目名称:meta-core,代码行数:8,代码来源:test_vq.py

示例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')
开发者ID:Agnesfen,项目名称:dml,代码行数:8,代码来源:kmeans.py

示例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
开发者ID:decarlin,项目名称:stuartlab-scripts,代码行数:8,代码来源:test_vq.py

示例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]
开发者ID:Arasz,项目名称:scipy,代码行数:9,代码来源:test_vq.py

示例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)
开发者ID:beiko-lab,项目名称:gengis,代码行数:9,代码来源:test_vq.py

示例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]
开发者ID:beiko-lab,项目名称:gengis,代码行数:11,代码来源:test_vq.py

示例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")
开发者ID:ymarfoq,项目名称:outilACVDesagregation,代码行数:12,代码来源:test_vq.py


注:本文中的scipy.cluster.vq.kmeans2函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。