MCPcopy Create free account
hub / github.com/dask/dask / sample

Method sample

dask/dataframe/dask_expr/_collection.py:2572–2618  ·  view source on GitHub ↗

Random sample of items Parameters ---------- n : int, optional Number of items to return is not supported by dask. Use frac instead. frac : float, optional Approximate fraction of items to return. This sampling fraction is

(self, n=None, frac=None, replace=False, random_state=None)

Source from the content-addressed store, hash-verified

2570 return func(self, *args, **kwargs)
2571
2572 def sample(self, n=None, frac=None, replace=False, random_state=None):
2573 """Random sample of items
2574
2575 Parameters
2576 ----------
2577 n : int, optional
2578 Number of items to return is not supported by dask. Use frac
2579 instead.
2580 frac : float, optional
2581 Approximate fraction of items to return. This sampling fraction is
2582 applied to all partitions equally. Note that this is an
2583 **approximate fraction**. You should not expect exactly ``len(df) * frac``
2584 items to be returned, as the exact number of elements selected will
2585 depend on how your data is partitioned (but should be pretty close
2586 in practice).
2587 replace : boolean, optional
2588 Sample with or without replacement. Default = False.
2589 random_state : int or ``np.random.RandomState``
2590 If an int, we create a new RandomState with this as the seed;
2591 Otherwise we draw from the passed RandomState.
2592
2593 See Also
2594 --------
2595 DataFrame.random_split
2596 pandas.DataFrame.sample
2597 """
2598 if n is not None:
2599 msg = (
2600 "sample does not support the number of sampled items "
2601 "parameter, 'n'. Please use the 'frac' parameter instead."
2602 )
2603 if isinstance(n, Number) and 0 <= n <= 1:
2604 warnings.warn(msg)
2605 frac = n
2606 else:
2607 raise ValueError(msg)
2608
2609 if frac is None:
2610 raise ValueError("frac must not be None")
2611
2612 if random_state is None:
2613 random_state = np.random.RandomState()
2614
2615 state_data = random_state_data(self.npartitions, random_state)
2616 return new_collection(
2617 expr.Sample(self, state_data=state_data, frac=frac, replace=replace)
2618 )
2619
2620 def _repr_data(self):
2621 raise NotImplementedError

Calls 3

random_state_dataFunction · 0.90
new_collectionFunction · 0.90
RandomStateMethod · 0.45