纵向拼接
np.concatenate([a,b], axis = 0)
np.vstack([a,b])
np.r_[a,b]
使用以上方法进行纵向拼接时,要注意被拼接array的维度相同,空的array不可以和非空的拼接。一般都需要对array进行reshape操作。
ptsInClust = np.array([0,0]).reshape([1,2])
x = np.array(X[i,:]).reshape([1,2])
ptsInClust = np.concatenate([ptsInClust,x],axis = 0)
寻找多维array中是否存在某一行指定的array
L 是一个[400,2]的多维array / 矩阵,需要从这400行里找出和
c
c
c 完全一样的行。
np.where((L == c).all(1))
Out[92]:
(array([102, 103, 168, 169, 183, 203, 207, 238, 272, 300, 301, 302, 303,
304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316,
317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329,
330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342,
343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355,
356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368,
369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381,
382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394,
395, 396, 397, 398, 399]),)
使用np.where得到与 c 相等的行编号,可作为索引得到X对应的行,这个X是我要处理的数据。
ptsInClust_ = X[np.where((L == c).all(1)),:]
ptsInClust = ptsInClust_.reshape([ptsInClust_.shape[1],2])
对于提取出的数据需要注意它的维数,如果不符合需求可以使用reshape重新构造。