Skip to content

Error in prediction of a dataset, prediction not linked this dataset #360

Open
@caporaliismaele

Description

@caporaliismaele

PLEASE HELP ME I HAVE A NEAR DEADLINE

function Train($conn, $supplier_code, &$supplier_data, &$encoder, $weights, &$normalizer, $i)
{
// Data validation
if (empty($supplier_data) || !is_array($supplier_data)) {
logOnDB($conn, "logs_models", $supplier_code, 1, "Invalid supplier data - step1");
return null;
}

// Data restructuring
$supplier_data = RestructureData($supplier_data, $i);

// Dataset creation
$samples = array_map(fn($row) => [(int)$row['progressive_block'], (int)$row['cyclic_block']], $supplier_data);
$labels = array_map(fn($row) => (int)$row['quantity'], $supplier_data);
$dataset = new Labeled($samples, $labels);

// Data normalization
$normalizer = new ZScaleStandardizer();
$normalizer->fit($dataset);
$dataset->apply($normalizer);

// Apply weights
foreach ($dataset->samples() as &$sample) {
    foreach ($weights as $column => $weight) {
        $sample[$column] *= $weight;
    }
}
$dataset = new Labeled($dataset->samples(), $labels);

// Model creation
$estimator = new RegressionTree();
$estimator->train($dataset);

// Log training result
$comment = $estimator->trained() ? "Model successfully trained - step2" : "Model training error - step2";
$logError = $estimator->trained() ? 0 : 1;
logOnDB($conn, "logs_models", $supplier_code, $logError, $comment);

return $estimator;

}

function Predict($conn, $product_codes, $company_codes, $supplier_code, &$product_quantities, $estimator, $encoder, $weights, $normalizer, $i)
{
$product_code = $product_codes[(string)$i] ?? null;
$company_code = $company_codes[(string)$i] ?? null;

if (empty($product_code) || empty($company_code)) {
    $product_quantities[$i] = null;
    return;
}

$cyclic_block_two_months_ahead = 1;
$progressive_block = getUltimateProgressiveBlock();

// Create dataset for prediction
$datasetPredict = new Unlabeled([[(int)$progressive_block, (int)$cyclic_block_two_months_ahead]]);
$datasetPredict->apply($normalizer);

// Apply weights
foreach ($datasetPredict->samples() as &$sample) {
    foreach ($weights as $column => $weight) {
        $sample[$column] *= $weight;
    }
}
$dataset = new Unlabeled($datasetPredict->samples());


// Predict and convert result
$quantity = intval($estimator->predict($dataset)[0]);
$product_quantities[$i] = $quantity;

}

this are the functions that i use to train my dataset and make prediction
with this code sometimes the prediction are not linked to the dataset, for example if the avg of the dataset is 32 the prediction is 10, this is far below the normal.

I tried changing estimator or nomalizer but there are not sensible changes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions