如何在python中删除两个numpy数组的重复元素 [英] How to remove duplicate elements for two numpy arrays in python
本文介绍了如何在python中删除两个numpy数组的重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
eg
u = np.array([1.0, 2.0,2.0,3.0,4.0])
v = np.array([10.0,21.0,18.0,30.0,40.0])
a = np.array([100.0,210.0,220.0,300.0,400.0] )
如果u中的两个元素相同,则删除v值较高的元素。
对于上面的例子,结果应该是
u_new = np.array([1.0,2.0,3.0, 4.0])
v_new = np.array([10.0,18.0,30.0,40.0])
a_new = np.array([100.0,220.0,300.0,400.0])
def remove_duplicates(u,v,a):
u_new,indices = np.unique(u,return_index = True)
v_new = np.zeros(len(u_new),dtype = np.float64)
a_new = np.zeros(len(u_new),dtype = np.float64)
for i in range(len(indices)):
j1 = indices [i]
如果i < len(indices) - 1:
j2 = indices [i + 1]
else:
j2 = j1 + 1
v_new [i] = np.amin(v [j1 :j2])
k = np.argmin(v [j1:j2])+ j1
a_new [i] = a [k]
return u_new,v_new,a_new
上面的代码在处理浮动数时有一个问题,因为两个浮点数之间没有精确的相等。所以我必须把它改成一个非常愚蠢的方式
def remove_duplicates(u,v,a):
u_new = u
v_new = v
a_new = a
cnt = 0
for i in range(len(u)):
如果cnt <1:
u_new [cnt] = u [i]
v_new [cnt] = v [i]
a_new [cnt] = a [i]
cnt + = 1
else:
如果abs(u [i] -u_new [cnt-1])> 1e-5:
u_new [cnt] = u [i]
v_new [cnt] = v [i]
a_new [cnt] = a [i]
cnt + 1
其他:
print(两点相同x coord found.ignore,i)
如果v_new [cnt-1]> v [i]:
v_new [cnt-1] = v [i]
a_new [cnt-1] = a [i]
return u_new [:cnt] v_new [:cnt],a_new [:cnt]
如何以Pythonic方式进行编程?
谢谢。
解决方案
Kinda cheeky改变你的问题,但这里。 b
def remove_duplicates(u,v,a,d = 1e-5):
s = np.argsort(u)
ud = abs(u [s] [1:] - u [s] [: - 1])< d
vd = v [s] [1:]< v [s] [: - 1]
drop = np.union1d(s [: - 1] [ud& vd],s [1:] [ud&〜vd])
返回np.delete(u,drop),np.delete (v,drop),np.delete(a,drop)
应该使用阈值清理你的浮标。
I have two array naming u,v, e.g.
u=np.array([1.0,2.0,2.0,3.0,4.0])
v=np.array([10.0,21.0,18.0,30.0,40.0])
a=np.array([100.0,210.0,220.0,300.0,400.0])
If two elements in u are same, then delete that one which is higher in v value. For the above example,the result should be
u_new=np.array([1.0,2.0,3.0,4.0])
v_new=np.array([10.0,18.0,30.0,40.0])
a_new=np.array([100.0,220.0,300.0,400.0])
def remove_duplicates(u,v,a):
u_new, indices = np.unique(u, return_index=True)
v_new = np.zeros(len(u_new), dtype=np.float64)
a_new = np.zeros(len(u_new), dtype=np.float64)
for i in range(len(indices)):
j1 = indices[i]
if i < len(indices) - 1:
j2 = indices[i + 1]
else:
j2 = j1 + 1
v_new[i] = np.amin(v[j1:j2])
k = np.argmin(v[j1:j2]) + j1
a_new[i] = a[k]
return u_new,v_new,a_new
The above code has a problem when treat floating number because there is not exact equality between two floating number. So I have to change it to a very 'stupid' way
def remove_duplicates(u, v, a):
u_new=u
v_new=v
a_new=a
cnt = 0
for i in range(len(u)):
if cnt <1:
u_new[cnt] = u[i]
v_new[cnt] = v[i]
a_new[cnt] = a[i]
cnt += 1
else:
if abs(u[i]-u_new[cnt-1]) > 1e-5:
u_new[cnt] = u[i]
v_new[cnt] = v[i]
a_new[cnt] = a[i]
cnt += 1
else:
print("Two points with same x coord found.ignore", i)
if v_new[cnt-1] > v[i]:
v_new[cnt-1] = v[i]
a_new[cnt-1] = a[i]
return u_new[:cnt], v_new[:cnt], a_new[:cnt]
How can I program it in a Pythonic way? Thank you.
解决方案
Kinda cheeky to change your question, but here goes.
def remove_duplicates(u,v,a,d=1e-5):
s=np.argsort(u)
ud=abs(u[s][1:]-u[s][:-1])<d
vd=v[s][1:]<v[s][:-1]
drop=np.union1d(s[:-1][ud&vd],s[1:][ud&~vd])
return np.delete(u,drop),np.delete(v,drop),np.delete(a,drop)
That should work with a threshhold value to clean up your floats.
这篇关于如何在python中删除两个numpy数组的重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文