title: SkLearn - Why LabelEncoder().fit only to training data
body: <p>I may be missing something but after following for quite a long time now the suggestion (of some senior data scientists) to <a href="http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html" rel="nofollow noreferrer"><code>LabelEncoder().fit</code></a> only to training data and not also to test data then I start to think why is this really necessary.</p>
  
  <p>Specifically, at <code>SkLearn</code> if I want to <code>LabelEncoder().fit</code> only to training data then there are two different scenarios:</p>
  
  <ol>
  <li><p>The test set has some new labels in relation to the training set. For example, the test set has only the labels <code>['USA', 'UK']</code> while the test set has the labels <code>['USA', 'UK', 'France']</code>. Then, as it has been reported elsewhere (e.g. <a href="https://stackoverflow.com/questions/46288517/getting-valueerror-y-contains-new-labels-when-using-scikit-learns-labelencoder">Getting ValueError: y contains new labels when using scikit learn&#39;s LabelEncoder</a>), you are getting an error if you try to transform the test set according to this <code>LabelEncoder()</code> because exactly it encounters a new label. </p></li>
  <li><p>The test set has the same labels as the training set. For example, both the training and the test set have the labels <code>['USA', 'UK', 'France']</code>. However, then <code>LabelEncoder().fit</code> only to training data is essentially redundant since  the test set have the same known values as the training set.</p></li>
  </ol>
  
  <p><strong>Hence, what is the point of <code>LabelEncoder().fit</code> only to training data and then <code>LabelEncoder().tranform</code> both the training and the test data if at case (1) this throws an error and if at case (2) it is redundant?</strong></p>
  
  <p><em>Let my clarify that the (pretty knowledgeable) senior data scientists whom I have seen to <code>LabelEncoder().fit</code> only to training data, they had justified this by saying that  the test set should be entirely new to even the simplest model like an encoder and it should not be mixed at any fitting with the training data.  They did not mention anything about any production or out-of-vocabulary purposes.</em></p>