使用np.unique从2个numpy数组中删除成对重复项

时间:2018-03-13 11:20:14

标签: python list duplicates coordinates unique

我创建了两个数组,如下所示:

     DotsLat1=np.concatenate((HLat,DotsLatMA,DotsLatMC,DotsLatMB,DotsLatMD,DotsLatMAB,DotsLatMAD,DotsLatMBC,DotsLatMDC), axis=0)
    DotsLon1=np.concatenate((HLon,DotsLonMA,DotsLonMC,DotsLonMB,DotsLonMD,DotsLonMAB,DotsLonMAD,DotsLonMBC,DotsLonMDC), axis=0)#

其中给出了以下纬度&经度分别为:

    array([51.43584   , 51.47806059, 51.47554269, 51.39361941, 51.39613731,
   51.43584   , 51.44428412, 51.45272824, 51.46117236, 51.46961647,
   51.39361941, 51.40206353, 51.41050764, 51.41895176, 51.42739588,
   51.43584   , 51.45172108, 51.46760215, 51.43584   , 51.41995892,
   51.40407785, 51.47604627, 51.4601652 , 51.46860931, 51.47705343,
   51.41252196, 51.42840304, 51.43684716, 51.44529128, 51.45915804,
   51.44327696, 51.43483284, 51.42638872, 51.39563373, 51.4115148 ,
   51.40307069, 51.39462657])
array([2.59277   , 2.72014661, 2.55890633, 2.46539339, 2.62663367,
   2.59277   , 2.61824532, 2.64372065, 2.66919597, 2.69467129,
   2.46539339, 2.49086871, 2.51634403, 2.54181935, 2.56729468,
   2.59277   , 2.57922453, 2.56567906, 2.59277   , 2.60631547,
   2.61986094, 2.59115438, 2.60469985, 2.63017518, 2.6556505 ,
   2.64533626, 2.63179079, 2.65726611, 2.68274144, 2.54020374,
   2.55374921, 2.52827389, 2.50279856, 2.59438562, 2.58084015,
   2.55536482, 2.5298895 ])

有些点具有相同的经度和相同的纬度(例如第一个)。如果BOTH纬度和经度有重复,我想删除两个数组中的那些点(所以如果点将在地图中相互绘制)。因此,保持正确的秩序非常重要。

当我使用

DotsLat2=np.unique(DotsLat1)
DotsLon2=np.unique(DotsLon1)

订单不再正确,我的分数也是分散的。

当我使用

DotsLat2=list(set([DotsLat1]))
DotsLon2=list(set([DotsLon1]))

错误是

unhashable type: 'numpy.ndarray'

知道如何摆脱错误并创建我的独特点吗?

2 个答案:

答案 0 :(得分:1)

import numpy as np
lat=np.array([51.43584   , 51.47806059, 51.47554269, 51.39361941, 51.39613731,
   51.43584   , 51.44428412, 51.45272824, 51.46117236, 51.46961647,
   51.39361941, 51.40206353, 51.41050764, 51.41895176, 51.42739588,
   51.43584   , 51.45172108, 51.46760215, 51.43584   , 51.41995892,
   51.40407785, 51.47604627, 51.4601652 , 51.46860931, 51.47705343,
   51.41252196, 51.42840304, 51.43684716, 51.44529128, 51.45915804,
   51.44327696, 51.43483284, 51.42638872, 51.39563373, 51.4115148 ,
   51.40307069, 51.39462657])
long=np.array([2.59277   , 2.72014661, 2.55890633, 2.46539339, 2.62663367,
   2.59277   , 2.61824532, 2.64372065, 2.66919597, 2.69467129,
   2.46539339, 2.49086871, 2.51634403, 2.54181935, 2.56729468,
   2.59277   , 2.57922453, 2.56567906, 2.59277   , 2.60631547,
   2.61986094, 2.59115438, 2.60469985, 2.63017518, 2.6556505 ,
   2.64533626, 2.63179079, 2.65726611, 2.68274144, 2.54020374,
   2.55374921, 2.52827389, 2.50279856, 2.59438562, 2.58084015,
   2.55536482, 2.5298895 ])

setl = np.column_stack((lat, long))
print(setl)
print(setl.shape)

setl2 = np.unique(setl, axis=0)
print( setl2 )
print(setl2.shape)

然后只需解压缩以再次获取组件:

lat_unique, long_unique = setl2[:, 0], setl2[:, 1]

答案 1 :(得分:0)

在您的

上使用以下示例
tp = np.array([1, 2, 3, 1, 5, 6, 7, 6, 9, 10])

fp = np.array([3, 2, 13, 3, 15, 16, 17, 16, 19, 20])

combined = np.vstack((tp, fp)).T

x = np.random.rand(combined.shape[1])
y = combined.dot(x)
unique, index = np.unique(y, return_index=True)

combined[index]

tp[index]