# Machine Learning in Elixir: Chapter 1, Poor Accuracy when following the code in book

**URL:** <https://forum.devtalk.com/t/machine-learning-in-elixir-chapter-1-poor-accuracy-when-following-the-code-in-book/119568>\
**Category:** PragProg Customers\
**Tags:** book-machine-learning-in-elixir, errata\
**Created:** [8 August 2023 08:24 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-chapter-1-poor-accuracy-when-following-the-code-in-book/119568 "2023-08-08T08:24:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![shawn\_leong](https://forum.devtalk.com/user_avatar/forum.devtalk.com/shawn_leong/32/5311_2.png) [@shawn\_leong](https://forum.devtalk.com/u/shawn_leong)\
**Post date:** [8 August 2023 08:24 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-chapter-1-poor-accuracy-when-following-the-code-in-book/119568/1 "2023-08-08T08:24:37Z")

</div>

Hey @seanmor5,

I’m having great training accuracy but poor evaluation accuracy for the example in Chapter 1 when following the code in the book.

```auto
Epoch: 9, Batch: 450, accuracy: 0.9750040 loss: 0.2519934

```

```auto
Batch: 0, accuracy: 0.0000000
%{
  0 => %{
    "accuracy" => #Nx.Tensor<
      f32
      0.0
    >
  }
}

```

After some trouble, I realised that the book’s code diverges from the [accompanying livebooks](https://media.pragprog.com/titles/smelixir/code/smelixir-code.zip) from PragProg. Here’s the code in question.

Book, Page 16:

```auto
...
train_categories =
  train_df["species"]
  |> Explorer.Series.cast(:category)

y_train =
  train_categories
  |> Nx.stack(axis: -1)
  |> Nx.equal(Nx.iota({1, 3}, axis: -1))

x_test = Nx.stack(test_df[feature_columns], axis: 1)

test_categories =
  test_df["species"]
  |> Explorer.Series.cast(:category)

y_test =
  test_categories
  |> Nx.stack(axis: -1)
  |> Nx.equal(Nx.iota({1, 3}, axis: -1))

```

Accompanying Livebook:

```auto
...
y_train =
  train_df
  |> DF.pull(label_column)
  |> Explorer.Series.to_list()
  |> Enum.map(fn
    "Iris-setosa" -> 0
    "Iris-versicolor" -> 1
    "Iris-virginica" -> 2
  end)
  |> Nx.tensor(type: :u8)
  |> Nx.new_axis(-1)
  |> Nx.equal(Nx.iota({1, 3}, axis: -1))
...
y_test =
  test_df
  |> DF.pull(label_column)
  |> Explorer.Series.to_list()
  |> Enum.map(fn
    "Iris-setosa" -> 0
    "Iris-versicolor" -> 1
    "Iris-virginica" -> 2
  end)
  |> Nx.tensor(type: :u8)
  |> Nx.new_axis(-1)
  |> Nx.equal(Nx.iota({1, 3}, axis: -1))

```

Seems like it had to do with the ordering of the categories and how it maps when doing the one-hot encoding.

There’s a [thread](https://elixirforum.com/t/book-question-machine-learning-in-elixir-poor-accuracy-for-chapter-1s-example/57185) started on Elixirforum where @grossvogel more succintly explains what’s happening & with some alternative code.

---

<div class="post-metadata">

**Author:** ![seanmor5](https://forum.devtalk.com/user_avatar/forum.devtalk.com/seanmor5/32/1169_2.png) [@seanmor5](https://forum.devtalk.com/u/seanmor5)\
**Post date:** [28 August 2023 17:50 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-chapter-1-poor-accuracy-when-following-the-code-in-book/119568/2 "2023-08-28T17:50:56Z")

</div>

Thanks for pointing this out, I’ve updated the code and language for the next beta. The approach is to cast the entire dataframe to a categorical variable before splitting/shuffling, this ensures that the one-hot encoding across train/test sets are consistent 🙂
