本文整理汇总了C++中vector_t::normalize_1方法的典型用法代码示例。如果您正苦于以下问题:C++ vector_t::normalize_1方法的具体用法?C++ vector_t::normalize_1怎么用?C++ vector_t::normalize_1使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类vector_t
的用法示例。
在下文中一共展示了vector_t::normalize_1方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: most_similar
void most_similar(
index_t const user,
vector_ll_t /*out*/ neighbors,
vector_t /*out*/ weights,
matrix_t const sim)
{
stack_assert(neighbors.get_len() <= sim.get_rows());
stack_assert(weights.get_len() <= sim.get_rows());
stack_assert(neighbors.get_len() == weights.get_len());
stack_assert(sim.get_rows() == sim.get_cols());
// assert symmetric matrix; how ?
stack_assert(sim.get_rows() > user);
stack_assert(user >= 0); // always true if typeof(user) is unsigned.
stack::fe_asserter dummy1{};
// sort users by their similarity this this user
scoped_timer dummy(__func__);
vector_t sim_vec = sim.get_row_clone(user);
stack_assert(std::abs(boost::math::float_distance(sim_vec[user], 1.0)) <= 2);
// The previous assert won't hold when using perturbed similarity,
// However, we require it is set artificially to 1.
std::vector<index_t> all_others(sim.get_rows());
std::iota(std::begin(all_others), std::end(all_others), 0);
all_others.erase(std::begin(all_others) + user); // TODO could be optimized by doing iota is two steps
// top-k
// heapify(all_others.data(), sim_vec, neighbors.get_len());
// for (index_t i = 0; i < sim.get_rows(); i++)
// {
// // only consider unseen neighbors
// // this loop could be unnecessary given the next conditional, but be safe first then verify later
// if(i == user || std::find(std::begin(all_others), std::begin(all_others) + i, i) != std::begin(all_others) + i)
// continue;
// if (sim_vec[i] > sim_vec[all_others[0]] /*top of the heap: the min value of all similarities we have*/)
// {
// all_others[0] = i; // discards the old value
// sift_down(all_others.data(), sim_vec.get_data(), static_cast<index_t>(0), static_cast<index_t>(neighbors.get_len() - 1) /*end of heap - inclusive*/);
// }
// }
// I have to test my heap first. Be safe for now.
std::partial_sort(
std::begin(all_others),
std::begin(all_others) + neighbors.get_len(),
std::end(all_others),
[=](size_t a, size_t b)->bool
{
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
stack_assert(a < sim_vec.get_len());
stack_assert(b < sim_vec.get_len());
return sim_vec[a] >= sim_vec[b]; // descending order
});
// get weights of neighbors
all_others.resize(neighbors.get_len());
std::sort(std::begin(all_others), std::end(all_others));
{
size_t i = 0;
for(auto n : all_others)
{
neighbors[i] = n;
weights[i] = sim_vec[n];
stack_assert(weights[i] >= 0);
++i;
}
}
// normalize weights
// weights must be positive and sum to 1, normalize_1 will make them sum to 1
// and we have already verified they are positive
weights.normalize_1();
}