Git Product home page Git Product logo

simpler-stat's Introduction

NAME

SimpleR::Stat

Simple Stat on arrayref, like sum, mean, calc rate, etc

简单数据统计处理

DESC

传入 scalar num / arrayref,计算 sum 求和, mean 均值, rate 比例 等

实例参考xt子文件夹

FUNCTION

calc_rate_arrayref

my @data = (3, 4, 1);
my $r = calc_rate_arrayref(\@data);
dump($r);
#$r:[0.375, 0.5, 0.125]

transpose_arrayref($arr)

数组行列转置

my $s= [ 1, 2 , 3 ], [ 4, 5 , 6 ] ];
my $d = transpose_arrayref($s);
# $d : [[1 ,4], [2, 5], [3 ,6 ]] ;

sort_by_other_arrayref($arr, $other_arr, $map_sub, $sort_sub)

根据指定的变换函数、排序函数处理other_arr,再将other_arr重排次序复制到arr进行重排

my $label = [ qw/x y z/ ];
my $data = [ [qw/3 2 1 4/],  [ qw/2 3 5 6/ ], [qw/4 5 1 6/] ];

my ($new_label, $new_data) = sort_by_other_arrayref(
    $label, $data, 
     sub { # data line
        my ($r) = @_; 
        my ($g, $n, $b, $f) = @$r;
        my $all = $g+$n+$b+$f;
        return [ $f/$all, $b/$all ] }, 
     sub {
        my ($x, $y) = @_;
        ($x->[0] <=> $y->[0]) or ($x->[1] <=> $y->[1]) },
);

dump($new_label, $new_data);
#$new_label ["z", "y", "x"]
#$new_data  [[4, 5, 1, 6], [2, 3, 5, 6], [3, 2, 1, 4]]

map_arrayref($arr, $calc_sub, %option)

取出数组中某些项,做指定运算,返回结果

参数:

return_arrayref : 以arraryref格式返回结果,默认是以array格式返回

keep_source : 返回的结果中保留原始数据

calc_col : 指定数组中的项

my $data=[ 'haha', 'xx', 4 , 2, 3, 1, 'heihei'];
my @res = map_arrayref(
    $data, 
    \&calc_rate_arrayref,

    calc_col => [ 2 .. 5 ], 

    return_arrayref => 1, 
    keep_source => 1, 
);
# [ "haha", "xx", 4, 2, 3, 1, "heihei", 0.4, 0.2, 0.3, 0.1 ] 
#keep_source => 1, return_arrayref=> 0 : ( "haha", "xx", 4, 2, 3, 1, "heihei", 0.4, 0.2, 0.3, 0.1 )
#keep_source => 0, return_arrayref=> 1 : [ 0.4, 0.2, 0.3, 0.1 ] 

calc_percent_arrayref($arr, $format);

my $format = "%.2f%%";
my @data = (3, 4, 1);
my $r = calc_percent_arrayref(\@data, $format);
dump($r);
#$r : ["37.50%", "50.00%", "12.50%"]

sum_arrayref

my $d = [ 1, 4, 3 ];
my $r = sum_arrayref($d);
# $r = 8

mean_arrayref

my $d = [ 1, 4, 3 ];
my $r = mean_arrayref($d);
# $r = 2.66666667

median_arrayref

my $d = [ 1, 4, 3 ];
my $r = median_arrayref($d);
# $r = 3

uniq_arrayref

my $d = [ 1, 1, 4, 4, 3 ];
my $r = uniq_arrayref($d, 
        # remember_key_order => 0, 
);
# $r = [ 1, 3, 4 ] 

uniq_arrayref_cnt

my $d = [ 1, 1, 4, 4, 3 ];
my $r = uniq_arrayref_cnt($d);
# $r = 3

OTHER FUNCTION

conv_arrayref_to_hash

将arrayref转换为hash,注意重复的key会被覆盖掉,数据可能变少

my $data=[ ['a','b',3],['e','f',6], ['a','f',9]];
my $r = conv_arrayref_to_hash($data, [ 0, 1 ], 2, 
        # remember_key_order => 0, 
);
# $r =  { a => { b => 3, f => 9 }, e => { f => 6 } },

BASE FUNCTION

calc_rate

my $r = calc_rate(3, 4);
# $r = 0.75

format_percent

my $r = format_percent(0.675, "%.2f%%");
# $r = '67.50%'

calc_compare_rate

计算增量

my $r = calc_compare_rate(4, 7);
# $r = 0.75
my ($r2, $diff) = calc_compare_rate(4, 7);
# $r2 = 0.75, $diff = 3

simpler-stat's People

Contributors

abbypan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.