• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python vq.kmeans2函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中scipy.cluster.vq.kmeans2函数的典型用法代码示例。如果您正苦于以下问题:Python kmeans2函数的具体用法?Python kmeans2怎么用?Python kmeans2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了kmeans2函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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


示例16: test_kmeans_lost_cluster

    def test_kmeans_lost_cluster(self):
        # This will cause kmean to have a cluster with no points.
        data = TESTDATA_2D
        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:dyao-vu,项目名称:meta-core,代码行数:13,代码来源:test_vq.py


示例17: gencode

def gencode(image, k, oldcenters=None):
  t1 = time.time()
  npix = image.size / 3
  P = np.reshape(image, (npix, 3), order='F')
  Pw = vq.whiten(P)
  if oldcenters == None:  
    (centers, label) = vq.kmeans2(Pw, k, iter=30)
  else:
    (centers, label) = vq.kmeans2(Pw, oldcenters, iter=5)
  (code, distortion) = vq.vq(Pw, centers)
  code = np.reshape(code, image.shape[0:2], order='F')
  print time.time() - t1
  return code, centers
开发者ID:k3daevin,项目名称:ochi,代码行数:13,代码来源:colreduce.py


示例18: kmeans

    def kmeans(self, id, k=5, is_row=True):
        """
        K-means clustering. http://en.wikipedia.org/wiki/K-means_clustering

        Clusterizes the (cols) values of a given row, or viceversa

        :param id: row (or col) id to cluster its values
        :param k: number of clusters
        :param is_row: is param *id* a row (or a col)?
        :type is_row: Boolean
        """
        # TODO: switch to Pycluster?
        # http://pypi.python.org/pypi/Pycluster
        if VERBOSE:
            sys.stdout.write('Computing k-means, k=%s, for id %s\n' % (k, id))
        point = None
        if is_row:
            point = self.get_matrix().get_row(id)
        else:
            point = self.get_matrix().get_col(id)
        points = []
        points_id = []
        for i in point.nonzero_entries():
            label = point.label(i)
            points_id.append(label)
            if not is_row:
                points.append(self.get_matrix().get_row(label))
            else:
                points.append(self.get_matrix().get_col(label))
        #return kmeans(array(points), k)
        if VERBOSE:
            sys.stdout.write('id %s has %s points\n' % (id, len(points)))
        M = array(points)

        MAX_POINTS = 150
        # Only apply Matrix initialization if num. points is not that big!
        if len(points) <= MAX_POINTS:
            centers = self._kinit(array(points), k)
            centroids, labels = kmeans2(M, centers, minit='matrix')
        else:
            centroids, labels = kmeans2(M, k, minit='random')
        i = 0
        clusters = dict()
        for cluster in labels:
            if not clusters.has_key(cluster): 
                clusters[cluster] = dict()
                clusters[cluster]['centroid'] = centroids[cluster]
                clusters[cluster]['points'] = []
            clusters[cluster]['points'].append(points_id[i])
            i += 1
        return clusters
开发者ID:Maksymdelta,项目名称:python-recsys,代码行数:51,代码来源:baseclass.py


示例19: test_kmeans_lost_cluster

    def test_kmeans_lost_cluster(self):
        # This will cause kmeans to have a cluster with no points.
        data = TESTDATA_2D
        initk = np.array([[-1.8127404, -0.67128041],
                         [2.04621601, 0.07401111],
                         [-2.31149087,-0.05160469]])

        with suppress_warnings() as sup:
            sup.filter(UserWarning,
                       "One of the clusters is empty. Re-run kmean with a different initialization")
            kmeans(data, initk)
            kmeans2(data, initk, missing='warn')

        assert_raises(ClusterError, kmeans2, data, initk, missing='raise')
开发者ID:Brucechen13,项目名称:scipy,代码行数:14,代码来源:test_vq.py


示例20: train_classifier

def train_classifier(train_inds, dict_size=300, shuffle=False):
    # load OFH descriptors from training videos from all-but-two classes
    train_action_n, train_video_n, train_descs, train_labels = load_actions(actions[train_inds])

    # cluster and quantize to produce BoW descriptors
    print 'clustering...'
    print 'train_descs:', train_descs.shape
    if path.exists(path.join(savedir, 'clusters.npy')):
        clusters = np.load(path.join(savedir, 'clusters.npy'))
        cluster_inds = np.load(path.join(savedir, 'cluster_inds.npy'))
    else:
        clusters, cluster_inds = vq.kmeans2(train_descs, dict_size, iter=20, minit='points')
        np.save(path.join(savedir, 'clusters.npy'), clusters)
        np.save(path.join(savedir, 'cluster_inds.npy'), cluster_inds)

    if shuffle:
        random.shuffle(train_labels)

    # produce quantized histograms for each training video
    print 'quantizing...'
    f = path.join(savedir, 'train_hists.npy')
    if path.exists(f):
        train_hists = np.load(f)
    else:
        train_hists = get_desc_hists(clusters, train_descs, train_video_n)
        np.save(f, train_hists)

    # linearly regress for each attribute based on manually produced labels
    print 'training regressors...'
    cls = lin_reg.train(train_hists, train_labels)

    return clusters, cls
开发者ID:nrafidi,项目名称:ML_project,代码行数:32,代码来源:cross_validation.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python vq.py_vq函数代码示例发布时间:2022-05-27
下一篇:
Python vq.kmeans函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap