title: XGBoost: Feature Names Mismatch
body: <p>I'm struggling big-time to get my XGBoost model to predict an article's engagement time from its text.  First, I get a dataframe representing the features I extracted from the article like this:</p>
  
  <pre><code>article_features = pd.concat([tfidf_df, numeric_df_normalized], axis=1)
  </code></pre>
  
  <p>I then train my model and get the relevant correct columns (features):</p>
  
  <pre><code>with open('correct_columns') as fp:
          correct_columns = pickle.load(fp)
  </code></pre>
  
  <p>Then I go through all of the required features and set them to <code>0.0</code> if they're not already in <code>article_features</code>:</p>
  
  <pre><code>for col in correct_columns:
          if col not in article_features.columns:
              article_features[col] = 0.0
  </code></pre>
  
  <p>Finally, I delete features that were extracted from this article that don't exist in the training data:    </p>
  
  <pre><code>for col in article_features:
      if col not in correct_columns:
          del article_features[col]
  </code></pre>
  
  <p>So now <code>article_features</code> has the correct number of features.  I try to run:</p>
  
  <pre><code>model.predict(article_features)
  </code></pre>
  
  <p>And I get:</p>
  
  <pre><code>ValueError: feature_names mismatch:...
  </code></pre>
  
  <p>So I Google around and try converting my dataframe to :</p>
  
  <pre><code>model.predict(article_features.as_matrix())
  </code></pre>
  
  <p>But I get the same error.</p>
  
  <p>I was then worried about order of columns in <code>article_features</code> not being the same as <code>correct_columns</code> so I did:</p>
  
  <pre><code>article_features.sort_index(axis=1, inplace=True)
  </code></pre>
  
  <p>But got the same error.</p>
  
  <p>Any idea how to fix?</p>
  
  <p>Thanks!</p>