Data::Transformers
Data::Transformers
Raku package with algorithms for transforming and smoothing data. Provides the subs:
DONE
accumulateDONE
array-padTODO
array-filterDONE
center-arrayTODO
clipTODO
differenceTODO
list-convolveTODO
list-correlateTODO
moving-averageTODO
moving-mapTODO
moving-medianDONE
rescaleDONE
standardize
Installation
From Zef ecosystem:
zef install Data::TranformersFrom GitHub:
zef install https://github.com/antononcube/Raku-Data-Tranformers.gitaccumulate
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)))# +---+---------+---------+----------+---------+----------+--+
# | |
# | ***** |
# | * *** ** * |
# + ****** * ** + 10.00
# | ** ** *** |
# | ** ********** * |
# | ***** * |
# + * *** + 5.00
# | **** |
# | *** |
# | *** * |
# + * ***** + 0.00
# | |
# +---+---------+---------+----------+---------+----------+--+
# 0.00 20.00 40.00 60.00 80.00 100.00array-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.625rescale(12.5, [-10, 10])# 1.125rescale(3, [-9, 7], [11, 28])# 23.75Complex number inputs:
rescale(1 + 2i, [0, 5])# 0.2+0.4irescale(1 + 2i, [0, 1 + 1i])# 1.5+0.5iRescale 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.
standardize
standardize(@data) shifts and rescales the elements of @data to have zero mean and unit sample variance.
standardize(@data, $center, $scale) shifts by $center and scales by $scale.
The arguments $center and $scale can be numbers, callables, or Whatever.
Compute standard scores for data:
standardize([6.5, 3.8, 6.6, 5.7, 6.0, 6.4, 5.3])# [0.8177071028391707 -2.1543437132493533 0.9277830589905974 -0.06290054637224389 0.2673273220820366 0.7076311466877439 -0.5032043709779511]