我想通过增加X数组的每行订购"deg"的顺序进行排序,每个数据都链接到"Ir"数组上的y值。 X和Y具有相同的维度(11乘10)
当对X矩阵进行排序时,我需要以与X相同的顺序排列y矩阵(因此,Y不会按越来越多的顺序排序)。
简化了问题的看法:
DEG A 2x2矩阵: [[5。 3.]] [6。 2.]]]
IR A 2x2矩阵: [[10。 20.]] [30。 40.]]]
我希望他们成为:
排序的DEG: [[3。 5.]] [2。 6.]]]]
链接排序的IR: [[20。 10.]] [40。 30.]]]
这是我的数据:
Ech1 deg 90 82 49 13 76 90 109 131.5 110 117
Ech1 Ir 0.848484848484849 0.0272727272727273 0.00484848484848485 0 0.00666666666666667 0.848484848484849 0.818181818181818 0.0118181818181818 0.384848484848485 0.0757575757575758
要理解,all_deg是{'ech1':['90','82','49','13','13','76','90','90','109','109','131.5','110 ','117'],'ech2':['90','73','5等
这是创建"deg"和"Ir"矩阵的方式(相同的过程):
numeric_part = 1 # It's important to start at 1 to have 'Ech 1' in the first iteration
matrix_size = len(all_deg) # ALL DEG IS A LIST OF LIST containing all degree data
deg = np.zeros((matrix_size, matrix_size-1), dtype=object)
for i in all_deg:
selected_list_key = f'Ech{numeric_part}' # Define which sample's list we want in the all-list
selected_list = all_deg[selected_list_key] # Retrieve the specific list from the all-list
float_sl = [float(ele) for ele in selected_list] # Convert the data into float
deg[numeric_part - 1, :] = float_sl # Enter the sorted list into the X-th row of the matrix
numeric_part += 1
分析解答
要以增加的顺序对DEG数组进行排序,同时将相应的行完好无损地保持在IRAR阵列中,您可以使用Numpy实现此目标。您可以做到这一点:
import numpy as np
# Your data
deg = np.array([[5., 3.], [6., 2.]])
Ir = np.array([[10., 20.], [30., 40.]])
# Sort deg in increasing order while keeping the corresponding rows in Ir intact
sorted_indices = np.argsort(deg, axis=1)
sorted_deg = np.take_along_axis(deg, sorted_indices, axis=1)
sorted_Ir = np.take_along_axis(Ir, sorted_indices, axis=1)
print("Sorted deg:")
print(sorted_deg)
print("Linked sorted Ir:")
print(sorted_Ir)