【问题标题】:Categorical Data with tpot带有 tpot 的分类数据
【发布时间】:2018-09-24 02:39:46
【问题描述】:

我正在尝试将 tpot 与我在 pandas 数据帧中的输入一起使用。我不断收到错误:

TypeError: 输入类型不支持 ufunc 'isnan',根据转换规则 ''safe'' 无法安全地将输入强制转换为任何支持的类型

我相信这个错误是由于 isnan 无法处理我的数据结构,但我不确定如何以不同的方式格式化它。我有分类和连续输入以及连续输出的组合。以下是具有相似数据的代码示例:

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()

我是否必须以某种方式转换我的分类数据? dataframe.values 和 dataframe.as_matrix 给我的对象也会给我一个错误。

【问题讨论】:

    标签: python tpot


    【解决方案1】:

    没错 - 您需要转换分类值。 TPOT 假设所有数据都采用 scikit-learn 兼容格式,这意味着所有数据都是数字的。我们最近才添加了对缺失值的支持,尽管大多数 scikit-learn 算法也不接受缺失值的数据。

    我在下面修改了您的示例,以展示如何使用 pandas 将分类值转换为数值。

    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)
    

    【讨论】:

    • 非常感谢,兰迪!这是有道理的!
    • 我运行 tpot 几个小时,然后提前停止,这次我收到了类似的关于 testing_features 而不是 features 的警告。知道发生了什么吗?这是完整的警告:第 832 行,在 score if np.any(np.isnan(testing_features)): TypeError: ufunc 'isnan' not supported for the input types,并且输入不能安全地强制转换为任何支持的类型根据强制转换规则“安全”
    • scikit-learn 似乎可以处理布尔特征,但 TPOT 不能。
    • @Randy - 我看到其中一种转换是 onehotencoder。这不是一次性对数据进行编码,所以仍然有必要吗?
    • @JSpen TPOT 假定您已经以适当的方式对数据进行了编码。 TPOT 在某些配置中确实使用 one-hot 编码器,但并非总是如此。最好自己将数据编码成数字格式,这样就不会遇到任何问题。
    猜你喜欢
    • 2018-07-15
    • 2021-07-27
    • 2012-11-17
    • 2017-08-06
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多