3

此问题不仅适用于 MATLAB 用户 - 如果您知道PSEUDOCODE中问题的答案,请随时留下您的答案!


我有两个表 Ta 和 Tb 有不同的行数和不同的列数。内容都是单元格文本,但将来可能还会包含单元格编号。

我想根据以下规则将这些表的内容合并在一起:

  • Ta(i,j)if的Tb(i*,j*)值为空,反之亦然。
  • 如果两者都可用,则取 的值Ta(i,j)(并且可选地,检查它们是否相同)。

然而,棘手的部分是我们没有唯一的行键,我们只有唯一的列键。i*请注意,我在和之间进行了区分i。原因是 Ta 中的行可以位于与 Tb 不同的索引处,对于列j*和 也是如此j。影响是:

  • 我们首先需要确定 Ta 的哪一行对应于 Tb 的行,反之亦然。我们可以通过尝试交叉匹配表共有的任何列来做到这一点。但是,我们可能找不到匹配项(在这种情况下,我们不会将一行与另一行合并)。

问题

我们如何才能以最有效的方式将这两个表的内容合并在一起?


这里有一些资源可以更详细地解释这个问题:

1. Matlab 示例:

Ta = cell2table({...
     'a1', 'b1', 'c1'; ...
     'a2', 'b2', 'c2'}, ...
      'VariableNames', {'A','B', 'C'})
Tb = cell2table({...
     'b2*', 'c2', 'd2'; ...
     'b3', 'c3', 'd3'; ...
     'b4', 'c4', 'd4'}, ...
      'VariableNames', {'B','C', 'D'})

结果表 Tc 应该是这样的:

Tc = cell2table({...
    'a1' 'b1' 'c1'   ''; ...
    'a2' 'b2' 'c2' 'd2'; ...
    ''   'b3' 'c3' 'd3'; ...
    ''   'b4' 'c4' 'd4'}, ...
     'VariableNames', {'A', 'B','C', 'D'})

2. 可能的第一步

我尝试了以下方法:

Tc = outerjoin(Ta, Tb, 'MergeKeys', true)

哪个工作顺利,但问题是它缺少看起来相似的行的堆叠。例如上面的命令产生:

 A        B       C       D  
____    _____    ____    ____
''      'b2*'    'c2'    'd2'
''      'b3'     'c3'    'd3'
''      'b4'     'c4'    'd4'
'a1'    'b1'     'c1'    ''  
'a2'    'b2'     'c2'    '' 

这里的行

''      'b2*'    'c2'    'd2'
'a2'    'b2'     'c2'    '' 

应该合并为一个:

'a2'    'b2'     'c2'    'd2' 

所以我们需要多一步将这两个堆叠在一起?


3. 障碍示例

如果我们有类似的东西:

Ta = 
     A        B       C       
    ____    _____    ____
    'a1'    'b1'     'c1' 
    'a2'    'b2'     'c2'

Tb = 
     A        B       C       
    ____    _____    ____
    'a1'    'b2'     'c3' 

那么问题就出现了 b 中的行是否应该与 a 的第 1 行或第 2 行合并,还是应该将所有行合并或只是作为单独的行?关于如何处理这些类型的情况的想法也很好。

4

2 回答 2

3

这是一个尝试完成这项工作的函数。您输入两个表,一个阈值用于决定是否合并两行,一个逻辑用于说明在出现合并冲突时是否更喜欢从第一个表中获取值。我没有为极端情况做准备,但看看它会给你带来什么:

TkeepAll=mergeTables(Tb,Ta,1,true)
TmergeSome=mergeTables(Tb,Ta,0.25,true)
TmergeAll=mergeTables(Tb,Ta,-1,true)

这是功能:

function Tmerged=mergeTables(Ta,Tb,threshold,preferA)
%% parameters
% Ta and Tb are two the two tables to merge
% threshold=0.25; minimal ratio of identical values in rows for merge.
%   example: you have one row in table A with 3 values, but you only have two
%   values for the same columns in data B. if one of the values is identical
%   and one isn't, you have ratio of 1/2 aka 0.5, which passes a threshold of
%   0.25
% preferA=true; which to take when there is merge conflict
%% see how well rows fit to each other
% T1 is the table with fewer rows
if size(Ta,1)<=size(Tb,1)
    T1=Ta;
    T2=Tb;
    prefer1=preferA;
else
    T1=Tb;
    T2=Ta;
    prefer1=~preferA;
