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.
Example of code
<?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.
Example of code
<?php
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Regressors\Ridge;
$samples = [
[10, 5, 2],
[4, 1, 0],
[20, 8, 5],
];
$labels = [8, 2, 15];
$dataset = new Labeled($samples, $labels);
$model = new Ridge(1.0);
$model->train($dataset);