如何从 pandas 的一个数据框中创建测试和训练样本? [英] How do I create test and train samples from one dataframe with pandas?

查看:199
本文介绍了如何从 pandas 的一个数据框中创建测试和训练样本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的数据集,以数据框的形式,我想知道如何将数据框分为两个随机样本(80%和20%)进行培训和测试。



谢谢!

解决方案

我只要使用numpy的 randn

 在[11]中:df = pd.DataFrame(np.random.randn(100,2 ))

在[12]中:msk = np.random.rand(len(df))< 0.8

在[13]:train = df [msk]

在[14]中:test = df [〜msk]

只是看到这已经有效:

  Out [15]:21 

在[16]中:len(train)
Out [16]:79


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.

Thanks!

解决方案

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