anchetaWern / sitepoint_codes

Code samples from my Sitepoint articles
51 stars 122 forks source link

movie db not loaded into prediction io? #1

Open Nathan-Srivi opened 10 years ago

Nathan-Srivi commented 10 years ago

Hi,

I am integrating your code into my prediction io. but movie db not uploaded into my prediction io. My code is given below

<?php //echo $agent= 'https://api.themoviedb.org/3/movie/popular?api_key=7b5e30851a9285340e78c201c4e4ab99&page=1'; require_once("vendor/autoload.php"); use PredictionIO\PredictionIOClient;

    $client = PredictionIOClient::factory(array("appkey" => "0slTZbbSiKIbK0topdXLfw0Byef4SHSCOJhReORCFYJbEBEGVmwAcBg3rKydeVMf"));
    $index = 0;

$index = 41; for($x = 3; $x <= 100; $x++){

        $movies_url = file_get_contents('https://api.themoviedb.org/3/movie/popular?api_key=MY_API_KEY&page=1');

        $movies_result = json_decode($movies_url, true);

    foreach($movies_result ['results'] as $row)
    {
         $id = $row['id'];
      $title = $row['title'];
      $poster_path = '';
      if(!empty($row['poster_path'])){
            $poster_path = $row['poster_path'];
          }

          $moviedetails_url = 'https://api.themoviedb.org/3/movie/' . $id . '?api_key=MY_API_KEY';

        $movies_url_new = file_get_contents($moviedetails_url);
        $movie = json_decode($movies_url_new, true);

          $overview = $movie['overview'];
          $release_date = $movie['release_date'];

          $command = $client->getCommand('create_item', array('pio_iid' => $index, 'pio_itypes' => 1));
          $command->set('tmdb_id', $id);
          $command->set('title', $title);
          $command->set('poster_path', $poster_path);
          $command->set('overview', $overview);
          $command->set('release_date', $release_date);

          $client_response = $client->execute($command);
          print_r($client_response);
          echo "<br><br>";
  $index++;

    }

}

when i run this code message shows Array ( [message] => Item created. )

But when i execute Deployed Algorithm i got the following warning

Not Running: Waiting for the the first-time data model training.

What can i do.. please help me. when i run prediction io sample import.php its working fine for me. example import.php file is given below

<?php // use composer's autoloader to load PredictionIO PHP SDK require_once("vendor/autoload.php"); use PredictionIO\PredictionIOClient; $client = PredictionIOClient::factory(array("appkey" => "ZTI6PW51bZdiE5rLgwhtGZhAuFM1llwLSHM2u4xXuQfL1Yi6XTdypiFfBmtyQyV2"));

// generate 10 users, with user ids 1,2,....,10
for ($i=1; $i<=10; $i++) {
    echo "Add user ". $i . "<br>";
    $command = $client->getCommand('create_user', array('pio_uid' => $i));
    $response = $client->execute($command);
}

// generate 50 items, with item ids 1,2,....,50
// assign type id 1 to all of them
for ($i=1; $i<=50; $i++) {
    echo "Add item ". $i . "<br>";
    $command = $client->getCommand('create_item', array('pio_iid' => $i, 'pio_itypes' => 1));
                $command->set('name', $name);
                $command->set('company', $company);

    $response = $client->execute($command);
}
// each user randomly views 10 items
for ($u=1; $u<=10; $u++) {
    for ($count=0; $count<10; $count++) {
        $i = rand(1, 50); // randomly pick an item
        echo "User ". $u . " views item ". $i ."<br>";
        $client->identify($u);
        $client->execute($client->getCommand('record_action_on_item', array('pio_action' => 'view', 'pio_iid' => $i)));
    }
}

?>

please help me asap

anchetaWern commented 10 years ago

Nothing was mentioned about the record_action_on_item method. In order for prediction io to work you need to import some users actions like view or like on a few movies.

Nathan-Srivi commented 10 years ago

Hi anchetaWern,

Thankyou so much for your reply,

You just misunderstand my question anchetaWern. i just modified your code and when i run the code is not working for me. my code same like your project code. i just hardcore some links like file_get_contents only.

please review my code and please give me the details. my code is given below.

<?php //echo $agent= 'https://api.themoviedb.org/3/movie/popular?api_key=7b5e30851a9285340e78c201c4e4ab99&page=1'; require_once("vendor/autoload.php"); use PredictionIO\PredictionIOClient;

$client = PredictionIOClient::factory(array("appkey" => "0slTZbbSiKIbK0topdXLfw0Byef4SHSCOJhReORCFYJbEBEGVmwAcBg3rKydeVMf"));
$index = 0;

    $movies_url = file_get_contents('https://api.themoviedb.org/3/movie/popular?api_key=MY_API_KEY&page=1');

    $movies_result = json_decode($movies_url, true);

