billylanchantin

billylanchantin

I have a dataset with categorical variables, e.g.:

alias Explorer.DataFrame, as: DF

df = DF.new(%{
  age: [1, 5, 3],
  animal: ["dog", "cat", "dog"],       # categorical
  color: ["brown", "black", "brindle"] # categorical
})

I currently have the variables one-hot encoded:

df_one_hot = DF.new(%{
  age: [1, 5, 3],
  animal_cat_1_of_2: [1, 0, 1], # animal == "dog"
  animal_cat_2_of_2: [0, 1, 0], # animal == "cat"
  color_cat_1_of_3: [1, 0, 0], # color == "brown"
  color_cat_2_of_3: [0, 1, 0], # color == "black"
  color_cat_3_of_3: [0, 0, 1], # color == "brindle"
})

Neural nets generally do well with that encoding. Tree-based models, however, may benefit from the original encoding or ordinal encoding. So while experimenting with different models, I found myself needing to undo the one-hot encoding.

I came up with a solution which I’ll post below. But I wanted to see how others would approach the problem. This is a somewhat computationally-intensive operation, and I worry that I’m not taking full advantage of Explorer. In particular, this looked like it may be a job for across, but I couldn’t make it work.

Showing Posts 1 to 3

billylanchantin

billylanchantin OP

My approach for converting to an ordinal encoding:

require DF

# Build up a map of %{original_col => %{category_col => category_num}}
categorical_cols =
  df_one_hot.names
  |> Enum.map(&Regex.run(~r/(.+)_cat_(\d+)_of_\d+/, &1))
  |> Enum.reject(&is_nil/1)
  |> Enum.group_by(
    fn [_, group, _] ->
      group
    end,
    fn [col, _, num_string] ->
      {num, _} = Integer.parse(num_string)
      {col, num}
    end
  )
  |> Map.new(fn {group, col_num_pairs} -> {group, Map.new(col_num_pairs)} end)

# Ordinal encode the data by repeatedly adding `num * col` to a column of all 0s.
df_ordinal =
  Enum.reduce(categorical_cols, df_one_hot, fn {group, col_to_num}, outer ->
    col_to_num
    |> Enum.reduce(DF.put(outer, group, [0]), fn {col_name, num}, inner ->
      DF.mutate(inner, [{^group, col(^group) + col(^col_name) * ^num}])
    end)
    |> DF.discard(Map.keys(col_to_num))
  end)
josevalim

josevalim

Creator of Elixir

If you can hardcode the fields, then you can do:

require Explorer.DataFrame, as: DF

DF.mutate(df_one_hot,
  animal: cond do
    animal_cat_1_of_2 == 1 -> "dog"
    animal_cat_2_of_2 == 1 -> "cat"
  end,
  color: cond do
    color_cat_1_of_3 == 1 -> "brown"
    color_cat_2_of_3 == 1 -> "black"
    color_cat_3_of_3 == 1 -> "brindle"
  end
)

If you cannot, then you can port your approach to mutate_with. mutate_with gives you access to the columns and allow you to dynamically build a query based on the field. Then use Series.select to build the cond. Scroll down to find the answer (I added some padding in case you want to try it out by yourself before seeing the solution):


















categorical_cols =
  df_one_hot.names
  |> Enum.map(&Regex.run(~r/(.+)_cat_(\d+)_of_\d+/, &1))
  |> Enum.reject(&is_nil/1)
  |> Enum.group_by(
    fn [_, group, _] -> group end,
    fn [col, _, num] -> {col, String.to_integer(num)} end
  )
  |> Map.new(fn {group, col_num_pairs} -> {group, col_num_pairs} end)

DF.mutate_with(df_one_hot, fn df ->
  Enum.map(categorical_cols, fn {group, col_to_num} ->
    expr = Enum.reduce(col_to_num, -1, fn {col_name, num}, acc ->
      equal = Explorer.Series.equal(df[col_name], 1)
      Explorer.Series.select(equal, num, acc)
    end)

    {group, expr}
  end)
end)
billylanchantin

billylanchantin OP

Thanks! That made a huge improvement.

Quick benchmark on a subset of the data (cols: 203, rows: 65,917):

{billy_us, billy_df} = :timer.tc(fn ->
  # ...
end)

{jose_us, jose_df} = :timer.tc(fn ->
  # ...
  |> DF.discard(#... to make the comparison fair
end)

billy_df.names == jose_df.names  #=> true
[billy: billy_us, jose: jose_us] #=> [billy: 26438, jose: 4680]

What’s more, your solution works on lazy frames. I needed to call DF.collect my dataframe first.

This particular dataset had ~80 columns one-hot columns. Hardcoding is do-able, but I’d prefer to avoid it if possible.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews