使用sciket学习Pipelin设置copywarning

2024-05-13 21:16:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用非常简单的代码:

simplePipe = Pipeline([
('string_fix', StringFix()),
])
class StringFix(BaseEstimator, TransformerMixin):
    def __init__(self):
        pass

    def fit(self, X, y = None):
        return self

    def transform(self, X, y = None):
        print('Removing NANs.')
        # next 2 lines will throw the SettingWithCopyWarning 
        X.loc[:, 'f1'] = 'testing'
        X.loc[:, 'f1'].replace(np.nan, '', inplace = True)
        # this line doesn't throw the warning but it is expected not 
        # modifying the dataframe.
        X.loc[:, 'f1'].replace(np.nan, '', inplace = False)
        return X

有趣的是(或不是),当我执行以下操作时:

trainSetDF = simplePipe.fit_transform(inputDF[:4])

它发出警告

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

但当我直接在scikit学习管道之外执行时就不行了:

inputDF.loc[0:4, 'f1'] = 'testing'

我是不是漏了什么?我花了相当长的时间试图理解why this warning is。现在我明白了,我修复了一些代码,但是不管我在管道中做什么,我总是得到这个警告。管道本身在做我不想做的事吗?即使我删除了“return X”,它可以做一些我不知道的事情,比如复制一个数组或者别的什么,我仍然得到这个警告。你知道吗

你知道我做错了什么吗?你知道吗


Tags: the代码selfnone警告return管道is