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


Python vq.kmeans方法代码示例

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


在下文中一共展示了vq.kmeans方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Kmeans

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def Kmeans(file, vocabfile, k):
  np.random.seed((1000,2000))
  whitened = whiten(embeddings)
  codebook, distortion = kmeans(whitened, k)
  clusters = [l2_nearest(embeddings, c, representatives+1) for c in codebook]
  # output
  print(len(codebook), distortion)
  for centroid in codebook:
    print(' '.join([str(x) for x in centroid]))
  print()
  for cluster in clusters:
    print(' '.join([id_word[i] for i, d in cluster]).encode('utf-8'))
  print()
  # assign clusters to words
  codes, _ = vq(embeddings, codebook)
  for w, c in zip(word_id.keys(), codes):
    print(w, c) 
开发者ID:attardi,项目名称:deepnl,代码行数:19,代码来源:knn.py

示例2: test_large_features

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def test_large_features(self):
        # Generate a data set with large values, and run kmeans on it to
        # (regression for 1077).
        d = 300
        n = 100

        m1 = np.random.randn(d)
        m2 = np.random.randn(d)
        x = 10000 * np.random.randn(n, d) - 20000 * m1
        y = 10000 * np.random.randn(n, d) + 20000 * m2

        data = np.empty((x.shape[0] + y.shape[0], d), np.double)
        data[:x.shape[0]] = x
        data[x.shape[0]:] = y

        kmeans(data, 2) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:18,代码来源:test_vq.py

示例3: test_kmeans_lost_cluster

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
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]])

        res = kmeans(data, initk)

        warn_ctx = WarningManager()
        warn_ctx.__enter__()
        try:
            warnings.simplefilter('ignore', UserWarning)
            res = kmeans2(data, initk, missing='warn')
        finally:
            warn_ctx.__exit__()

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

示例4: kMeansClustering

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def kMeansClustering(x,k):

    # Convert list into numpy format
    conv = np.asarray(x)

    # Compute the centroids
    centroids = kmeans(conv,k,iter=10)[0]

    # Relabel the x's
    labels = []
    for y in range(len(x)):
        minDist = float('inf')
        minLabel = -1
        for z in range(len(centroids)):
            e = euclidean(conv[y],centroids[z])
            if (e < minDist):
                minDist = e
                minLabel = z
        labels.append(minLabel)

    # Return the list of centroids and labels
    return (centroids,labels)

# Performs a weighted clustering on the examples in xTest
# Returns a 1-d vector of predictions 
开发者ID:lbenning,项目名称:Load-Forecasting,代码行数:27,代码来源:clustering.py

示例5: cluster_lon_lats

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def cluster_lon_lats(self):
        """Clusters the list of lon_lats into groups """
        np_lon_lats = []
        for lon_lat in self.lon_lats:
            dpoint = np.fromiter(lon_lat, np.dtype('float'))
            np_lon_lats.append(dpoint)
        data = array(np_lon_lats)
        centroids, _ = kmeans(data, self.number_clusters)
        idx, _ = vq(data, centroids)
        self.idx = idx
        self.data = data
        self.centroids = centroids
        # Sort the centroids by lon, then lat
        sc = centroids[centroids[:,1].argsort()]
        sc = sc[sc[:,0].argsort()]
        self.sorted_centroids = sc.tolist() 
开发者ID:ekansa,项目名称:open-context-py,代码行数:18,代码来源:clustergeojson.py

示例6: __init__

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def __init__(self, data, kk):
		# Convolutional K-means 
		# INPUT:
		# data: matrix each column is a sample vector
		# kk: number of total clusters
		# ii: number of iterations for kmeans training
		# OUTPUT:
		# D: matrix containing center vectors in columns"""

		print('starting kmeans quatization...(.py file is used)')
		# Initialization of D by randomly pick from training data
		col_idx = random.sample(range(0, len(data)), kk)
		D = data[col_idx, :]
		D = self.colnorm(D)
		self.data = data
		self.kk = kk
		self.D = D 
开发者ID:zchengquan,项目名称:TextDetector,代码行数:19,代码来源:ckmean.py

示例7: colorz

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def colorz(fd, n=DEFAULT_NUM_COLORS, min_v=DEFAULT_MINV, max_v=DEFAULT_MAXV,
           bold_add=DEFAULT_BOLD_ADD, order_colors=True):
    """
    Get the n most dominant colors of an image.
    Clamps value to between min_v and max_v.

    Creates bold colors using bold_add.
    Total number of colors returned is 2*n, optionally ordered by hue.
    Returns as a list of pairs of RGB triples.

    For terminal colors, the hue order is:
    red, yellow, green, cyan, blue, magenta
    """
    img = Image.open(fd)
    img.thumbnail(THUMB_SIZE)

    obs = get_colors(img)
    clamped = [clamp(color, min_v, max_v) for color in obs]
    clusters, _ = kmeans(array(clamped).astype(float), n)
    colors = order_by_hue(clusters) if order_colors else clusters
    return list(zip(colors, [brighten(c, bold_add) for c in colors])) 
开发者ID:metakirby5,项目名称:colorz,代码行数:23,代码来源:colorz.py

