Categorical Data with tpot

寵の児 提交于 2019-12-07 08:53:54

问题


I'm trying to use tpot with my inputs in pandas dataframes. I keep getting the error:

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I believe this error is from isnan not being able to handle my data structure, but I'm unsure how to format it differently. I have a combination of categorical and continuous inputs and continuous outputs. Here's an example of code with similar data:

train_x=[[1,2,3],['test1','test2','test3'],[56.2,4.5,3.4]]
train_y=[[3,6,7]]
from tpot import TPOTRegressor

tpot=TPOTRegressor()

Do I have to convert my categorical data somehow? dataframe.values and dataframe.as_matrix give me objects that also give me an error.


回答1:


That's right - you need to convert your categorical values. TPOT assumes that all data will come in a scikit-learn compatible format, which entails that all of the data is numeric. We only recently added support for missing values, though most scikit-learn algorithms do not accept data with missing values either.

I reworked your example below to show how pandas can be used to convert the categorical values to numerical values.

import pandas as pd
from tpot import TPOTRegressor

train_x = pd.DataFrame()
train_x['a'] = [1,2,3,4]
train_x['b'] = ['test1','test2','test3','test4']
train_x['c'] = [56.2,4.5,3.4,6.7]

# This line one-hot encodes the categorical variables
train_x = pd.get_dummies(train_x).values
# Print train_x out to understand what one-hot encoding entails
print(train_x)

train_y = [3,6,7,9]

my_tpot = TPOTRegressor(cv=2)
my_tpot.fit(train_x, train_y)


来源:https://stackoverflow.com/questions/49822035/categorical-data-with-tpot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!