title: Getting ValueError: y contains new labels when using scikit learn's LabelEncoder
body: <p>I have a series like:</p>
  
  <pre><code>df['ID'] = ['ABC123', 'IDF345', ...]
  </code></pre>
  
  <p>I'm using scikit's <code>LabelEncoder</code> to convert it to numerical values to be fed into the <code>RandomForestClassifier</code>.</p>
  
  <p>During the training, I'm doing as follows:</p>
  
  <pre><code>le_id = LabelEncoder()
  df['ID'] = le_id.fit_transform(df.ID) 
  </code></pre>
  
  <p>But, now for testing/prediction, when I pass in new data, I want to transform the 'ID' from this data based on <code>le_id</code> i.e., if same values are present then transform it according to the above label encoder, otherwise assign a new numerical value.</p>
  
  <p>In the test file, I was doing as follows:</p>
  
  <pre><code>new_df['ID'] = le_dpid.transform(new_df.ID)
  </code></pre>
  
  <p>But, I'm getting the following error: <code>ValueError: y contains new labels</code></p>
  
  <p>How do I fix this?? Thanks!</p>
  
  <p>UPDATE:</p>
  
  <p>So the task I have is to use the below (for example) as training data and predict the <code>'High', 'Mod', 'Low'</code> values for new BankNum, ID combinations. The model should learn the characteristics where a 'High' is given, where a 'Low' is given from the training dataset. For example, below a 'High' is given when there are multiple entries with same BankNum and different IDs.</p>
  
  <pre><code>df = 
  
  BankNum   | ID    | Labels
  
  0098-7772 | AB123 | High
  0098-7772 | ED245 | High
  0098-7772 | ED343 | High
  0870-7771 | ED200 | Mod
  0870-7771 | ED100 | Mod
  0098-2123 | GH564 | Low
  </code></pre>
  
  <p>And then predict it on something like:</p>
  
  <pre><code>BankNum   |  ID | 
  
  00982222  | AB999 | 
  00982222  | AB999 |
  00981111  | AB890 |
  </code></pre>
  
  <p>I'm doing something like this:</p>
  
  <pre><code>df['BankNum'] = df.BankNum.astype(np.float128)
  
      le_id = LabelEncoder()
      df['ID'] = le_id.fit_transform(df.ID)
  
  X_train, X_test, y_train, y_test = train_test_split(df[['BankNum', 'ID'], df.Labels, test_size=0.25, random_state=42)
      clf = RandomForestClassifier(random_state=42, n_estimators=140)
      clf.fit(X_train, y_train)
  </code></pre>