Map predictions back to IDs - Python Scikit Learn DecisionTreeClassifier

最后都变了- 提交于 2019-12-06 12:36:09
Grr

Not knowing how you made your split I would suggest just making sure the ID column is not included in your training data. Something like this perhaps:

X_train, X_test, y_train, y_test = test_train_split(df.ix[:, ~df.columns.isin(['ID', 'Response'])].values, df.Response)

That will split only the values from the DataFrame not in ID or Response for the X values, and split Response for the y values.

But you will still not be able to use the DecisionTreeClassifier with this data as it contains strings. You will need to convert any column with categorical data, i.e. TypeA and TypeB to a numerical representation. The best way to do this in my opinion for sklearn is with the LabelEncoder. Using this will convert the categorical string labels ['M', 'S'] into [1, 2] which can be implemented with the DecisionTreeClassifier. If you need an example take a look at Passing categorical data to sklearn decision tree.

Update

Per your comment I now understand that you need to map back to the ID. In this case you can leverage pandas to your advantage. Set ID as the index of your data and then do the split, that way you will retain the ID value for all of your train and test data. Let's assume your data are already in a pandas dataframe.

df = df.set_index('ID')
X_train, X_test, y_train, y_test = test_train_split(df.ix[:, ~df.columns.isin(['Response'])], df.Response)
print(X_train)
         LenA TypeA  LenB TypeB  Diff  Score
ID
345-678    87     M    70     M    17    0.7
234-567    46     S    49     S     3    0.9
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!