How to get evenly distributed selection from Perl array values?

I have an array containing many values ​​from 0 to 360 (for example, degrees in a circle), but unevenly distributed:

1,45,46,47,48,49,50,51,52,53,54,55,100,120,140,188, 210, 280, 355

Now I need to reduce these values, for example. 4, but as evenly distributed values.

How to do it?

Thanks, Jan

+3
source share
2 answers

Put the numbers on the circle like a clock. Now create a logical cross, say, at 12, 3, 6, and 9 oclock. Put 12 on the first number. Now find which numbers will be closest to oclock 3, 6, and 9, and write down the sum of these three numbers next to the first number.

, - 12 - , . , , 12- .

, 12 , 3 oclock, . , , .

R N , . "" - R/N , , , . , 6 , 6- , 60 , 90 . , ​​ . : R N.

Perl, Ive - .:)

+3

, . . $datafile :

1   1
45  45
46  46
...
210 210
280 280
355 355

- , - . $K = 4:

use strict; use warnings;
use Algorithm::KMeans;

my $datafile = $ARGV[0] or die;
my $K        = $ARGV[1] or 0;
my $mask     = 'N1';

my $clusterer = Algorithm::KMeans->new(
    datafile => $datafile,
    mask     => $mask,
    K        => $K,
    terminal_output => 0,
);

$clusterer->read_data_from_file();

my ($clusters, $cluster_centers) = $clusterer->kmeans();

my %clusters;

while (@$clusters) {

    my $cluster = shift @$clusters;
    my $center  = shift @$cluster_centers;

    $clusters{"@$center"} = $cluster->[int rand( @$cluster - 1)];
}

use YAML; print Dump \%clusters;

:

120: 120
199: 188
317.5: 355
45.9166666666667: 46

- , - . .

+1

Source: https://habr.com/ru/post/1776357/


All Articles