我尝试了以下方法.
>创建一组引用2d数组并对每个数组中的行进行排序,以最小化到参考2d数组的平均欧氏距离.这恐怕会产生偏颇的结果.
>按顺序对数组中的行进行排序,然后对成对的中间数,然后是成对的等等……这实际上不起作用,我不知道为什么.
第三种方法可能只是强力优化,但我试图避免这种情况,因为我有多组数组来执行该过程.
这是我的第二种方法(Python)的代码:
def reorder_to(A,B): """Reorder rows in A to best match rows in B. input ----- A : N x M numpy.array B : N x M numpy.array Output ------ perm_order : permutation order """ if A.shape != B.shape: print "A and B must have the same shape" return None N = A.shape[0] # Create a distance matrix of distance between rows in A and B distance_matrix = np.ones((N,N))*np.inf for i,a in enumerate(A): for ii,b in enumerate(B): ba = (b-a) distance_matrix[i,ii] = np.sqrt(np.dot(ba,ba)) # Choose permutation order by smallest distances first perm_order = [[] for _ in range(N)] for _ in range(N): ind = np.argmin(distance_matrix) i,ii = ind/N,ind%N perm_order[ii] = i distance_matrix[i,:] = np.inf distance_matrix[:,ii] = np.inf return perm_orderdef permute_tensor_rows(A): """Permute 1d rows in 3d array along the 0th axis to minimize average SD along 2nd axis. input ----- A : numpy.3darray Each "slice" in the 2nd direction is an independent array whose rows can be permuted to decrease the average SD in the 2nd direction. Output ------ A : numpy.3darray A with sorted rows in each "slice". """ step = 2 while step <= A.shape[2]: for k in range(0,A.shape[2],step): # If last,reorder to prevIoUs if k + step > A.shape[2]: A_kk = A[:,:,k:(k+step)] kk_order = reorder_to(np.median(A_kk,axis=2),np.median(A_k,axis=2)) A[:,k:(k+step)] = A[kk_order,k:(k+step)] continue k_0,k_1 = k,k+step/2 kk_0,kk_1 = k+step/2,k+step A_k = A[:,k_0:k_1] A_kk = A[:,kk_0:kk_1] order = reorder_to(np.median(A_k,np.median(A_kk,axis=2)) A[:,k_0:k_1] = A[order,k_0:k_1] print "Step:",step,"\t ... Average SD:",np.mean(np.std(A,axis=2)) step *= 2 return A解决方法 对不起,我应该查看你的代码示例;这是非常翔实的.
这里看起来似乎为您的问题提供了开箱即用的解决方案:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html#scipy.optimize.linear_sum_assignment
根据我的经验,只有最多100分才真正可行.
总结以上是内存溢出为你收集整理的python – 在3d数组的“切片”中置换行以相互匹配全部内容,希望文章能够帮你解决python – 在3d数组的“切片”中置换行以相互匹配所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)