Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented maps in normalize_labels_impl.hpp #1780

Merged
merged 21 commits into from Apr 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 13 additions & 14 deletions src/mlpack/core/data/normalize_labels_impl.hpp
Expand Up @@ -39,32 +39,31 @@ void NormalizeLabels(const RowType& labelsIn,
// we'll resize it back down to its actual size.
mapping.set_size(labelsIn.n_elem);
labels.set_size(labelsIn.n_elem);
// Map for mapping labelIn to their label.
std::unordered_map<eT, size_t> labelMap;
size_t curLabel = 0;
for (size_t i = 0; i < labelsIn.n_elem; ++i)
{
bool found = false;
for (size_t j = 0; j < curLabel; ++j)
// If labelsIn[i] is already in the map, use the existing label.
if (labelMap.count(labelsIn[i]) > 0)
{
// Is the label already in the list of labels we have seen?
if (labelsIn[i] == mapping[j])
{
labels[i] = j;
found = true;
break;
}
labels[i] = labelMap[labelsIn[i]];
}

// Do we need to add this new label?
if (!found)
else
{
mapping[curLabel] = labelsIn[i];
// If labelsIn[i] not there then add it to map.
labelMap[labelsIn[i]] = curLabel;
labels[i] = curLabel;
++curLabel;
}
}

// Resize mapping back down to necessary size.
mapping.resize(curLabel);
// Mapping array created with encoded labels.
for (auto it = labelMap.begin(); it != labelMap.end(); ++it)
{
mapping[it->second] = it->first;
}
}

/**
Expand Down