k表示具有有限内存的聚类

时间:2013-07-17 01:35:37

标签: python google-app-engine scipy cluster-analysis k-means

我正在App Engine上开发一个应用程序,并且正在使用SciPy的kmeans2。

当群集运行时,我收到此错误:

Exceeded soft private memory limit with 159.852 MB after servicing 1 requests total

以下是我正在做的事情,color_data约为500万x,y,z点:

def _cluster(color_data, k):
  """ Clusters colors and return top k 

      Arguments:
      ----------
        color_data
          TYPE: list
          DESC: The pixel rgb values to cluster
        k
          TYPE: int
          DESC: number of clusters to find in the colors

      Returns:
      --------
        sorted_colors
          TYPE: list
          DESC: A list of rgb centroids for each color cluster
  """

  # make rgbs into x,y,z points
  x,y,z = [],[],[]
  for color in color_data:
    x.append(color[0])
    y.append(color[1])
    z.append(color[2])

  # averaged_colors are points at center of color clusters
  # labels are cluster numbers for each point
  averaged_colors, labels = kmeans2(array(zip(x,y,z)), k, iter=10)

  # get count of nodes per cluster
  frequencies = {}
  for i in range(k):
    frequencies[i] = labels.tolist().count(i)

  # sort labels on frequency
  sorted_labels = sorted(frequencies.iteritems(), key=itemgetter(1))

  # sort colors on label they belong to
  sorted_colors = []
  for l in sorted_labels:
    sorted_colors.append(tuple(averaged_colors[l[0]].tolist()))

  return sorted_colors

如何在128MB以下的内存中完成此操作?

编辑: 在我的本地计算机上,运行我的应用程序显示我的活动监视器中使用了~500 MB的内存

2 个答案:

答案 0 :(得分:1)

请勿使用所有像素。

如果你只使用10%或更少的像素,K-Means通常会返回几乎相同的结果。因为它计算均值,并且平均值不再有太大变化,如果你添加更多信息,除非数据的分布不同。

只使用10%的像素才能使应用程序使用更少的内存。

答案 1 :(得分:0)

如果您无法减少操作的持续内存使用量,请you should look for this answer获取有关增加应用内存分配或更改为其他提供商的建议。每月20美元这是机架空间服务器的简单请求,虽然根据定义它更接近金属并需要更多设置。

相关问题