【问题标题】:Sampling n= 2000 from a Dask Dataframe of len 18000 generates error Cannot take a larger sample than population when 'replace=False'从 len 18000 的 Dask 数据帧中采样 n = 2000 会产生错误 当“replace = False”时无法采用比总体更大的样本
【发布时间】:2017-01-03 16:43:39
【问题描述】:

我有一个从 csv 文件创建的 dask 数据框,len(daskdf) 返回 18000,但是当我 ddSample = daskdf.sample(2000) 时出现错误

ValueError: Cannot take a larger sample than population when 'replace=False'

如果数据框大于样本大小,我可以在不替换的情况下进行采样吗?

【问题讨论】:

    标签: python dask


    【解决方案1】:

    示例方法仅支持frac= 关键字参数。见API documentation

    您收到的错误来自 Pandas,而不是 Dask。

    In [1]: import pandas as pd
    In [2]: df = pd.DataFrame({'x': [1]})
    In [3]: df.sample(frac=2000, replace=False)
    ValueError: Cannot take a larger sample than population when 'replace=False'
    

    解决方案

    正如 Pandas 错误所暗示的,考虑替换抽样

    In [4]: df.sample(frac=2, replace=True)
    Out[4]: 
       x
    0  1
    0  1
    
    In [5]: import dask.dataframe as dd
    In [6]: ddf = dd.from_pandas(df, npartitions=1)
    In [7]: ddf.sample(frac=2, replace=True).compute()
    Out[7]: 
       x
    0  1
    0  1
    

    【讨论】:

    • 抱歉,我仍然无法理解它sampledf = df.sample(frac=2000) 如果我 sampledf.head() 仍然会产生错误
    • 是的,pandas 无法生成比总体更大的样本。 Frac 必须小于 1,例如 df.sample(frac=0.10)。或者,也许您想设置replace=True
    【解决方案2】:

    sample方法中,将参数replace改为True

    df.sample(samples, replace=True)
    

    表明DataFrame的大小大于他们需要的样本数。所以这是一个临时的解决方法。

    【讨论】:

      【解决方案3】:

      也许关键是他想从原始数据框中提取行样本,所以恕我直言,我认为您应该指定 axis=0 从行中采样。

      【讨论】:

        猜你喜欢
        • 2016-06-19
        • 1970-01-01
        • 2019-03-19
        • 2021-01-02
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多