示例8: mean_on_most_assigned

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def mean_on_most_assigned(x, c):
    nb_c = len(c)
    assign = np.zeros(nb_c)
    mean = np.zeros(c.shape)
    for i in range(len(x)):
        y = x[i].reshape((1,2))
        d = np.sqrt(np.sum(np.power(y.repeat(nb_c, axis=0) - c, 2), axis=1))
        idx = d.argmin()
        assign[idx] += 1
        mean[idx,:] += x[i]
    idx = assign.argmax()
    return mean[idx,:] / assign[idx]

# def best_kmeans(pred):
    # plt.scatter(pred[:,0], pred[:,1], color='b')
    # c,v = kmeans(pred, 3)
    # plt.scatter(c[:,0], c[:,1], color='g')
    # n = most_assigned(pred, c)
    # plt.scatter(c[n,0], c[n,1], color='r')
    # plt.show() 
开发者ID:dluvizon,项目名称:deephar,代码行数:22,代码来源:cluster.py

示例9: sigma_bin_walls

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def sigma_bin_walls(sigma, bins):
        import scipy, scipy.cluster, scipy.cluster.vq as vq
        std = np.std(sigma)
        if np.isclose(std, 0): return pimms.imm_array([0, np.max(sigma)])
        cl = sorted(std * vq.kmeans(sigma/std, bins)[0])
        cl = np.mean([cl[:-1],cl[1:]], axis=0)
        return pimms.imm_array(np.concatenate(([0], cl, [np.max(sigma)]))) 
开发者ID:noahbenson,项目名称:neuropythy,代码行数:9,代码来源:cmag.py

示例10: get_palette

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def get_palette(samples, options, return_mask=False, kmeans_iter=40):

    '''Extract the palette for the set of sampled RGB values. The first
palette entry is always the background color; the rest are determined
from foreground pixels by running K-means clustering. Returns the
palette, as well as a mask corresponding to the foreground pixels.

    '''

    if not options.quiet:
        print('  getting palette...')

    bg_color = get_bg_color(samples, 6)

    fg_mask = get_fg_mask(bg_color, samples, options)

    centers, _ = kmeans(samples[fg_mask].astype(np.float32),
                        options.num_colors-1,
                        iter=kmeans_iter)

    palette = np.vstack((bg_color, centers)).astype(np.uint8)

    if not return_mask:
        return palette
    else:
        return palette, fg_mask

###################################################################### 
开发者ID:mzucker,项目名称:noteshrink,代码行数:30,代码来源:noteshrink.py

示例11: test_kmeans_simple

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def test_kmeans_simple(self):
        initc = np.concatenate(([[X[0]], [X[1]], [X[2]]]))
        code = initc.copy()
        code1 = kmeans(X, code, iter=1)[0]

        assert_array_almost_equal(code1, CODET2) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:8,代码来源:test_vq.py

示例12: test_kmeans_0k

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def test_kmeans_0k(self):
        """Regression test for #546: fail when k arg is 0."""
        assert_raises(ValueError, kmeans, X, 0)
        assert_raises(ValueError, kmeans2, X, 0)
        assert_raises(ValueError, kmeans2, X, np.array([])) 
开发者ID:ktraunmueller,项目名称:Computable,代码行数:7,代码来源:test_vq.py

示例13: run_kmeans

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def run_kmeans(self, X, K):
        """Runs k-means and returns the labels assigned to the data."""
        wX = vq.whiten(X)
        means, dist = vq.kmeans(wX, K, iter=100)
        labels, dist = vq.vq(wX, means)
        return means, labels 
开发者ID:urinieto,项目名称:msaf,代码行数:8,代码来源:xmeans.py

示例14: test_kmeans

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def test_kmeans(K=5):
    """Test k-means with the synthetic data."""
    X = XMeans.generate_2d_data(K=4)
    wX = vq.whiten(X)
    dic, dist = vq.kmeans(wX, K, iter=100)

    plt.scatter(wX[:, 0], wX[:, 1])
    plt.scatter(dic[:, 0], dic[:, 1], color="m")
    plt.show() 
开发者ID:urinieto,项目名称:msaf,代码行数:11,代码来源:xmeans.py

示例15: init_pseudo_inputs

# 需要导入模块: from scipy.cluster import vq [as 别名]
# 或者: from scipy.cluster.vq import kmeans [as 别名]
def init_pseudo_inputs(self):
        msg = "Dataset must have more than n_inducing [ %n ] to enable"
        msg += " inference with sparse pseudo inputs"
        assert self.N >= self.n_inducing, msg % (self.n_inducing)
        self.should_recompile = True
        # pick initial cluster centers from dataset
        X = self.X.get_value()
        X_sp_ = utils.kmeanspp(X, self.n_inducing)

        # perform kmeans to get initial cluster centers
        utils.print_with_stamp('Initialising pseudo inputs', self.name)
        X_sp_, dist = kmeans(X, X_sp_, iter=200, thresh=1e-9)
        # initialize symbolic tensor variable if necessary
        # (this will create the self.X_sp atttribute)
        self.set_params({'X_sp': X_sp_}) 
开发者ID:mcgillmrl,项目名称:kusanagi,代码行数:17,代码来源:SPGP.py


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