You are here

function _recommender_expand_sparse_data in Recommender API 5

Same name and namespace in other branches
  1. 6 recommender.module \_recommender_expand_sparse_data()

Expand the sparse database table to have the missing data default as zero. 'adjusted' means that only mouses that share common cheese will be expanded.

Parameters

$table_name the input table name:

$field_mouse the input table field for mouse:

$field_cheese the input table field for cheese:

$field_weight the input table field weight:

$missing the way of handling missing data. could be either 'zero' or 'adjusted'.:

Return value

null the {recommender_helper_matrix} and {recommender_helper_pair_stat} will be filled with data.

2 calls to _recommender_expand_sparse_data()
recommender_prediction_classical in ./recommender.module
Classical weight-average algorithm to calculate prediction from the similarity matrix, based on average weight. Limitation: we only do database operation for now. no in-memory operation available until future release. Limitation: we only do average…
_recommender_similarity_classical_in_database in ./recommender.module
Classical algorithm computed in databases. Full functioning. It has no memory limits because all data are saved in database. But the performance is worse than in-memory computation.

File

./recommender.module, line 259
Providing generic recommender system algorithms.

Code

function _recommender_expand_sparse_data($table_name, $field_mouse, $field_cheese, $field_weight, $missing) {

  // clean up
  db_query("DELETE FROM {recommender_helper_matrix}");

  // if 'adjusted', we record the pairs of mice that share at least one cheese.
  // we'll skip those mice that don't share cheese with other mice.
  // this measure is extremely useful for very sparse dataset.
  if ($missing == 'adjusted') {
    db_query("DELETE FROM {recommender_helper_pair_stat}");
    db_query("INSERT INTO {recommender_helper_pair_stat}(id1, id2)\n              SELECT n1.{$field_mouse}, n2.{$field_mouse} FROM {{$table_name}} n1\n              INNER JOIN {{$table_name}} n2 ON n1.{$field_cheese}=n2.{$field_cheese}\n              GROUP BY n1.{$field_mouse}, n2.{$field_mouse}");
  }

  // expand sparse matrix in {$table_name} to the full matrix, saved to {recommender_helper_matrix}, initially as 0
  // if 'adjusted', then we skip those mice that don't share at least one cheese with other mice.
  $sql_adjusted = $missing != 'adjusted' ? '' : "INNER JOIN (SELECT DISTINCT id1 FROM {recommender_helper_pair_stat}) p ON l.{$field_mouse}=p.id1";
  db_query("INSERT INTO {recommender_helper_matrix} SELECT m.mouse_id, c.cheese_id, 0 FROM\n            (SELECT DISTINCT l.{$field_mouse} mouse_id FROM {{$table_name}} l {$sql_adjusted}) m JOIN\n            (SELECT DISTINCT {$field_cheese} cheese_id FROM {{$table_name}}) c");

  // update the full matrix to have the correct value where cell is not 0
  db_query("UPDATE {recommender_helper_matrix} m, {{$table_name}} l SET m.weight=l.{$field_weight}\n            WHERE m.mouse_id=l.{$field_mouse} AND m.cheese_id=l.{$field_cheese}");
}