14

I've been learning to work with neural networks as a hobby project, but am at a complete loss with how to handle categorical data. I read the article http://visualstudiomagazine.com/articles/2013/07/01/neural-network-data-normalization-and-encoding.aspx, which explains normalization of the input data and explains how to preprocess categorical data using effects encoding. I understand the concept of breaking the categories into vectors, but have no idea how to actually implement this.

For example, if I'm using countries as categorical data (e.g. Finland, Thailand, etc), would I process the resulting vector into a single number to be fed to a single input, or would I have a separate input for each component of the vector? Under the latter, if there are 196 different countries, that would mean I would need 196 different inputs just to process this particular piece of data. If a lot of different categorical data is being fed to the network, I can see this becoming really unwieldy very fast.

Is there something I'm missing? How exactly is categorical data mapped to neuron inputs?

2
  • Possible duplicate of Clarification on a Neural Net that plays Snake
    – devinbost
    Feb 15, 2017 at 20:32
  • 5
    @devinbost this is like the 10th question I have seen in the past 5 minutes on neural networks that you have flagged as a duplicate of the Clarification on a Neural Net that plays Snake question, which is a question with a -5 score but one which you happen to have written an answer for. I find it difficult to believe that all these questions are variants of a question with such a specific title. I don't pretend to understand your motives for doing this but please explain, or stop. Feb 15, 2017 at 21:08

1 Answer 1

27

Neural network inputs

As a rule of thumb: different classes and categories should have their own input signals.


Why you can't encode it with a single input

Since a neural network acts upon the input values through activation functions, a higher input value will result in a higher activation input.

A higher input value will make the neuron more likely to fire.

As long as you don't want to tell the network that Thailand is "better" than Finland then you may not encode the country input signal as InputValue(Finland) = 24, InputValue(Thailand) = 140.

How not to format the input


How it should be encoded

Each country deserves its own input signal so that they contribute equally to activating the neurons. enter image description here

3
  • What to do, if new input will be added (for example, new country)?
    – Drakmail
    Sep 30, 2014 at 10:23
  • 2
    Great answer. For those interested, this technique is called One-Hot Encoding. There are several tools, like the ones from Scikit or Keras built in method, that handle these sort of categorical data.
    – DarkCygnus
    Sep 28, 2017 at 17:28
  • One hot encoding doesn't handle the "new" input though. Alternatively, one can create their own OHE algorithm with an additional "other" column, this way when a new input is added, it can go into the "other" column until the model is updated/trained with the new inputs.
    – Lisle
    May 8, 2018 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.