# How to recode categorical variable into numerical variable when using SVM or Neural Network

To use SVM or Neural Network it needs to transform (encode) categorical variables into numeric variables, the normal method in this case is to use 0-1 binary values with the k-th categorical value transformed to be (0,0,...,1,0,...0) (1 is on the k-th position). Is there other methods to do this, especially when there are a large number of categorical values(e.g.10000) such that the 0-1 representation will introduce a large number of additional dimensions(input units) in Neural Network which seems not quite desired or expected?

• Are you asking about general strategies or about some specific problem? – Denis Tarasov Feb 25 '15 at 13:58

In NLP, where words are typically encoded as 1-of-k, the use of word embeddings has emerged recently. The wikipedia page with its references is a good start.

The general idea is to learn a vectorial representation $x_i \in \mathbb{R}^n$ for each word $i$ where semantically similar words are close in that space. Consequently, the inputs are of size $n$ instead of the size of the vocabulary.

Maybe you can transfer that idea to your setting.

The 'standard' methods are: one-hot encoding (which you mentioned in the question). If there are too many possible categories, but you need 0-1 encoding, you can use hashing trick.

The other frequently used method is averaging answer over category: see picture from comment at kaggle.

You can use dummyVars in R, from the caret package. It will automatically create different columns based on number of levels. Afterwards, you can use cbind and attach it to you original data. Other options include model.matrix and sparse.model.matrix.

You can try binary encoding which is more compact and sometimes outperforms one-hot. You can implement categorical embedding in Keras, for example.