feature_names must be unique - Xgboost

user2728024 picture user2728024 · Apr 24, 2017 · Viewed 10.7k times · Source

I am running the xgboost model for a very sparse matrix.

I am getting this error. ValueError: feature_names must be unique

How can I deal with this?

This is my code.

  yprob = bst.predict(xgb.DMatrix(test_df))[:,1]

Answer

andrew_reece picture andrew_reece · Apr 24, 2017

According the the xgboost source code documentation, this error only occurs in one place - in a DMatrix internal function. Here's the source code excerpt:

if len(feature_names) != len(set(feature_names)):
    raise ValueError('feature_names must be unique')

So, the error text is pretty literal here; your test_df has at least one duplicate feature/column name.

You've tagged pandas on this post; that suggests test_df is a Pandas DataFrame. In this case, DMatrix literally runs df.columns to extract feature_names. Check your test_df for repeat column names, remove or rename them, and then try DMatrix() again.