Joh*_*nGH 3 sorting perl machine-learning
我想找到一种有效的方法(最好是在Perl中)通过比较它们在组的多个子集中的顺序来学习单词族的固定顺序.(它们是工作参数.大约有30种不同的工作参数.不同的工作需要不同的参数组合,每个工作中只有一些参数)
例如,给定:
first
second
third
sixth
seventh
tenth
first
third
fourth
fifth
sixth
third
fifth
seventh
eighth
ninth
tenth
Run Code Online (Sandbox Code Playgroud)
它应该能够记住它看到的相对顺序关系,以确定订单是:
first
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth
Run Code Online (Sandbox Code Playgroud)
我已生成如下列表:
first.second.third.sixth.seventh.tenth
first.third.fourth.fifth.sixth
third.fifth.seventh.eighth.ninth.tenth
Run Code Online (Sandbox Code Playgroud)
然后按字母顺序排序+视觉比较它们,但我有30个参数的数百种不同的组合,所以将它们全部排序并手动将它们放在一起将是一项大工作.
我认为@ daniel-tran已经回答了/sf/answers/3362936041/中的"如何" 并使用了这个和一些hackery:
$order->{$prev}->{$this} = 1;
$order->{$this}->{$prev} = 0;
Run Code Online (Sandbox Code Playgroud)
我设法为每对连续参数填充哈希值为1或0的哈希值,以说明哪个是第一个,如:
$VAR1 = {
    'first' => {
        'second' => 1,
        'third' => 1,
    },
    'second' => {
        'first' => 0,
        'third' => 1,
    },
    'third' => {
        'first' => 0,
        'second' => 0,
        'fourth' => 1,
        'fifth'  => 1,
        'sixth'  => 1,
    },            
    'fourth' => {
        'third' => 0,
        'fifth' => 1,
    },
    ...
Run Code Online (Sandbox Code Playgroud)
但当我被要求排序一对从未被视为直接邻居的对时,我试图找出在我的排序函数中要做什么,因此没有定义关系.
有一个简单的解决方案吗?我是以正确的方式来做这件事的吗?首先是否有更好的WTDI?
谢谢,
约翰
您链接的问题包括使用图表和拓扑排序的另一个答案.该Graph模块非常易于使用:
use warnings;
use strict;
use Graph;
my $graph = Graph->new(directed => 1);
my $prev;
while (<DATA>) {
    chomp;
    $graph->add_edge($prev, $_) if length && length $prev;
    $prev = $_;
}
print $_,"\n" for $graph->topological_sort;
__DATA__
first
second
third
sixth
seventh
tenth
first
third
fourth
fifth
sixth
third
fifth
seventh
eighth
ninth
tenth
Run Code Online (Sandbox Code Playgroud)
输出:
first
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth
Run Code Online (Sandbox Code Playgroud)