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

Fix Julia category mapping #3305

Merged
merged 3 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### mlpack ?.?.?
###### ????-??-??
* Fix mapping of categorical data for Julia bindings (#3305).

### mlpack 4.0.0
###### 2022-10-23
Expand Down
27 changes: 20 additions & 7 deletions src/mlpack/bindings/julia/julia_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,14 @@ void SetParamMatWithInfo(void* params,
hasCategoricals = true;
}

arma::mat m(memptr, arma::uword(rows), arma::uword(cols), false, false);
arma::mat alias(memptr, arma::uword(rows), arma::uword(cols), false, false);
arma::mat m = pointsAreRows ? alias.t() : alias;

// Do we need to find how many categories we have?
if (hasCategoricals)
{
// Compute the maximum in each dimension.
arma::vec maxs;
if (pointsAreRows)
maxs = arma::max(m, 0).t();
else
maxs = arma::max(m, 1);
const arma::vec maxs = arma::max(m, 1);

for (size_t i = 0; i < d.Dimensionality(); ++i)
{
Expand All @@ -289,14 +286,19 @@ void SetParamMatWithInfo(void* params,
oss << j;
d.MapString<double>(oss.str(), i);
}

// In Julia we specify the categorical value from 1 to the number of
// categories, but in C++ we expect 0 to the number of categories minus
// one. (Just like the labels.)
m.row(i) -= 1.0;
}
}
}

std::get<0>(p->Get<std::tuple<data::DatasetInfo, arma::mat>>(
paramName)) = std::move(d);
std::get<1>(p->Get<std::tuple<data::DatasetInfo, arma::mat>>(
paramName)) = pointsAreRows ? m.t() : std::move(m);
paramName)) = std::move(m);
p->SetPassed(paramName);
}

Expand Down Expand Up @@ -683,8 +685,19 @@ double* GetParamMatWithInfoPtr(void* params, const char* paramName)

// Are we using preallocated memory? If so we have to handle this more
// carefully.
data::DatasetInfo& di = std::get<0>(
p->Get<std::tuple<data::DatasetInfo, arma::mat>>(paramName));
arma::mat& m = std::get<1>(
p->Get<std::tuple<data::DatasetInfo, arma::mat>>(paramName));

// Add 1 to any categorical columns, to map them back to the 1-indexed values
// expected in Julia.
for (size_t d = 0; d < di.Dimensionality(); ++d)
{
if (di.Type(d) == data::Datatype::categorical)
m.row(d) += 1.0;
}

if (m.n_elem <= arma::arma_config::mat_prealloc)
{
double* newMem = new double[m.n_elem];
Expand Down
10 changes: 7 additions & 3 deletions src/mlpack/bindings/julia/tests/test_julia_binding_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,15 @@ void BINDING_FUNCTION(util::Params& params, util::Timers& /* timers */)
{
if (ceil(m(i, c)) != m(i, c))
throw std::invalid_argument("non-integer value in categorical!");
else if (m(i, c) <= 0)
throw std::invalid_argument("negative/zero value in categorical!");
else if (size_t(m(i, c)) > di.NumMappings(i))
else if (m(i, c) < 0)
throw std::invalid_argument("negative value in categorical!");
else if (size_t(m(i, c)) >= di.NumMappings(i))
throw std::invalid_argument("value outside number of categories!");
}

// Since this goes back to Julia as a regular matrix, we have to
// manually convert the categorical features to be one-indexed.
m.row(i) += 1.0;
}
}

Expand Down