foreach($movies_result ['results'] as $row)
{
     $id = $row['id'];
  $title = $row['title'];
  $poster_path = '';
  if(!empty($row['poster_path'])){
        $poster_path = $row['poster_path'];
      }

      $moviedetails_url = 'https://api.themoviedb.org/3/movie/' . $id . '?api_key=MY_API_KEY';

    $movies_url_new = file_get_contents($moviedetails_url);
    $movie = json_decode($movies_url_new, true);

      $overview = $movie['overview'];
      $release_date = $movie['release_date'];

      $command = $client->getCommand('create_item', array('pio_iid' => $index, 'pio_itypes' => 1));
      $command->set('tmdb_id', $id);
      $command->set('title', $title);
      $command->set('poster_path', $poster_path);
      $command->set('overview', $overview);
      $command->set('release_date', $release_date);

      $client_response = $client->execute($command);
      print_r($client_response);
      echo "<br><br>";

$index++;

}

Please guide me i am helpless

anchetaWern commented 10 years ago

You mentioned this was the problem right?

But when i execute Deployed Algorithm i got the following warning

Not Running: Waiting for the the first-time data model training.

That's exactly what you get when prediction io doesn't have enough data to make suggestions. The code won't work immediately if you just run it for the first time. It needs some movie data and user actions in order to work.

As I can see from the code you've given so far you're only importing movie data but not performing any user actions.

Nathan-Srivi commented 10 years ago

ok please wait. i will check it some thinks and get back to you..

Nathan-Srivi commented 10 years ago

Hi anchetaWern,

i am importing records into prediction io. but i have one problem please help me $client->execute($client->getCommand('record_action_on_item', array('pio_action' => 'view', 'pio_iid' => $index))); // you just load $index on record importing. $index just a auto increment item.

When i change $index into my product id data not loaded.

and following URl is my recommentation show page

https://gist.github.com/anandanmdu/21aab21cbdf41f1349d2

When i print the $recommended_movies_raw i got a following output.

'Array ( [pio_iids] => Array ( [0] => 4 [1] => 7 )

)'

i just get the 4,7 only. how can i get the recommendation list please help me. Please help me. i am using pure core php and my sql & preidctionio

Nathan-Srivi commented 10 years ago

This is my full code pack

https://gist.github.com/anandanmdu/21aab21cbdf41f1349d2

please help me i am realy helplessss

anchetaWern commented 10 years ago

You're almost there. All you have to do now is to use those ID's to fetch the corresponding items from the database. You mentioned you're using MySQL. You can just use the WHERE IN clause and supply those ID's into it. Something like this:

$ids = implode("','", $recommended_movies_raw);
$ids = "'" . $ids . "'";
SELECT * FROM items WHERE id IN ($ids)

I'm assuming you got the correct IDs saved in your database because that's how you would usually set things up.

Nathan-Srivi commented 10 years ago

Hi anchetaWern,

Thank you so much for your response.. i found only one problem for the folloing line. when i send movie id into import product like this

$k = array(121,124,126,128);//my product ids foreach($k as $productid) { $client->execute($client->getCommand('record_action_on_item', array('pio_action' => 'view', 'pio_iid' => $productid))); }

data not import into prediction io.

But when i send dummy auto increment ids its working like this Working code

for($k=1;$k<10;$k++) { $client->execute($client->getCommand('record_action_on_item', array('pio_action' => 'view', 'pio_iid' => $k))); }

Please refer this gist also https://gist.github.com/anandanmdu/3b00e12a07b048e8b35a

my movie id's are not continuously. so i need this please help me. i think this is my final issue

Nathan-Srivi commented 10 years ago

if i send movie id un order list format i mean (1,3,5,6) this type of movie ids. prediction io page when i click run train data model now my engine status

Engine Status: Not Running: Waiting for the the first-time data model training.

This is the issue i faced. please help me

Nathan-Srivi commented 10 years ago

Please help me anchetaWern,

anchetaWern commented 10 years ago

That's odd. It should be working even if the order is like that. One thing you can do is to make sure that those ids has already corresponding items in prediction io saved using the create_item command. Before the record_action_on_item command is called you first need to have save users and items. And then make sure that those user ids and item ids you're using exists before using them on the record_action_on_item command.

Nathan-Srivi commented 10 years ago

Hi anchentaWern,

This is my full pack code including all files

https://gist.github.com/anandanmdu/a748f359276662a83644

Nathan-Srivi commented 10 years ago

Hi anchetaWern,

Please help me.. Have you seen my codes?.