16
$\begingroup$

For unsupervised anomaly detection / fraud analytics on credit card data (where I don't have labeled fraudulent cases), there are a lot of variables to consider. The data is of mixed type with continuous/numerical variables (e.g. USD amount spent) as well as categorical variables (e.g. account number).

What is the most suitable way of including categorical variables that have a very large number of unique classes? My thoughts so far:

  • Label Encoding (scikit-learn): i.e. mapping integers to classes. While it returns a nice single encoded feature column, it imposes a false sense of ordinal relationship (e.g. 135 > 72).
  • One Hot / Dummy Encoding (scikit-learn): i.e. expanding the categorical feature into lots of dummy columns taking values in {0,1}. This is infeasible for categorical features having e.g. >10,000 unique values. I understand that models will struggle with the sparse and large data.

What other (more advanced?) suitable methods are there to include large categorical feature columns? Is it possible to still use One Hot Encoding with some tricks? I read about bin counting (Microsoft blog) though I haven't found any applications related to intrusion detection / fraud analytics.

P.S.: In my view, this problem seems very similar to encoding an IP-address feature column when dealing with unsupervised intrusion detection.

$\endgroup$
3
  • $\begingroup$ See the good advice in stats.stackexchange.com/questions/146907/… $\endgroup$ Jul 1, 2019 at 16:08
  • $\begingroup$ linear models have no problems with sparse data (nb bag of words models, where the words/ bigrams/trigrams are definitely in the 1000's) basically you just needs a model that supports sparse data (eg glmnet, vowpalwabbit etc). I have found sklearn is not so good for handling sparse data ( memory hungry). vowpalwabbit does hash coding, but this is effectively one hot coding after hashing ( so you will typically have 10000 unique values after hashing too). I have used (python-glmnet) with a dataset of (548823, 45544) - which took 2 hours. [dummy variables and their interactions etc] $\endgroup$
    – seanv507
    Jul 1, 2019 at 16:19
  • 1
    $\begingroup$ as @zhubarb mentioned, why would you even want to use account number as a feature? maybe if you explain that then the apporpriate encoding will become more obvious $\endgroup$
    – seanv507
    Jul 1, 2019 at 16:20

3 Answers 3

14
$\begingroup$

This link provides a very good summary and should be helpful. As you allude to, label-encoding should not be used for nominal variables at it introduces an artificial ordinality. Hashing is a potential alternative that is particularity suitable for features that have high cardinality.

You can also use a distributed representation, which has become very popular in the deep learning community. The most common example given for distributed representation is word embeddings in NLP. That is not to say you cannot utilise them in encoding other categorical features. Here is an example.

Finally, account number would not be a wise input as it is more a unique identifier rather than a generalisable (account) feature.

$\endgroup$
0
1
$\begingroup$

This might help Quantile Encoder: Tackling High Cardinality Categorical Features in Regression Problems: https://link.springer.com/chapter/10.1007%2F978-3-030-85529-1_14

The most well-known encoding for categorical features with low cardinality is One Hot Encoding [1]. This produces orthogonal and equidistant vectors for each category. However, when dealing with high cardinality categorical features, one hot encoding suffers from several shortcomings [20]: (a) the dimension of the input space increases with the cardinality of the encoded variable, (b) the created features are sparse - in many cases, most of the encoded vectors hardly appear in the data -, and (c) One Hot Encoding does not handle new and unseen categories.

An alternative encoding technique is Label/Ordinal Encoding [3] which uses a single column of integers to represent the different categorical values. These are assumed to have no true order and integers are selected at random. This encoding handles the problem of the high dimensional encoding found in One Hot Encoding but imposes an artificial order of the categories. This makes it harder for the model to extract meaningful information. For example, when using a linear model, this effect prevents the algorithm from assigning a high coefficient to this feature.

Alternatively, Target Encoding (or mean encoding) [15] works as an effective solution to overcome the issue of high cardinality. In target encoding, categorical features are replaced with the mean target value of each respective category. With this technique, the high cardinality problem is handled and categories are ordered allowing for easy extraction of the information and model simplification. The main drawback of Target Encoding appears when categories with few (even only one) samples are replaced by values close to the desired target. This biases the model to over-trust the target encoded feature and makes it prone to overfitting.

It is not your exact case but the introduction has a bit of lit review that can be helpful and the pitfalls of some of this techniques.

Also, in arxiv https://arxiv.org/abs/2105.13783

$\endgroup$
0
0
$\begingroup$

Zhubarb had a very nice answer. I just want to provide more details on embedding and hashing and add one common approach binning.

Starting with the binning, this is a very common approach used in many fields, the key idea is many data follows 80-20 rules, that even we have a feature with many values but most of the data will concentred in few values. One simple example is nationality. There are many nations in the world, but if we want to build a statistical model using nationality, we will not use / encoding all of them (there are many reasons behind this, but generally, we may have overfitting if we use all of them). Instead we will pick top nationalities, and bin others int Others category. Note that this approach is also widely used in Deep Learning, where a word will have OOV(out of vocabulary) label when it is in Other category. This is an interesting paper to read: How Large a Vocabulary Does Text Classification Need?, In this paper, the largest vocabulary size is 60K.

Embedding is a very nice idea from Deep Learning and NLP. Suppose we are building a model that vocabulary size is 60K, we do not want to do one hot embedding because the vector is very sparse and the distance between vectors are not meaningful. For example, if we encode the word cat into [0,0,....,1,0,0], a lot of space will be wasted (in real word if we use sparse vector instead of dense vector to store the data, it will still be OK, but sparse vector have its own computational challenges.). And the distance between the word "cat" and say "dog", will as same as the distance between cat and say "keyboard".

Embedding uses dense vector to do the encoding, and the general idea is the distance between the dense vectors will have meanings. For example, the distance between "cat" and "dog", will be much smaller than the distance between "cat" and "keyboard".

Hashing is another interesting idea, an example can be found in sklearn documentation here. The idea is we use hash functions to produce a fixed number of features. This approach will apply a hash function to the features to determine their column index in data / design matrices directly. The result is increased speed and reduced memory usage, at the expense of inspectability; the hasher does not remember what the input features looked like and has no inverse_transform method. In addition, there will be collisions if we set number of the output features small. (for example, the this trick make not be able to differentiate the word "cat" and "keyboard" as both of them mapped into same column index.)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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