BeM*_*ase 2 python warnings copy view pandas
我对SettingWithCopyWarning有基本的了解,但是我无法弄清楚为什么我收到此特定情况的警告。
我正在遵循https://github.com/ageron/handson-ml/blob/master/02_end_to_end_machine_learning_project.ipynb中的代码
当我运行以下代码(使用.loc)时,没有得到SettingWithCopyWarning
但是,如果我改为使用.iloc运行代码,则会收到警告。
有人可以帮我理解吗?
from sklearn.model_selection import StratifiedShuffleSplit
split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
for train_index, test_index in split.split(housing, housing["income_cat"]):
strat_train_set = housing.loc[train_index]
strat_test_set = housing.loc[test_index]
for set_ in (strat_train_set, strat_test_set):
set_.drop("income_cat", axis=1, inplace=True)
Run Code Online (Sandbox Code Playgroud)
The issue here is not because of indexing, iloc and loc would work the same way for you here. The problem is in set_.drop("income_cat", axis=1, inplace=True). It looks like there's a weak reference between the set_ data frame and the strat_train_set and strat_test_set.
for set_ in (strat_train_set, strat_test_set):
print(set_._is_copy)
Run Code Online (Sandbox Code Playgroud)
With this you get:
<weakref at 0x128b30598; to 'DataFrame' at 0x128b355c0>
<weakref at 0x128b30598; to 'DataFrame' at 0x128b355c0>
Run Code Online (Sandbox Code Playgroud)
This could lead to SettingWithCopyWarning as it's trying to transform the copy of the data frame and applying those change to the original ones as well.
| 归档时间: |
|
| 查看次数: |
231 次 |
| 最近记录: |