generator = new Agglomerate([ 'red' => new Blob([255, 32, 0], 50.0), 'green' => new Blob([0, 128, 0], 10.0), 'blue' => new Blob([0, 32, 255], 30.0), ], [0.5, 0.2, 0.3]); $this->estimator = new NaiveBayes(null, 1.0); $this->metric = new FBeta(); srand(self::RANDOM_SEED); } protected function assertPreConditions() : void { $this->assertFalse($this->estimator->trained()); } /** * @test */ public function build() : void { $this->assertInstanceOf(NaiveBayes::class, $this->estimator); $this->assertInstanceOf(Online::class, $this->estimator); $this->assertInstanceOf(Learner::class, $this->estimator); $this->assertInstanceOf(Probabilistic::class, $this->estimator); $this->assertInstanceOf(Estimator::class, $this->estimator); $this->assertInstanceOf(Persistable::class, $this->estimator); } /** * @test */ public function badAlpha() : void { $this->expectException(InvalidArgumentException::class); new NaiveBayes(null, -1.0); } /** * @test */ public function type() : void { $this->assertEquals(EstimatorType::classifier(), $this->estimator->type()); } /** * @test */ public function compatibility() : void { $expected = [ DataType::categorical(), ]; $this->assertEquals($expected, $this->estimator->compatibility()); } /** * @test */ public function params() : void { $expected = [ 'priors' => null, 'smoothing' => 1.0, ]; $this->assertEquals($expected, $this->estimator->params()); } /** * @test */ public function trainPartialPredict() : void { $dataset = $this->generator->generate(self::TRAIN_SIZE + self::TEST_SIZE); $transformer = new IntervalDiscretizer(5); $transformer->fit($dataset); $dataset->apply($transformer); $testing = $dataset->randomize()->take(self::TEST_SIZE); $folds = $dataset->stratifiedFold(3); $this->estimator->train($folds[0]); $this->estimator->partial($folds[1]); $this->estimator->partial($folds[2]); $this->assertTrue($this->estimator->trained()); $predictions = $this->estimator->predict($testing); $score = $this->metric->score($predictions, $testing->labels()); $this->assertGreaterThanOrEqual(self::MIN_SCORE, $score); } /** * @test */ public function trainIncompatible() : void { $this->expectException(InvalidArgumentException::class); $this->estimator->train(Labeled::quick([[1.0]], ['green'])); } /** * @test */ public function predictUntrained() : void { $this->expectException(RuntimeException::class); $this->estimator->predict(Unlabeled::quick()); } }