PHP code example of divineomega / eloquent-attribute-value-prediction

1. Go to this page and download the library: Download divineomega/eloquent-attribute-value-prediction library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

divineomega / eloquent-attribute-value-prediction example snippets


$animal = new \App\Models\Animal();
$animal->size = 'small';
$animal->has_wings = false;
$animal->domesticated = true;

$animal->name = $animal->predict('name');

// 'cat'

$predictions = $animal->getPredictions('name');

// [
//   'cat' => 0.43,
//   'dog' => 0.40,
//   'bird' => 0.10,
//   'elephant => 0.9,
// ]

$house = new \App\Models\House();
$house->num_bedrooms = 3;
$house->num_bathrooms = 1;

$house->value = $house->predict('value');

// 180000



namespace App\Models;

use DivineOmega\EloquentAttributeValuePrediction\Interfaces\HasPredictableAttributes;
use DivineOmega\EloquentAttributeValuePrediction\Traits\PredictsAttributes;
use Illuminate\Database\Eloquent\Model;

class IrisFlower extends Model implements HasPredictableAttributes
{
    use PredictsAttributes;

}

public function registerPredictableAttributes(): array
    {
        return [
            'species' => [
                'sepal_length',
                'sepal_width',
                'petal_length',
                'petal_width',
            ],
        ];
    }

protected $casts = [
        'sepal_length' => 'float',
        'sepal_width' => 'float',
        'petal_length' => 'float',
        'petal_width' => 'float',
        'species' => 'string',
    ];

$flower = new \App\Models\IrisFlower();
$flower->sepal_length = 5.1;
$flower->sepal_width = 3.5;
$flower->petal_length = 1.4;
$flower->petal_width = 0.2;

$species = $flower->predict('species');  

$flower = new \App\Models\IrisFlower();
$flower->sepal_length = 4.5;
$flower->sepal_width = 2.3;
$flower->petal_length = 1.3;
$flower->petal_width = 0.3;

$predictions = $flower->getPredictions('species');

/*
array:3 [
  "setosa" => 0.69785665879791
  "versicolor" => 0.30214334120209
  "virginica" => 0.0
]
*/

public function registerEstimators(): array
{
    return [
        'species' => new MultilayerPerceptron([
            new Dense(50),
            new Dense(50),
        ]),
    ];
}
bash
php artisan eavp:train \\App\\Models\\IrisFlower