Skip to content

Instantly share code, notes, and snippets.

@abinashpanda
Created June 14, 2014 11:38
Show Gist options
  • Save abinashpanda/d1273e12b3aa6886a5c2 to your computer and use it in GitHub Desktop.
Save abinashpanda/d1273e12b3aa6886a5c2 to your computer and use it in GitHub Desktop.
SGSparseVector<float64_t> hash_vector(SGSparseVector<float64_t> vec, int32_t dim,
uint32_t seed)
{
SGVector<float64_t> h_vec(dim);
h_vec.zero();
for (index_t i=0; i<vec.num_feat_entries; i++)
{
uint32_t hash = CHash::MurmurHash3((uint8_t* ) &vec.features[i].feat_index, sizeof (index_t),
seed);
h_vec[hash % dim] += vec.features[i].entry;
}
int32_t num_nnz_features = 0;
for (index_t i=0; i<dim; i++)
{
if (h_vec[i]!=0)
num_nnz_features++;
}
SGSparseVector<float64_t> sv(num_nnz_features);
int32_t sparse_index = 0;
for (index_t i=0; i<dim; i++)
{
if (h_vec[i]!=0)
{
sv.features[sparse_index].entry = h_vec[i];
sv.features[sparse_index++].feat_index = i;
}
}
return sv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment