gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:14 34 4
gpt4 key购买 nike

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

     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 ])

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

当我使用

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

顺序不对了,我的点也散了。

当我使用

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

错误是

unhashable type: 'numpy.ndarray'

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

最佳答案

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]

关于python - 使用 np.unique 从 2 个 numpy 数组中删除成对重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49254850/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com