title: How to use LabelBinarizer to one hot encode both train and test correctly
body: <p>Suppose I have a train set like this:</p>
  
  <pre><code>Name   | day
  ------------
  First  |  0
  Second |  1
  Third  |  1
  Forth  |  2
  </code></pre>
  
  <p>And a test set that does not contain all these names or days. Like so:</p>
  
  <pre><code>Name   | day
  ------------
  First  |  2
  Second |  1
  Forth  |  0
  </code></pre>
  
  <p>I have the following code to transform these columns in encoded features:</p>
  
  <pre><code>features_to_encode = ['Name', 'day']
  label_final = pd.DataFrame()
  
  for feature in features_to_encode:
      label_campaign = LabelBinarizer()
      label_results = label_campaign.fit_transform(df[feature])
      label_results = pd.DataFrame(label_results, columns=label_campaign.classes_)
      label_final = pd.concat([label_final, label_results], axis=1)
  
  df_encoded = label_final.join(df)
  </code></pre>
  
  <p>To produce the following output on train (<strong>this works fine</strong>):</p>
  
  <pre><code>First  | Second  | Third  | Forth | 0 | 1 | 2 | 
  -----------------------------------------------
    1    |    0    |   0    |   0   | 1 | 0 | 0 |
    0    |    1    |   0    |   0   | 0 | 1 | 0 |
    0    |    0    |   1    |   0   | 0 | 1 | 0 |
    0    |    0    |   0    |   1   | 0 | 0 | 1 |
  </code></pre>
  
  <p>However, when I run this on test data (new data), I get <strong>mismatching features if the test data does not contain exactly all the same Names and days as train data</strong>. So if I run similar code on this test sample, I would get:</p>
  
  <pre><code>First  | Second  | Forth | 0 | 1 | 2 | 
  --------------------------------------
    1    |    0    |   0   | 0 | 0 | 1 |
    0    |    1    |   0   | 0 | 1 | 0 |
    0    |    0    |   1   | 1 | 0 | 0 |
  </code></pre>
  
  <p>What can I do to preserve the same transformation from train data and apply it correctly to test data, resulting in this <strong>desired output</strong>:</p>
  
  <pre><code>First  | Second  | Third  | Forth | 0 | 1 | 2 | 
  -----------------------------------------------
    1    |    0    |   0    |   0   | 0 | 0 | 1 |
    0    |    1    |   0    |   0   | 0 | 1 | 0 |
    0    |    0    |   0    |   1   | 1 | 0 | 0 |
  </code></pre>
  
  <p>I have already tried adding a dict to catch the fit_transform results, but I am not sure if this works or what to do with it afterwards:</p>
  
  <pre><code>features_to_encode = ['Name', 'day']
  label_final = pd.DataFrame()
  
  labels = {}--------------------------------------------------------------------&gt; TRIED THIS
  for feature in features_to_encode:
      label_campaign = LabelBinarizer()
      label_results = label_campaign.fit_transform(df[feature])
      labels[feature] = label_results--------------------------------------------&gt; WITH THIS
      label_results = pd.DataFrame(label_results, columns=label_campaign.classes_)
      label_final = pd.concat([label_final, label_results], axis=1)
  
  df_encoded = label_final.join(df)
  </code></pre>
  
  <p>Any help is appreciated. Thanks =)</p>