<div dir="ltr"><div dir="ltr">Hello everyone,<div><br></div><div>I am Sole, I started the conversation on <a href="https://feature-engine.readthedocs.io">feature engine</a>, a package I created for feature engineering.</div><div><br></div><div>Regarding the grouping of <u>rare /  infrequent</u> categories into an umbrella term like "Rare", "Other", etc, which Federico raised recently, I would like to provide some literature at the end of this email, that quotes the use of this procedure. These are a series of articles by the best solutions to the 2009 KDD annual competition, which were compiled into one "<a href="http://www.mtome.com/Publications/CiML/CiML-v3-book.pdf">book</a>", and I am sure you are aware of it already. I would also like to highlight that this is extremely common practice in the industry, not only to avoid overfitting, but also to handle unseen categories when models are deployed. It would be great to see this functionality added to both the OrdinalEncoder and the OneHotEncoder, with triggers on the representation of the label in the dataset (eg. percentage)</div><div><br></div><div>Pointing to the main quotes from <a href="http://www.mtome.com/Publications/CiML/CiML-v3-book.pdf">these articles</a>:</div><div><br></div><div><div>Page 4 of the summary and introductory article:</div><div>"For categorical variables, grouping of under-represented categories proved to be useful  to avoid overfitting. The winners of the fast and the slow track used similar strategies consisting in retaining the most populated categories and coarsely grouping the others in  an unsupervised way"</div><div><br></div><div>Page 23:</div><div>"Most of the learning algorithms we were planning to use do not handle categorical variables,  so we needed to recode them. This was done in a standard way, by generating indicator vari-  ables for the different values a categorical attribute could take. The only slightly non-standard  decision was to limit ourselves to encoding only the 10 most common values of each categorical  attribute, rather than all the values, in order to avoid an explosion in the number of features from  variables with a huge vocabulary"</div><div><br></div><div>Page 36:</div><div>"We consolidate the extremely low populated entries  (having fewer than 200 examples) with their neighbors to smooth out the outliers. Similarly, we  group some categorical variables which have a large number of entries (  >  1000 distinct values)  into 100 categories."</div><div><br></div><div>See bulletpoints in Page 47</div></div><div><br></div><div>I hope you find these useful.</div><div><br></div><div>Let me know if / how I can help.</div><div><br></div><div>Regards</div><div><br></div><div>Sole </div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 19 Apr 2019 at 17:54, federico vaggi <<a href="mailto:vaggi.federico@gmail.com">vaggi.federico@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi everyone,<div><br></div><div>I wanted to use the scikit-learn transformer API to clean up some messy data as input to a neural network.  One of the steps involves converting categorical variables (of very high cardinality) into integers for use in an embedding layer.</div><div><br></div><div>Unfortunately, I cannot quite use LabelEncoder to do solve this.  When dealing with categorical variables with very high cardinality, I found it useful in practice to have a threshold value for the frequency under which a variable ends up with the 'unk' or 'rare' label.  This same label would also end up applied at test time to entries that were not observed in the train set.</div><div><br></div><div>This is relatively straightforward to add to the existing label encoder code, but it breaks the contract slightly: if we encode some variables with a 'rare' label, then the transform operation is no longer a bijection.</div><div><br></div><div>Is this feature too niche for the main sklearn?  I saw there was a package (<a href="https://feature-engine.readthedocs.io/en/latest/RareLabelCategoricalEncoder.html" target="_blank">https://feature-engine.readthedocs.io/en/latest/RareLabelCategoricalEncoder.html</a>) that implemented a similar feature discussed in the mailing list.</div></div>
_______________________________________________<br>
scikit-learn mailing list<br>
<a href="mailto:scikit-learn@python.org" target="_blank">scikit-learn@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/scikit-learn" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/scikit-learn</a><br>
</blockquote></div>