title: LabelEncoder with sklearn , transform and inverse single relationship between values?
body: <p>Hello and thank you in advance for any tip or advice.</p>
  
  <p>I am working in Python 3.6 with sklearn and DecisionTree Classifier. I use label encoder as my Pandas Dataframe has 4 columns and some are strings.</p>
  
  <pre><code>      Origin Duration   Origin Octave  Origin Pitch   Next Pitch
  0          quarter           3          B                    G   
  1          quarter           4          D                    D   
  2          quarter           4          A                    D   
  3             16th           4          A                    D 
  </code></pre>
  
  <p>Before splitting the dataframe into into training and test sets, I use label encode and a dict to enconde it (tip I read here in StackOverflow, thanks Jon!). The resulting encoded dataframe looks like:</p>
  
  <pre><code>from collections import defaultdict
  
  d = defaultdict(preprocessing.LabelEncoder)
  encoded = scoredf.apply(lambda x: d[x.name].fit_transform(x))
  
  
        Origin Duration   Origin Octave  Origin Pitch   Next Pitch
  0                 5            1           1                0                
  1                 5            2           2                4                 
  2                 5            2           0                4                 
  3                 0            2           0                4                 
  </code></pre>
  
  <p>After training and testing the Decision Tree Classifier, I want to pass another dataframe for the model, to obtain predictions.
  For example, I want to pass the following DataFrame XX to my model, to obtain the predicition:</p>
  
  <pre><code>print(XX)
  Origin Duration Origin Octave Origin Pitch
  0            zero             5            G
  </code></pre>
  
  <p>I enconded it using the same dict (d = defaultdict(preprocessing.LabelEncoder)), Labelenconding used for the original Dataframe, I use only <strong>"transform"</strong> as I supposed everything is "fitted":</p>
  
  <pre><code>XXX = XX.apply(lambda x: d[x.name].transform(x))
  </code></pre>
  
  <p>However the results are incosintents in terms of encoding. I see it gets encoded like: </p>
  
  <pre><code>    print(XXX)
             Origin Duration  Origin Octave  Origin Pitch
      0                6              3            10
  </code></pre>
  
  <p>However, following the encoding of the original dataframe used for train and test the model, the resulting data frame should be:</p>
  
  <pre><code>            Origin Duration  Origin Octave  Origin Pitch
  0                6              3            6
  </code></pre>
  
  <p>So the question is, how to keep the same and consistent LabelEnconding I used for training and testing the  model when I actually used the model to predict afterwards,
  This is: How to encode the input dataframe to get predicitions, using the same labels as the one used for training and testing the model?</p>
  
  <p>Thanks!</p>