pandas 在train_test_split之后显示SettingWithCopyWarning [英] Pandas shows SettingWithCopyWarning after train_test_split
问题描述
我正在尝试处理从Sci-Kit Learn的 train_test_split 操作接收的数据帧.系统给我以下信息:
I'm trying to manipulate a dataframe that I received from Sci-Kit Learn's train_test_split operation. The system gives me the following:
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning:试图在一个副本上设置一个值 从DataFrame切片
/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py:179: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
以下内容在我的系统上发出警告:
The following raises the warning on my system:
import pandas as pd
from sklearn.model_selection import train_test_split
X=pd.DataFrame({'A':[2,5,7,8,9],'B':[2,5,3,51,5]})
(Xt,Xv)=train_test_split(X)
Xt.iloc[0,0]=6
我使用以下版本:
python:'3.6.1(默认,2017年6月26日,19:29:26)\ n [GCC 4.9.2]'
python: '3.6.1 (default, Jun 26 2017, 19:29:26) \n[GCC 4.9.2]'
熊猫:0.20.3
sklearn:0.18.2
sklearn: 0.18.2
推荐答案
您可以按照以下方法解决此问题:
You can workaround it as follows:
In [16]: Xt = Xt.copy()
In [17]: Xt.iloc[0,0]=6
In [18]: Xt
Out[18]:
A B
0 6 2
2 7 3
1 5 5
In [19]: X
Out[19]:
A B
0 2 2 # <--- NOTE: the value in the original DF has NOT been changed
1 5 5
2 7 3
3 8 51
4 9 5
或者,您可以使用 numpy.split(...)方法
这篇关于 pandas 在train_test_split之后显示SettingWithCopyWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!