【问题标题】:Error when using numpy to encode categorical features of dataset使用 numpy 对数据集的分类特征进行编码时出错
【发布时间】:2017-09-21 08:04:33
【问题描述】:

我使用以下函数对我的数据集的分类特征进行编码(它有 27 个特征,其中 11 个是分类特征):

from sklearn import preprocessing
def features_encoding(data):
    columnsToEncode = list(data.select_dtypes(include=['category', 'object']))
    le = preprocessing.LabelEncoder()
    for feature in columnsToEncode:
        try:
            data[feature] = le.fit_transform(data[feature])
        except:
            continue
    return data

但我收到此错误:

FutureWarning: numpy not_equal will not check object identity in the future. The comparison did not return the same result as suggested by the identity (`is`)) and will change.
      flag = np.concatenate(([True], aux[1:] != aux[:-1]))

我不明白这个错误。请问,有人可以解释它是什么以及如何解决它吗?

【问题讨论】:

    标签: pandas numpy scikit-learn feature-extraction


    【解决方案1】:

    这几乎可以肯定是由于dtype=object 数组中不止一次出现np.nan 而导致的,该数组被传递到np.unique

    这可能有助于澄清发生了什么:

    >>> np.nan is np.nan
    True
    >>> np.nan == np.nan
    False
    >>> np.array([np.nan], dtype=object) == np.array([np.nan], dtype=object)
    FutureWarning: numpy equal will not check object identity in the future. The comparison did not return the same result as suggested by the identity (`is`)) and will change.
    array([ True], dtype=bool)
    

    所以当比较dtype=object 的两个数组时,numpy 会检查比较函数的返回值是否为False,当两个被比较的对象完全相同时。因为现在它假设所有对象的比较都等于自己,但将来会同时改变。

    总而言之,这只是一个警告,所以你可以忽略它,至少现在是这样......

    【讨论】:

    • 非常感谢。感谢您的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2021-03-26
    • 2021-08-20
    • 2019-05-21
    • 2020-03-18
    • 1970-01-01
    相关资源
    最近更新 更多