sklearn fit_transform() CopyWarning : A value is trying to be set on a copy of a slice from a DataFrame

huangapple go评论70阅读模式
英文:

sklearn fit_transform() CopyWarning : A value is trying to be set on a copy of a slice from a DataFrame

问题

我已阅读关于此错误的众多帖子。我无法找到解决我的问题的解决方案。以下是三次尝试解决此问题的结果。它们都会产生CopyWarning。

ordinal = OrdinalEncoder(categories=[['F','E','D','C','B','A']])

X['merchant_category'] = ordinal.fit_transform(X[['merchant_category']])
X.loc[:, 'merchant_category'] = ordinal.fit_transform(X[['merchant_category']])
X.loc[:, 'merchant_category'] = ordinal.fit_transform(X.loc[:, ['merchant_category']])

我尝试使用了上述三种不同的变体。

英文:

I have read numerous posts about this error. I wasn't able to produce a solution that solved my issue. Below are three attempts at solving the issue. They all produce CopyWarning.

ordinal = OrdinalEncoder(categories=[['F','E','D','C','B','A']])

X['merchant_category'] = ordinal.fit_transform(X[['merchant_category']])
X.loc[:, 'merchant_category'] = ordinal.fit_transform(X[['merchant_category']])
X.loc[:, 'merchant_category'] = ordinal.fit_transform(X.loc[:, ['merchant_category']])

I have tried to use the 3 different variation above.

答案1

得分: 0

你的问题是从你的代码这部分之前出现的。由于X是一个数据框的副本,你在这之前一定做了类似这样的操作:

X = X[['merchant_category']]

你可以在这之前的代码中纠正这个问题,或者从这里开始你的代码:

X = X.copy()

这将使X成为一个新的独立数据框。

英文:

Your issue is from before this part of your code. Since X is a copy of a dataframe you must have done something like this before:

X = X[['merchant_category']]

You can either correct this issue in your code before, or start your code here with:

X = X.copy()

This will set X as a new independent dataframe.

huangapple
  • 本文由 发表于 2023年2月7日 01:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定