end
[commonVar1,commonVar2]=ismember(T1.Properties.VariableNames,...
    T2.Properties.VariableNames);
commonVar1=find(commonVar1);
commonVar2(commonVar2==0)=[];
% fit is a table with the size of N rows T1 by M rows T2, with values
% describing what ratio of identical items between each row in
% table 1 (shorter) and each row in table 2 (longer), among all not-missing
% points
for ii=1:size(T1,1) %rows of T1
    for jj=1:size(T2,1)
        fit(ii,jj)=sum(ismember(T1{ii,commonVar1},T2{jj,commonVar2}))/length(commonVar1);
    end
end
%% pair rows according to fit
% match has two columns, first one has T1 row number and secone one has the
% matching T2 row number
unpaired1=true(size(T1,1),1);
unpaired2=true(size(T2,1),1);
count=0;
match=[];
maxv=max(fit,[],2);
[~,order]=sort(maxv,'descend');
order=order';
for ii=order %1:size(T1,1)
    [maxv,maxi]=max(fit,[],2);
    if maxv(ii)>threshold
        count=count+1;
        match(count,1)=ii;
        match(count,2)=maxi(ii);
        unpaired1(ii)=false;
        unpaired2(match(count,2))=false;
        fit(:,match(count,2))=nan; %exclude paired row from next pairing
    end
end

%% prepare new variables
% first variables common to the two tables
Nrows=sum(unpaired1)+sum(unpaired2)+size(match,1);
namesCommon={};
namesCommon(1:length(commonVar1))={T1.Properties.VariableNames{commonVar1}};
for vari=1:length(commonVar1)
    if isempty(match)
        mergedData={};
    else
        if prefer1
            mergedData=T1{match(:,1),commonVar1(vari)}; %#ok<*NASGU>
        else
            mergedData=T2{match(:,2),commonVar2(vari)};
        end
    end
    data1=T1{unpaired1,commonVar1(vari)};
    data2=T2{unpaired2,commonVar2(vari)};
    eval([namesCommon{vari},'=[data1;mergedData;data2];']);
end
% variables only in 1
uncommonVar1=1:size(T1,2);
uncommonVar1(commonVar1)=[];
names1={};
names1(1:length(uncommonVar1))={T1.Properties.VariableNames{uncommonVar1}};
for vari=1:length(uncommonVar1)
    data1=T1{:,uncommonVar1(vari)};
    tmp=repmat({''},Nrows-size(data1,1),1);
    eval([names1{vari},'=[data1;tmp];']);
end
% variables only in 2
uncommonVar2=1:size(T2,2);
uncommonVar2(commonVar2)=[];
names2={};
names2(1:length(uncommonVar2))={T2.Properties.VariableNames{uncommonVar2}};
for vari=1:length(uncommonVar2)
    data2=T2{:,uncommonVar2(vari)};
    tmp=repmat({''},Nrows-size(data2,1),1);
    eval([names2{vari},'=[tmp;data2];']);
end
%% collect variables to a table
names=sort([namesCommon,names1,names2]);
str='table(';
for vari=1:length(names)
    str=[str,names{vari},','];
end
str=[str(1:end-1),');'];
Tmerged=eval(str);
于 2017-10-23T17:53:45.423 回答
3

这是一个概念性的答案,可以帮助您上路:

  1. 定义一个“评分函数”,告诉您每行 Tb 与 Ta 中的行匹配程度如何。
  2. 用 Ta 填充 Tc
  3. 对于 Ta 中的每一行,确定与 Tb 的最佳匹配。如果匹配质量高于您的标准,请将最佳匹配定义为成功匹配。
  4. 如果找到了成功的匹配,则“使用”它(使用来自 Tb 的信息来丰富 Tc 中需要的相应行)
  5. 继续前进,直到到达 Ta 的末尾,Tb 中未消耗的任何内容现在都可以“附加”到 Tc。

改进空间:

比赛选择注意事项

玩弄消耗 Ta 而不是 Tb,或者使用更复杂的启发式方法来确定消耗顺序(例如,计算所有“距离”并根据成本函数优化匹配)。

请注意,只有在基本解决方案中的匹配项出现大量误报时,这些改进才是必要的。

关于匹配质量定义的说明

我建议您从非常简单的开始,例如,如果您有 4 个字段,只需计算有多少字段匹配,或者是否所有非空字段都匹配。

如果您想更进一步,请考虑评估这些值之间的距离(例如 mse)或文本之间的距离(例如,levensteihn 距离)。

于 2017-10-19T14:01:35.410 回答