使用Index分割numpy的数组 [英] Split Numpy array using Index
本文介绍了使用Index分割numpy的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三维数组
pcar=[[xa ya za]
[xb yb zb]
.
.
[xn yn zn]]
和指数数组
[0,1,0....,2]
这使得在 PCAR
行应在集群去,这样我可以在不同的颜色绘制。簇的最大值为3。
which gives rows in pcar
should go in which cluster so that I can plot it in different color. The maximum value of clusters is 3.
输出应
clusters[0] = [[xa ya za], [xc yc zc], ...]
clusters[1] = [[xb yb zb], ...]
clusters[2] = [..., [xn yn zn]]
和我想打印点图形具有不同颜色的每个群集点
and I want to print that points in graph with each cluster points having different color
推荐答案
据我了解,你想生成像集群的列表如下:
As I understand it, you want to generate a list of clusters like the following:
clusters[0] = [[xa ya za], [xc yc zc], ...]
clusters[1] = [[xb yb zb], ...]
clusters[2] = [..., [xn yn zn]]
这是很容易与numpy的索引做。让指数阵列被称作指数
。然后,
This is quite easy to do with NumPy indexing. Let the index array be called indices
. Then,
indices = np.asarray(indices)
num_clusters = 3
clusters = [pcar[indices==i] for i in xrange(num_clusters)]
会给你想要的结果。
will give you the desired result.
这篇关于使用Index分割numpy的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文