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

Method sample

dask/dataframe/dask_expr/_collection.py:2576–2622  ·  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

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

Calls 3

random_state_dataFunction · 0.90
new_collectionFunction · 0.90
RandomStateMethod · 0.45