Installation:
composer require baks-dev/machine-learning
php bin/console baks:assets:install
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
baks-dev/core (≥7.4) compatibility.First Use Case:
use BaksDev\MachineLearning\Services\TrainingService;
$service = app(TrainingService::class);
$model = $service->trainFromData(
'path/to/training_data.csv',
'model_name',
['feature1', 'feature2'] // Features to train on
);
$prediction = $service->predict($model, ['feature1' => 1.2, 'feature2' => 3.4]);
Where to Look First:
config/packages/baks_machine_learning.yaml for default settings.php bin/console list baks to explore available commands (e.g., baks:ml:train, baks:ml:predict).tests/Unit/MachineLearning for usage patterns.Data Preparation:
DataPreprocessor to clean/normalize data before training:
$preprocessor = app(DataPreprocessor::class);
$cleanedData = $preprocessor->process($rawData, ['impute' => true, 'scale' => 'minmax']);
Model Training:
$service->trainFromData(
'data.csv',
'customer_churn_model',
['purchase_frequency', 'support_calls'],
['algorithm' => 'random_forest', 'epochs' => 100]
);
$service->updateModel('existing_model', 'new_data.csv');
Prediction Integration:
// In a Symfony Controller
public function predict(Request $request, TrainingService $service)
{
$model = $service->loadModel('customer_churn_model');
$input = $request->validate(['feature1' => 'required|numeric']);
return response()->json($service->predict($model, $input));
}
Model Management:
$service->saveModel($model, 'custom_path/models');
$loadedModel = $service->loadModel('model_name', 'custom_path/models');
Event-Driven Workflows:
// In a Service Provider
$this->bus->subscribe(
ModelTrainedEvent::class,
function (ModelTrainedEvent $event) {
// Notify team, log, etc.
}
);
$this->messageBus->dispatch(
new TrainModelMessage('data.csv', 'model_name', ['feature1', 'feature2'])
);
$cache = $this->container->get('cache.app');
$key = 'prediction:model_name:'.md5(json_encode($input));
$prediction = $cache->get($key, function() use ($service, $model, $input) {
return $service->predict($model, $input);
});
$entityManager->getEventManager()->addEventListener(
ModelSavedEvent::class,
new ModelPerformanceLogger()
);
Data Schema Mismatches:
DataValidator to validate:
$validator = app(DataValidator::class);
$errors = $validator->validate($data, ['feature1', 'feature2']);
php bin/console doctrine:migrations:execute).Memory Limits:
$service->trainFromData('large_data.csv', 'model', [], ['chunk_size' => 1000]);
Model Serialization:
Serializable. Override serialize()/unserialize() if needed.Dependency Conflicts:
baks-dev/core (≥7.4) is required. Downgrade if conflicts arise:
composer require baks-dev/core:^7.3
Russian Documentation:
src/Resources/config/ for YAML configs.config/packages/baks_machine_learning.yaml:
debug: true
logging: true
$service->dumpModel('model_name', 'path/to/dump.json');
random_forest, neural_net) matches src/Algorithms/ classes.DataPreprocessor::getFeatureSchema() to debug.Custom Algorithms:
Extend AbstractAlgorithm to add new models:
namespace App\MachineLearning;
use BaksDev\MachineLearning\Algorithms\AbstractAlgorithm;
class CustomAlgorithm extends AbstractAlgorithm {
public function train(array $data): void { /* ... */ }
public function predict(array $input): mixed { /* ... */ }
}
Register in config/packages/baks_machine_learning.yaml:
algorithms:
custom:
class: App\MachineLearning\CustomAlgorithm
GPU Acceleration: Configure CUDA support (if available):
# config/packages/baks_machine_learning.yaml
gpu:
enabled: true
device_id: 0
Testing: Use the provided test group:
php bin/phpunit --group=machine-learning --filter=testCustomAlgorithm
Performance Tuning:
config/packages/baks_machine_learning.yaml:
training:
batch_size: 512
threads: 4
hyperparams option:
$service->trainFromData(/* ... */, ['hyperparams' => ['learning_rate' => 0.001]]);
Backup Models: Automate model backups with a cron job:
php bin/console baks:ml:backup --path=/backups/models --days=7
How can I help you explore Laravel packages today?