Exemple #1
0
void scalar_mul_and_add(
    const common::sfv_t& left,
    float s,
    common::sfv_t& right) {
  common::sfv_t::const_iterator l = left.begin();
  common::sfv_t::iterator r = right.begin();
  while (l != left.end() && r != right.end()) {
    if (l->first < r->first) {
      std::pair<std::string, float> p = *l;
      p.second *= s;
      r = right.insert(r, p);
      ++l;
    } else if (l->first > r->first) {
      ++r;
    } else {
      r->second += l->second * s;
      ++l;
      ++r;
    }
  }
  for (; l != left.end(); ++l) {
    std::pair<std::string, float> p = *l;
    p.second *= s;
    right.push_back(p);
  }
}
 void add_feature(const std::string& key,
                  double value,
                  common::sfv_t& ret_fv) const {
   ret_fv.push_back(std::make_pair(
       key,
       static_cast<float>(std::log(std::max(1.0, value)))));
 }
Exemple #3
0
void recommender_base::complete_row(const common::sfv_t& query,
                                    common::sfv_t& ret) const {
  ret.clear();
  vector<pair<string, float> > ids;
  similar_row(query, ids, complete_row_similar_num_);
  if (ids.size() == 0) {
    return;
  }

  size_t exist_row_num = 0;
  for (size_t i = 0; i < ids.size(); ++i) {
    common::sfv_t row;
    orig_.get_row(ids[i].first, row);
    if (row.size() == 0) {
      continue;
    } else {
      ++exist_row_num;
    }
    float ratio = ids[i].second;
    for (size_t j = 0; j < row.size(); ++j) {
      ret.push_back(make_pair(row[j].first, row[j].second * ratio));
    }
  }

  if (exist_row_num == 0) {
    return;
  }
  common::sort_and_merge(ret);
  for (size_t i = 0; i < ret.size(); ++i) {
    ret[i].second /= exist_row_num;
  }
}
 void add_feature(const std::string& key,
                  double value,
                  common::sfv_t& ret_fv) const {
   std::stringstream ss;
   ss << key << "$" << value;
   ret_fv.push_back(std::make_pair(ss.str(), static_cast<float>(1.0)));
 }
void eigen_feature_mapper::rinsert(
    const pair<int, float>& item,
    common::sfv_t& dst) const {
  if (rmap_.find(item.first) != rmap_.end()) {
    dst.push_back(
        make_pair((rmap_.find(item.first))->second, item.second));
  }
}