1. Go to this page and download the library: Download jenutka/titanic_php 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/ */
jenutka / titanic_php example snippets
use Rubix\ML\Extractors\CSV;
use Rubix\ML\Extractors\ColumnPicker;
$extractor = new ColumnPicker(new CSV('train.csv', true), [
'Pclass', 'Age', 'Fare', 'SibSp', 'Parch', 'Sex', 'Embarked', 'Survived',
]);
use Rubix\ML\Transformers\LambdaFunction;
$toPlaceholder = function (&$sample, $offset, $types) {
foreach ($sample as $column => &$value) {
if (empty($value) && $types[$column]->isContinuous()) {
$value = NAN;
}
else if (empty($value) && $types[$column]->isCategorical()) {
$value = '?';
}
}
};
use Rubix\ML\Transformers\MinMaxNormalizer;
use Rubix\ML\Transformers\OneHotEncoder;
use Rubix\ML\Transformers\MissingDataImputer;
$minMaxNormalizer = new MinMaxNormalizer();
$oneHotEncoder = new OneHotEncoder();
$imputer = new MissingDataImputer();
use Rubix\ML\Persisters\Filesystem;
use Rubix\ML\Serializers\RBX;
$serializer->serialize($imputer)->saveTo(new Filesystem('imputer.rbx'));
$serializer->serialize($minMaxNormalizer)->saveTo(new Filesystem('minmax.rbx'));
$serializer->serialize($oneHotEncoder)->saveTo(new Filesystem('onehot.rbx'));
use Rubix\ML\Classifiers\RandomForest;
use Rubix\ML\Classifiers\ClassificationTree;
$estimator = new RandomForest(new ClassificationTree(10), 500, 0.8, false);
$estimator->train($dataset);
use Rubix\ML\PersistentModel;
if (strtolower(readline('Save this model? (y|[n]): ')) === 'y') {
$estimator = new PersistentModel($estimator, new Filesystem('model.rbx'));
$estimator->save();
$logger->info('Model saved as model.rbx');
}
use Rubix\ML\Extractors\ColumnPicker;
$extractor = new ColumnPicker(new CSV('test.csv', true), [
'Pclass', 'Age', 'Fare', 'SibSp', 'Parch', 'Sex', 'Embarked',
]);
$persister_imputer = new Filesystem('imputer.rbx', true, new RBX());
$imputer = $persister_imputer->load()->deserializeWith(new RBX);
$persister_minMax = new Filesystem('minmax.rbx', true, new RBX());
$minMaxNormalizer = $persister_minMax->load()->deserializeWith(new RBX);
$persister_oneHot = new Filesystem('onehot.rbx', true, new RBX());
$oneHotEncoder = $persister_oneHot->load()->deserializeWith(new RBX);
use Rubix\ML\Datasets\Unlabeled;
use Rubix\ML\Transformers\LambdaFunction;
$dataset = Unlabeled::fromIterator($extractor)
->apply(new NumericStringConverter());
$dataset->apply(new LambdaFunction($toPlaceholder, $dataset->types()))
->apply($imputer)
->apply($minMaxNormalizer)
->apply($oneHotEncoder);
use Rubix\ML\PersistentModel;
use Rubix\ML\Persisters\Filesystem;
use Rubix\ML\Serializers\RBX;
$estimator = PersistentModel::load(new Filesystem('model.rbx'));