python - Pandas 中的 Dato : What's the equivalent function for graphlab. random_split() ?

标签 python pandas graphlab sframe

我正在 Coursera 上学习机器学习类(class)。类(class)中强调我们使用Dato中的GraphLab 。在其中一个练习中,讲师使用 graphlab.random_split() 来分割 SFrame,如下所示:

sales = graphlab.SFrame('home_data.gl/')
train_data, test_data = sales.random_split(.8,seed=0)

我已经完成了第一周的类(class),测验要求我们使用 GraphLab 和 SFrame 解决一个问题。我尝试安装 GraphLab,但是它需要 64 位 PC,而我的 PC 是 32 位。如果我们愿意的话,老师可以选择使用 Pandas,所以我开始使用 Pandas。

我的问题是这样的,讲师使用 sales.random_split(.8,seed=0),这将为他提供 train_data, test_data。他将使用它们进行进一步分析,并得出答案。

现在,如果我不使用 pandas 函数来以完全相同的方式分割数据,我的答案将永远不会与他的匹配,我永远无法通过这个测验。我有兴趣使用的 pandas 函数是:

train_data, test_data = pandas.DataFrame.sample(frac=0.8, random_state=0)

我的问题是这样的: pandas.DataFrame.sample(frac=0.8, random_state=0) 会产生与 sales.random_split(.8,seed=0) 相同的输出。

我已经写信给老师了,正在等待他的回复,同时如果有人可以帮助我,请帮忙。谢谢。

最佳答案

最接近的等价物可能是 sklearn.cross_validation.train_test_split 。但是,它的行为与 SFrame.random_split 不同。 。快速检查:

from __future__ import print_function
import numpy as np
import pandas as pd
import graphlab as gl
from sklearn.cross_validation import train_test_split

df = pd.DataFrame({'a':np.arange(100), 'b':np.arange(100)[::-1]})
sf = gl.SFrame({'a':np.arange(100), 'b':np.arange(100)[::-1]})

train_pd, test_pd = train_test_split(df, test_size=0.8, random_state=0)
train_gl, test_gl = sf.random_split(0.8, seed=0)

frames = [train_pd, test_pd, train_gl, test_gl]

print(*[len(f) for f in frames], end='\n\n')
print(*[f.head(3) for f in frames], sep='\n\n')

输出:

20 80 86 14

     a   b
25  25  74
37  37  62
81  81  18

     a   b
26  26  73
86  86  13
2    2  97

+---+----+
| a | b  |
+---+----+
| 0 | 99 |
| 1 | 98 |
| 2 | 97 |
+---+----+
[3 rows x 2 columns]


+----+----+
| a  | b  |
+----+----+
| 12 | 87 |
| 15 | 84 |
| 25 | 74 |
+----+----+
[3 rows x 2 columns]

关于python - Pandas 中的 Dato : What's the equivalent function for graphlab. random_split() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35764715/

相关文章:

python - 如何在 Mac/Python 中检查屏幕是否关闭?

python - NoSuchModuleError : Can't load plugin: sqlalchemy. 方言:雪花

python - 我可以在matplotlib中显示没有科学计数法的图片像素值吗

python - 多键字典的平均值

python - 将所有数据框列转换为 float 的最快方法 - pandas astype slow

Python CSV : Grab all values in row with conditions for time values

python - 如何在不合并索引的情况下连接两个具有不同多索引的数据帧?

python - 使用 SFrame 和 SArray 与 Graphlab 和/或 Numpy 进行矩阵乘法

python-2.7 - 从 Spyder 运行 graphlab

graphlab - 如何将 `apply`与需要多个输入的函数一起使用