使用 numpy 编码数据集的分类特征时出错

Error when using numpy to encode categorical features of dataset

我使用以下函数对我的数据集的分类特征进行编码(它有 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]))

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

这几乎可以肯定是由于在传递到 np.unique.

dtype=object 数组中不止一次 np.nan 引起的

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

>>> 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 检查比较函数的 return 是否是 False 当两个被比较的对象完全相同时。因为现在它假设所有对象都比较等于自己,但将来会同时改变。

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