如何使用 Pandas 从一个数据帧创建测试和训练样本? [英] How do I create test and train samples from one dataframe with pandas?
本文介绍了如何使用 Pandas 从一个数据帧创建测试和训练样本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框形式的相当大的数据集,我想知道如何将数据框分成两个随机样本(80% 和 20%)进行训练和测试.
I have a fairly large dataset in the form of a dataframe and I was wondering how I would be able to split the dataframe into two random samples (80% and 20%) for training and testing.
谢谢!
推荐答案
我只想使用 numpy 的 randn
:
I would just use numpy's randn
:
In [11]: df = pd.DataFrame(np.random.randn(100, 2))
In [12]: msk = np.random.rand(len(df)) < 0.8
In [13]: train = df[msk]
In [14]: test = df[~msk]
只是为了看看这是否有效:
And just to see this has worked:
In [15]: len(test)
Out[15]: 21
In [16]: len(train)
Out[16]: 79
这篇关于如何使用 Pandas 从一个数据帧创建测试和训练样本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文