Case 2: Estimating object relevance

Implementation in pure PHP

In this variant we estimate object relevance via the dot product of features and weights. It is a fast and transparent baseline for ranking. We define a feature vector and an importance vector, then compute the final score as the sum of pairwise products.

 
<?php

function dotProduct(array $a, array $b): float {
    
$n count($a);

    if (
$n !== count($b)) {
        throw new 
InvalidArgumentException('Vectors must have the same length');
    }

    
$sum 0.0;

    for (
$i 0$i $n$i++) {
        
$sum += $a[$i] * $b[$i];
    }

    return 
$sum;
}

Implementation in RubixML

In the second variant we use RubixML with a Ridge regressor trained on examples with known target values. After training, the model predicts relevance for a new object, which is useful when moving from manual weights to a trainable approach.

 
<?php

use Rubix\ML\Datasets\Labeled;
use 
Rubix\ML\Regressors\Ridge;

$samples = [
    [
1052],
    [
410],
    [
2085],
];

$labels = [8215];

$dataset = new Labeled($samples$labels);

$model = new Ridge(1.0);
$model->train($dataset);