GraphAware\Reco4PHP\Algorithms\Model\KNNModelBuilder::createVectors PHP Method

createVectors() public method

public createVectors ( ObjectSet $tfSource, ObjectSet $tfDestination )
$tfSource GraphAware\Reco4PHP\Common\ObjectSet
$tfDestination GraphAware\Reco4PHP\Common\ObjectSet
    public function createVectors(ObjectSet $tfSource, ObjectSet $tfDestination)
    {
        $ratings = [];
        foreach ($tfSource->getAll() as $source) {
            /* @var \GraphAware\Reco4PHP\Algorithms\Model\Rating $source */
            $ratings[$source->getId()][0] = $source->getRating();
        }
        foreach ($tfDestination->getAll() as $dest) {
            /* @var \GraphAware\Reco4PHP\Algorithms\Model\Rating $dest */
            $ratings[$dest->getId()][1] = $dest->getRating();
        }
        ksort($ratings);
        $xVector = [];
        $yVector = [];
        foreach ($ratings as $k => $rating) {
            $xVector[] = array_key_exists(0, $ratings[$k]) ? $ratings[$k][0] : 0;
            $yVector[] = array_key_exists(1, $ratings[$k]) ? $ratings[$k][1] : 0;
        }
        return array($xVector, $yVector);
    }

Usage Example

 public function testCreateVectors()
 {
     $instance = new KNNModelBuilder();
     $source = new ObjectSet(Rating::class);
     $destination = new ObjectSet(Rating::class);
     $node1 = new FakeNode(1);
     $node2 = new FakeNode(2);
     $node3 = new FakeNode(3);
     $node4 = new FakeNode(4);
     $source->add(new Rating(1, $node1->identity()));
     $source->add(new Rating(1, $node3->identity()));
     $destination->add(new Rating(1, $node2->identity()));
     $destination->add(new Rating(1, $node4->identity()));
     $vectors = $instance->createVectors($source, $destination);
     $xVector = $vectors[0];
     $yVector = $vectors[1];
     $this->assertEquals(array(1, 0, 1, 0), $xVector);
     $this->assertEquals(array(0, 1, 0, 1), $yVector);
 }