{"id":15984898,"url":"https://github.com/divineomega/eloquent-attribute-value-prediction","last_synced_at":"2025-07-23T18:07:51.558Z","repository":{"id":54264170,"uuid":"250385238","full_name":"DivineOmega/eloquent-attribute-value-prediction","owner":"DivineOmega","description":"Predict attribute values for your Laravel Eloquent models using machine learning!","archived":false,"fork":false,"pushed_at":"2022-04-21T00:18:47.000Z","size":53,"stargazers_count":149,"open_issues_count":4,"forks_count":11,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-06-15T01:36:49.694Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DivineOmega.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-26T22:25:32.000Z","updated_at":"2025-05-01T14:38:38.000Z","dependencies_parsed_at":"2022-08-13T10:20:54.758Z","dependency_job_id":null,"html_url":"https://github.com/DivineOmega/eloquent-attribute-value-prediction","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/DivineOmega/eloquent-attribute-value-prediction","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DivineOmega%2Feloquent-attribute-value-prediction","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DivineOmega%2Feloquent-attribute-value-prediction/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DivineOmega%2Feloquent-attribute-value-prediction/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DivineOmega%2Feloquent-attribute-value-prediction/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DivineOmega","download_url":"https://codeload.github.com/DivineOmega/eloquent-attribute-value-prediction/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DivineOmega%2Feloquent-attribute-value-prediction/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266726679,"owners_count":23974928,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-08T02:11:10.661Z","updated_at":"2025-07-23T18:07:51.516Z","avatar_url":"https://github.com/DivineOmega.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003chr/\u003e\n\n\u003ch1 align=\"center\"\u003e🖥️🧠💪 Eloquent Attribute Value Prediction\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\n    ❤️ \u003cb\u003eMachine Learning For Laravel Developers!\u003c/b\u003e ❤️ \n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/DivineOmega/eloquent-attribute-value-prediction/actions\"\u003e\n        \u003cimg src=\"https://github.com/DivineOmega/eloquent-attribute-value-prediction/workflows/Tests/badge.svg\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://packagist.org/packages/divineomega/eloquent-attribute-value-prediction/stats\"\u003e\n        \u003cimg src=\"https://img.shields.io/packagist/dt/DivineOmega/eloquent-attribute-value-prediction.svg\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003chr/\u003e\n\nWith this package you'll be able to predict attribute values for your Laravel Eloquent models using the power of machine learning! 🌟\n\nWith an intuitive syntax you can predict the values of both categorical (string) and continuous (numeric) attributes. Take a look at the examples below.\n\n```php\n$animal = new \\App\\Models\\Animal();\n$animal-\u003esize = 'small';\n$animal-\u003ehas_wings = false;\n$animal-\u003edomesticated = true;\n\n$animal-\u003ename = $animal-\u003epredict('name');\n\n// 'cat'\n\n$predictions = $animal-\u003egetPredictions('name');\n\n// [\n//   'cat' =\u003e 0.43,\n//   'dog' =\u003e 0.40,\n//   'bird' =\u003e 0.10,\n//   'elephant =\u003e 0.9,\n// ]\n\n$house = new \\App\\Models\\House();\n$house-\u003enum_bedrooms = 3;\n$house-\u003enum_bathrooms = 1;\n\n$house-\u003evalue = $house-\u003epredict('value');\n\n// 180000\n```\n\n## Installation\n\nTo install just run the following Composer command.\n\n```bash\ncomposer require divineomega/eloquent-attribute-value-prediction\n```\n\nAfter installation, you need to set up your model for attribute prediction.\n\n## Setup\n\nLet's say you have an `IrisFlowers` table that contains data about each of \nthree species of the Iris flower. In this example, we want to be able to \npredict the flower's species given the sepal length and width, and \npetal length and width.\n\nFirst, we need to set up out `IrisFlower` model for attribut prediction.\n\nThis is done by adding the `HasPredictableAttributes` interface and \n`PredictsAttributes` trait to our model as shown below.\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse DivineOmega\\EloquentAttributeValuePrediction\\Interfaces\\HasPredictableAttributes;\nuse DivineOmega\\EloquentAttributeValuePrediction\\Traits\\PredictsAttributes;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass IrisFlower extends Model implements HasPredictableAttributes\n{\n    use PredictsAttributes;\n\n}\n``` \n\nWe then need to tell our model which attributes we wish to predict. We do this\nby adding the `registerPredictableAttributes()` function. \n\nIn this example, we want to use predict the `species` attribute based on the \n`sepal_length`, `sepal_width`, `petal_length`, and `petal_width` attributes.\nThis can be done by returning an array in the following format.\n\n```php\npublic function registerPredictableAttributes(): array\n    {\n        return [\n            'species' =\u003e [\n                'sepal_length',\n                'sepal_width',\n                'petal_length',\n                'petal_width',\n            ],\n        ];\n    }\n```\n\nYou also need to add the attributes you are using to the `$casts` array. \nIt is important that the machine learning algorithm knows the type of data\nstored in each attribute, and that it is consistent.\n\nFor our `IrisFlower` example, the following format is appropriate.\n\n```php\nprotected $casts = [\n        'sepal_length' =\u003e 'float',\n        'sepal_width' =\u003e 'float',\n        'petal_length' =\u003e 'float',\n        'petal_width' =\u003e 'float',\n        'species' =\u003e 'string',\n    ];\n``` \n\n## Training\n\nBefore you can make attribute value predictions, you must train a machine \nlearning model on your data. As a general rule, the more data you provide\nyour model, the better it will perform, and the more accurate it will be.\n\nYou can train your model(s) using the `eavp:train` Artisan command, as shown\nin the example below.\n\n```bash\nphp artisan eavp:train \\\\App\\\\Models\\\\IrisFlower\n```\n\nOne model will be trained for each of the attributes you wish to predict. When\nthey are trained, they will be saved into the `storage/eavp/model/` directory\nfor future use.\n\nBe aware that the training process can take some time to complete depending \non the amount of data you are using, and the complexity of your machine \nlearning model. Training progress will be output to the console where possible.\n\nYou can re-run this command (manually, or on a schedule) to re-train your \nmachine learning model(s). Previously trained models will be replaced \nautomatically. \n\n## Prediction\n\nOnce you have set up your Eloquent model, and trained your machine learning \nmodel(s), you can begin predicting attributes.\n\nFor example, to predict the species of an Iris flower, you can create a new\n`IrisFlower` object and populate a few of its known attributes, then call the\n`predict` method.\n\n```php\n$flower = new \\App\\Models\\IrisFlower();\n$flower-\u003esepal_length = 5.1;\n$flower-\u003esepal_width = 3.5;\n$flower-\u003epetal_length = 1.4;\n$flower-\u003epetal_width = 0.2;\n\n$species = $flower-\u003epredict('species');  \n```\n\nThe `predict` method should be passed the attribute name you wish to predict.\nIt will then returns the prediction as a string or numeric type. \n\nIn our example, this should be the 'setosa'\nspecies, based on [Iris flower data set](https://en.wikipedia.org/wiki/Iris_flower_data_set).\n\n## Advanced\n\n### Prediction confidence\n\nIf you wish, you can also retrieve the machine learning model's\nconfidence that a prediction is correct. This is done with the `getPredictions`\nmethod.\n\n```php\n$flower = new \\App\\Models\\IrisFlower();\n$flower-\u003esepal_length = 4.5;\n$flower-\u003esepal_width = 2.3;\n$flower-\u003epetal_length = 1.3;\n$flower-\u003epetal_width = 0.3;\n\n$predictions = $flower-\u003egetPredictions('species');\n\n/*\narray:3 [\n  \"setosa\" =\u003e 0.69785665879791\n  \"versicolor\" =\u003e 0.30214334120209\n  \"virginica\" =\u003e 0.0\n]\n*/\n```\n\nIn this example, you can see that the machine learning model is ~70% confident\nthe flower is a Setosa, ~30 confident the flower is a Versicolor, and 0% \nconfident the flower is a Virginica.\n\nNote that you can only use the `getPredictions` method if the attribute you are\nattempting to predict the value of is non-numeric. \n\n### Changing attribute estimators\n\nBy default, attribute values are predicted using K-d Neighbors. \nThis is a more efficient form of a standard K Nearest Neighbors algorithm.\n\nThe machine learning algorithm that is used to predict your attribute values\nis known as an 'estimator'. If you wish, you can modify the estimator which\nis used for each attribute.\n\nTo do this, you need to add a `registerEstimators` method to your model.\n\n```php\npublic function registerEstimators(): array\n{\n    return [\n        'species' =\u003e new MultilayerPerceptron([\n            new Dense(50),\n            new Dense(50),\n        ]),\n    ];\n}\n```\n\nIn the example above, we are changing the estimator for the `species` attribute\nto a multilayer perceptron classifier (neural network) with two densely connected \nhidden layers.\n\nUnder the hood, this package uses the [Rubix ML](https://rubixml.com/) library.\nThis means you can use any estimator is supports.\n\nSee the [Choosing an Estimator](https://docs.rubixml.com/latest/choosing-an-estimator.html)\npage for a list of all available estimators you can use for attribute prediction.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivineomega%2Feloquent-attribute-value-prediction","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdivineomega%2Feloquent-attribute-value-prediction","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdivineomega%2Feloquent-attribute-value-prediction/lists"}