Rand Stats

Data::Transformers

zef:antononcube

Data::Transformers

Raku package with algorithms for transforming and smoothing data. Provides the subs:


Installation

From Zef ecosystem:

zef install Data::Tranformers

From GitHub:

zef install https://github.com/antononcube/Raku-Data-Tranformers.git

accumulate

Cumulative sums:

use Data::Transformers;

accumulate([3, 8, 4, 11, 9, 2]);
# [3 11 15 26 35 37]

Cumulative sums for a list of lists of numbers:

accumulate((1...10).rotor(2))
# [[1 2] [4 6] [9 12] [16 20] [25 30]]

Applications

Triangular numbers:

(1..10) ==> accumulate
# [1 3 6 10 15 21 28 36 45 55]

Random walk:

use Text::Plot;
use Data::Generators;

text-list-plot(accumulate(random-real([-1, 1], 100)))
# +---+---------+---------+----------+---------+----------+--+      
# |                                                          |      
# +                                              **          +  2.00
# |   ** *                                      *            |      
# |   * ****                                    *  *         |      
# +     *  **                                 **    ****     +  0.00
# |         * *                               **       ***   |      
# |          *                               *       ** *    |      
# +           *     *                    *  **               + -2.00
# |            **   **        *   * ***** ***                |      
# |             ****  *  *   *   * *  *    *                 |      
# +              * * * * ** ** ****  *                       + -4.00
# |               *     * ***   *                            |      
# |                                                          |      
# +---+---------+---------+----------+---------+----------+--+      
#     0.00      20.00     40.00      60.00     80.00      100.00

array-pad

Pad the edges with 0s:

array-pad([1,2,3], 1)
# [0 1 2 3 0]

Specify different padding on each side:

array-pad(1..3, [2, 4])
# [0 0 1 2 3 0 0 0 0]

Pad the edges of a matrix:

.say for array-pad([[1, 2], [3, 4]], 2)
# [0 0 0 0 0 0]
# [0 0 0 0 0 0]
# [0 0 1 2 0 0]
# [0 0 3 4 0 0]
# [0 0 0 0 0 0]
# [0 0 0 0 0 0]

Pad according to a named rule:

array-pad(1..3, 2, "Fixed")
# [1 1 1 2 3 3 3]

Pad only on the right:

array-pad(1..3, [0, 2])
# [1 2 3 0 0]

Remove elements from each edge of an array:

array-pad(1..10, -2)
# [3 4 5 6 7 8]

This is equivalent to dropping the first two elements:

array-pad([1, 2, 3, 4], [-2, 0]);
# [3 4]

This is equivalent to dropping the last two elements:

array-pad([1, 2, 3, 4], [0, -2]);
# [1 2]

Named rules

Pad by repeating periodically:

array-pad(1..6, 4, "Periodic")
# [3 4 5 6 1 2 3 4 5 6 1 2 3 4]
array-pad(1..3, 4, "Periodic")
# [3 1 2 3 1 2 3 1 2 3 1]

Pad with the reversal of the list:

array-pad(1..3, 4, "Reversed")
# [3 3 2 1 1 2 3 3 2 1 1]

Pad with the negative of the reversal:

array-pad(1..3, 4, "ReversedNegation")
# [3 -3 -2 -1 1 2 3 -3 -2 -1 1]

Pad by reflecting about the edge:

array-pad(1..3, 4, "Reflected")
# [1 2 3 2 1 2 3 2 1 2 3]

center-array

Create a list of length 5 with a single 1 at the center:

center-array(5)
# [0 0 1 0 0]

Create a list of length 5 with the specified element at the center:

center-array('x', 5)
# [0 0 x 0 0]

Place an element at the center of a 2D array:

.say for center-array('x', [5, 5])
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 0 x 0 0]
# [0 0 0 0 0]
# [0 0 0 0 0]

Center a list in a 2D array:

.say for center-array(1..3, [5, 5])
# [0 0 0 0 0]
# [0 0 0 0 0]
# [0 1 2 3 0]
# [0 0 0 0 0]
# [0 0 0 0 0]

The default padding is zero:

center-array([1, 2, 3], 6)
# [0 1 2 3 0 0]

rescale

Rescale to run from 0 to 1 over the range -10 to 10:

rescale(2.5, [-10, 10])
# 0.625
rescale(12.5, [-10, 10])
# 1.125
rescale(3, [-9, 7], [11, 28])
# 23.75

Complex number inputs:

rescale(1 + 2i, [0, 5])
# 0.2+0.4i
rescale(1 + 2i, [0, 1 + 1i])
# 1.5+0.5i

Rescale so that all the list elements run from 0 to 1:

rescale([-.7, .5, 1.2, 5.6, 1.8])
# (0 0.190476 0.301587 1 0.396825)

clip

Not implemented yet.