我有这些数组:
A=np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012]) # points on x-axis
B=np.array([53.090028, 54.829213, 54.573222, 47.244701, 52.033966, 48.613694, 53.425587]) # points on y-axis
我创建了(x,y)坐标数组:
coord = np.array([A, B])
我有另一个坐标数组:
C=np.array([160.514, 161.67894, 161.68438, 160.59858, 161.55013, 161.61683, 161.55903, 161.67383, 161.70316, 161.63421 ])
D=np.array([53.068106, 54.552493,53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067, 54.65673, 54.599929])
和一列距离:
z=np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])
现在我想创建一个压缩的坐标数组,所以:
coord1=np.array([C,D])
目标如下:搜索coord中的点位于coord1中,然后从z中提取相应的距离。这是我的代码:
delta = 0.09
for i in range(coord.shape[1]):
for j in range(coord1.shape[1]):
if (np.all(coord[:,i] >= coord1[:,j]-delta)) and (np.all(coord[:,i] <= coord1[:,j]+delta)):
print coord[:,i], i, coord1[:,j], z[j], j
输出如下:
[ 160.592625 53.090028] 0 [ 160.514 53.068106] 0.000164209 0
[ 161.61683 54.829213] 1 [ 161.55013 54.852462] 0.303595 4
[ 161.61683 54.829213] 1 [ 161.61683 54.829213] 0.144146 5
[ 161.61683 54.829213] 1 [ 161.55903 54.916358] 0.142063 6
[ 161.61683 54.829213] 1 [ 161.67383 54.801067] 0.903051 7
[ 161.672708 54.573222] 2 [ 161.67894 54.552493] 0.0452326 1
[ 161.672708 54.573222] 2 [ 161.70316 54.65673] 0.144751 8
[ 161.672708 54.573222] 2 [ 161.63421 54.599929] 0.101169 9
如您所见,我有多个对应于1和2.对于这些元素,我想只保留最小的z元素。例如:在1中,我只想打印
[ 161.61683 54.829213] 1 [ 161.55013 54.852462] 0.303595 4
仅限2s
[ 161.672708 54.573222] 2 [ 161.67894 54.552493] 0.0452326 1
我不知道......提前致谢
答案 0 :(得分:0)
我没有太多时间来优化这个,但我希望这对你的数据量很有效,因为它不是很大 -
您可以直接运行它并检查finalResult
是否能满足您的预期。试着理解算法,但重要的是它。
import numpy as np
A = np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012]) # points on x-axis
B = np.array([53.090028, 54.829213, 54.573222, 47.244701, 52.033966, 48.613694, 53.425587]) # points on y-axis
coord = np.array([A, B])
C = np.array(
[160.514, 161.67894, 161.68438, 160.59858, 161.55013, 161.61683, 161.55903, 161.67383, 161.70316, 161.63421])
D = np.array(
[53.068106, 54.552493, 53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067, 54.65673, 54.599929])
z = np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])
coord1 = np.array([C, D])
delta = 0.09
# create a empty list and append whatever youre printing right now into it
mylst = list()
for i in range(coord.shape[1]):
for j in range(coord1.shape[1]):
if (np.all(coord[:, i] >= coord1[:, j] - delta)) and (np.all(coord[:, i] <= coord1[:, j] + delta)):
temp = str(coord[:, i]) + " , " + str(i) + " , " + str(coord1[:, j]) + " , " + str(z[j]) + " , " + str(j)
mylst.append(temp)
#see your list with all data
for each in mylst:
print(each)
print("--------")
#create a new list for final output where you want records with min(Z)
finalResult = list()
for each in mylst:
alpha = each
# check if this element is already in final result
alreadyDone = False
for eachFR in finalResult:
if str(alpha).split(",")[1] == str(eachFR).split(",")[1]:
alreadyDone = True
break
if alreadyDone:
continue
# if not, look for minimum value of Z
for ee in mylst:
if str(alpha).split(",")[1] == str(ee).split(",")[1]:
if str(alpha).split(",")[4] > str(ee).split(",")[4]:
alpha = ee
finalResult.append(alpha)
for each in finalResult:
print(each)
答案 1 :(得分:0)
不幸的是,numpy专家还没有看到这个,所以你必须忍受我的尝试。但没有循环,只有numpy矢量化函数。
import numpy as np
A=np.array([160.592625, 161.616830, 161.672708, 163.544365, 163.745786, 164.260333, 164.277012])
B=np.array([53.090028, 54.829213, 54.573222, 47.244701, 52.033966, 48.613694, 53.425587])
C=np.array([160.514, 161.67894, 161.68438, 160.59858, 161.55013, 161.61683, 161.55903, 161.67383, 161.70316, 161.63421 ])
D=np.array([53.068106, 54.552493,53.171848, 54.907098, 54.852462, 54.829213, 54.916358, 54.801067, 54.65673, 54.599929])
z=np.array([0.0452326, 0.903051, 0.126823, 0.101169, 0.000164209, 0.127296, 0.303595, 0.144146, 0.142063, 0.144751])
delta = 0.09
#calculate all differences between A and C
diff_AC = np.abs(A[:,None] - C[None,:])
#calculate all differences between B and D
diff_BD = np.abs(B[:,None] - D[None,:])
#find pairs, where both coordinates differ by less than delta
diff_coord = np.argwhere(np.logical_and(diff_AC < delta, diff_BD < delta))
#sort according to AB index and, if more than one exists, by the z value derived from CD index
diff_coord_sorted = diff_coord[np.lexsort((z[diff_coord[:,1]], diff_coord[:,0]))]
#find the first occurrence of each value in AB
_unik, unikindices = np.unique(diff_coord_sorted[:,0], return_index = True)
#get index for AB and CD
pairs_AB_CD = diff_coord_sorted[unikindices]
输出:
>>>[[0 0]
[1 4]
[2 9]]
每对代表一对A / B和C / D坐标的索引,它们的距离低于delta和最小的z值。