gpt4 book ai didi

python - 使用numpy区分两个 "symmetrical"数组

转载 作者:行者123 更新时间:2023-11-28 17:10:56 26 4
gpt4 key购买 nike

我有两个以弧度为单位的角度值数组。这两个阵列关于已知的恒定角是对称的。数组如图所示: Array values in blue and red

示例值如下:

   one = [ 2.98153965 -1.33298928  2.94993567 -1.39909924  2.99214403  3.00138863
3.04390642 -1.59098448 -1.65660299 -1.73146174 -1.8166248 -2.85595599
-2.02035274 -2.64530394 -2.26451127 -2.3982946 -2.52735954 -2.17570346
-2.77544658 -2.88566686 -1.84913768 -3.07261908 -1.66738719 -1.6029932
-1.54596053 -1.50177363 -1.46133745 -1.42288915 -1.38241718 2.79925996
-1.30775884 -1.27309395 2.72153718 -1.20592812 -1.18113435 -1.15029987]

two = [-1.30507254 2.9385436 -1.36415496 2.95897805 -1.43845065 -1.48295087
-1.53346541 3.09685482 -3.11358085 -3.0466034 -2.95794156 -1.9128659
-2.75067133 -2.13826992 -2.51567194 -2.39565127 -2.28148844 -2.65519436
-2.05312249 -1.95523663 -2.98473857 -1.75415233 3.13322155 3.06539723
3.00595703 2.95378704 2.90786779 2.86730208 2.831318 -1.34113191
2.77057495 2.74479777 -1.23620286 2.70046364 2.68129889 2.66380717]

可以看出,这些值是“跟随”两条对称的arctan线,我的问题是我如何区分这两者,并得到这样的东西: Distinguished arrays

我已经尝试了几种方法,但无法想出一种适用于所有情况的通用方法,通常会有一个被误解的部分分配给错误的数组。

欢迎任何想法!谢谢!

最佳答案

这是一个最小化连续点之间的距离以及斜率变化(由参数 lam 加权)的解决方案。仅距离在交叉点处失效。 enter image description here

import numpy as np

one = list(map(float, """ 2.98153965 -1.33298928 2.94993567 -1.39909924 2.99214403 3.00138863
3.04390642 -1.59098448 -1.65660299 -1.73146174 -1.8166248 -2.85595599
-2.02035274 -2.64530394 -2.26451127 -2.3982946 -2.52735954 -2.17570346
-2.77544658 -2.88566686 -1.84913768 -3.07261908 -1.66738719 -1.6029932
-1.54596053 -1.50177363 -1.46133745 -1.42288915 -1.38241718 2.79925996
-1.30775884 -1.27309395 2.72153718 -1.20592812 -1.18113435 -1.15029987""".split()))

two = list(map(float, """-1.30507254 2.9385436 -1.36415496 2.95897805 -1.43845065 -1.48295087
-1.53346541 3.09685482 -3.11358085 -3.0466034 -2.95794156 -1.9128659
-2.75067133 -2.13826992 -2.51567194 -2.39565127 -2.28148844 -2.65519436
-2.05312249 -1.95523663 -2.98473857 -1.75415233 3.13322155 3.06539723
3.00595703 2.95378704 2.90786779 2.86730208 2.831318 -1.34113191
2.77057495 2.74479777 -1.23620286 2.70046364 2.68129889 2.66380717""".split()))

data = np.array([one, two])

dd = (data[[[0, 1], [1, 0]], 1:] - data[:, None, :-1] + np.pi)%(2*np.pi) - np.pi
dde2 = np.einsum('ijk,ijk->jk', dd, dd)

xovr1 = np.argmin(dde2, axis=0)
pick1 = np.r_[0, np.cumsum(xovr1) & 1]

d2d = dd[:, :, None, 1:] - dd[[[1, 0], [0, 1]], :, :-1]
d2de2 = np.r_['2', np.zeros((2, 2, 1)), np.einsum('ijkl,ijkl->jkl', d2d, d2d)]

lam = 0.5
e2 = (dde2[:, None, :] + lam * d2de2).reshape(4, -1)

xovr2 = np.argmin(e2, axis=0)>>1
pick2 = np.r_[0, np.cumsum(xovr2) & 1]

print('by position only')
print(data[pick1, np.arange(data.shape[1])])
print(data[1-pick1, np.arange(data.shape[1])])

print('by position and slope')
print(data[pick2, np.arange(data.shape[1])])
print(data[1-pick2, np.arange(data.shape[1])])


# by position only
# [ 2.98153965 2.9385436 2.94993567 2.95897805 2.99214403 3.00138863
# 3.04390642 3.09685482 -3.11358085 -3.0466034 -2.95794156 -2.85595599
# -2.75067133 -2.64530394 -2.51567194 -2.3982946 -2.52735954 -2.65519436
# -2.77544658 -2.88566686 -2.98473857 -3.07261908 3.13322155 3.06539723
# 3.00595703 2.95378704 2.90786779 2.86730208 2.831318 2.79925996
# 2.77057495 2.74479777 2.72153718 2.70046364 2.68129889 2.66380717]
# [-1.30507254 -1.33298928 -1.36415496 -1.39909924 -1.43845065 -1.48295087
# -1.53346541 -1.59098448 -1.65660299 -1.73146174 -1.8166248 -1.9128659
# -2.02035274 -2.13826992 -2.26451127 -2.39565127 -2.28148844 -2.17570346
# -2.05312249 -1.95523663 -1.84913768 -1.75415233 -1.66738719 -1.6029932
# -1.54596053 -1.50177363 -1.46133745 -1.42288915 -1.38241718 -1.34113191
# -1.30775884 -1.27309395 -1.23620286 -1.20592812 -1.18113435 -1.15029987]
# by position and slope
# [ 2.98153965 2.9385436 2.94993567 2.95897805 2.99214403 3.00138863
# 3.04390642 3.09685482 -3.11358085 -3.0466034 -2.95794156 -2.85595599
# -2.75067133 -2.64530394 -2.51567194 -2.39565127 -2.28148844 -2.17570346
# -2.05312249 -1.95523663 -1.84913768 -1.75415233 -1.66738719 -1.6029932
# -1.54596053 -1.50177363 -1.46133745 -1.42288915 -1.38241718 -1.34113191
# -1.30775884 -1.27309395 -1.23620286 -1.20592812 -1.18113435 -1.15029987]
# [-1.30507254 -1.33298928 -1.36415496 -1.39909924 -1.43845065 -1.48295087
# -1.53346541 -1.59098448 -1.65660299 -1.73146174 -1.8166248 -1.9128659
# -2.02035274 -2.13826992 -2.26451127 -2.3982946 -2.52735954 -2.65519436
# -2.77544658 -2.88566686 -2.98473857 -3.07261908 3.13322155 3.06539723
# 3.00595703 2.95378704 2.90786779 2.86730208 2.831318 2.79925996
# 2.77057495 2.74479777 2.72153718 2.70046364 2.68129889 2.66380717]

关于python - 使用numpy区分两个 "symmetrical"数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47514987/

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