我有一个数据是这样的:

1 1:-0.394668 2:-0.794872 3:-1 4:-0.871341 5:0.9365 6:0.75597
1 1:-0.463641 2:-0.897436 3:-1 4:-0.871341 5:0.44378 6:0.121824
1 1:-0.469432 2:-0.897436 3:-1 4:-0.871341 5:0.32668 6:0.302529
-1 1:-0.241547 2:-0.538462 3:-1 4:-0.871341 5:0.9994 6:0.987166
1 1:-0.757233 2:-0.948718 3:-1 4:-0.871341 5:-0.33904 6:0.915401
1 1:-0.167147 2:-0.589744 3:-1 4:-0.871341 5:0.95078 6:0.991566

第一列是类,接下来的6列是特性,我试图找到所有可能的特性组合(2个特性,3个特性,…5个特征)。
例如。:
feat1 - feat2
feat1 - feat3
...
feat5 - feat6
...
feat1 - feat2 -feat3 -feat4 -feat 5
feat1 - feat2 -feat3 -feat4 -feat 6
..etc..

其中一个文件包含:
1 1:-0.394668 2:-0.794872
1 1:-0.463641 2:-0.897436
1 1:-0.469432 2:-0.897436
-1 1:-0.241547 2:-0.538462
1 1:-0.757233 2:-0.948718
1 1:-0.167147 2:-0.589744

在Perl中是否存在现有的实现?

最佳答案

当然,这里有Algorithm::Combinatorics和/或Set::CrossProduct但是很难从你的问题描述中找出哪个更合适。

Maybe you can use something like this as a starting point:

 #!/usr/bin/perl

use strict; use warnings;
use Algorithm::Combinatorics qw( combinations );

while ( my $line = <DATA> ) {
    last unless $line =~ /\S/;
    my $row = [ $line =~  /([1-6]:\S+)/g ];
    for my $i (2 .. 6) {
        my $it = combinations($row, $i);
        while ( my $x = $it->next ) {
            print "@$x\n";
        }
    }
}

__DATA__
1 1:-0.394668 2:-0.794872 3:-1 4:-0.871341 5:0.9365 6:0.75597
1 1:-0.463641 2:-0.897436 3:-1 4:-0.871341 5:0.44378 6:0.121824
1 1:-0.469432 2:-0.897436 3:-1 4:-0.871341 5:0.32668 6:0.302529
-1 1:-0.241547 2:-0.538462 3:-1 4:-0.871341 5:0.9994 6:0.987166
1 1:-0.757233 2:-0.948718 3:-1 4:-0.871341 5:-0.33904 6:0.915401
1 1:-0.167147 2:-0.589744 3:-1 4:-0.871341 5:0.95078 6:0.991566

C:\温度>C
1:-0.167147 2:-0.589744 3:-1
1:-0.167147 2:-0.589744 4:-0.871341
1:-0.167147 2:-0.589744 5:0.95078

2:-0.589744 3:-15:0.95078 6:0.991566
2:-0.589744 4:-0.871341 5:0.95078 6:0.991566
3:-14:-0.871341 5:0.95078 6:0.991566
1:-0.167147 2:-0.589744 3:-14:-0.871341 5:0.95078
1:-0.167147 2:-0.589744 3:-14:-0.871341 6:0.991566
1:-0.167147 2:-0.589744 3:-15:0.95078 6:0.991566
1:-0.167147 2:-0.589744 4:-0.871341 5:0.95078 6:0.991566
1:-0.167147 3:-14:-0.871341 5:0.95078 6:0.991566
2:-0.589744 3:-14:-0.871341 5:0.95078 6:0.991566
1:-0.167147 2:-0.589744 3:-14:-0.871341 5:0.95078 6:0.991566

08-27 08:45