Data Transformation with PHP

Reshaping Data Structures with Rubix

Reshaping allows us to organize data into structures required by specific algorithms. For example, in time series analysis, data can be reshaped into rolling windows for sequence modeling.

100
150
200
250
300
350

Example of use:

 
<?php

use Rubix\ML\Datasets\Unlabeled;
use 
Rubix\ML\Extractors\CSV;
use 
Rubix\ML\Transformers\MinMaxNormalizer;
use 
Rubix\ML\Transformers\ZScaleStandardizer;

// Load the dataset using CSV
$dataset Unlabeled::fromIterator(new CSV(dirname(__FILE__) . '/data/time_series.csv'false));

// Create windows manually since RubixML doesn't have built-in windowing
function reshapeIntoRollingWindows(array $dataint $windowSize): array {
    
// If input is a flat array, convert each element to an array
    
$isFlat = !is_array(reset($data));
    
$formattedData $isFlat array_map(fn ($value) => [$value], $data) : $data;

    
$windows = [];
    for (
$i 0$i <= count($formattedData) - $windowSize$i++) {
        
$window array_slice($formattedData$i$windowSize);
        
$windows[] = array_column($window0);
    }
    return 
$windows;
}

$reshapedData reshapeIntoRollingWindows($dataset->samples(), 3);

// Convert back to RubixML dataset if needed
$windowedDataset = new Unlabeled($reshapedData);

echo 
"After Reshaping: \n";
echo 
"---------------\n";
// Format output
$reshapedDataCount count($reshapedData);
echo 
'[';
foreach (
$reshapedData as $i => $window) {
    echo 
'[' implode(', '$window) . ']';
    if (
$i $reshapedDataCount 1) {
        echo 
', ';
    }
}
echo 
"]\n";