起点:两个一个热编码数组
import numpy as np
import pandas as pd
arr1 = np.array([[1, 1, 0],
[0, 1, 0],
[0, 1, 1]])
arr2 = np.array([[0, 1, 0],
[0, 0, 1],
[1, 0, 1]])
匹配arr1和arr2:4/9
Aim:以多数数字匹配的方式切换arr2中的列,然后给出索引。
切换列0和1:
arr2b = np.array([[1, 0, 0],
[0, 0, 1],
[0, 1, 1]])
匹配arr1和arr2b:6/9
在arr2上使用所选列索引时的输出,例如借助数据帧:
col_idx = [1, 0, 2] # (column 0 and 1 are switched here)
pd.DataFrame(data=arr2).iloc[:,col_idx]
但这可能不是最好的结果。如何通过代码找到arr2的最佳列索引
切换列索引的所有排列如下所示:
是:[(0,1,2)、(0,2,1)、(1,0,2)、(1,2,0)、(2,0,1)、(2,1,0)]
我们循环所有这些排列,并检查命中了多少个数字:
is:[4,4,6,8,2,4]
is:3
is:[1,2,0]
是:
或在df中:
检查:
是:
因此,8x
True
是正确的,最大化列索引开关是正确的相关问题 更多 >
编程相关推荐