Return elements from bag with probability of ``prob``. Parameters ---------- prob : float A float between 0 and 1, representing the probability that each element will be returned. random_state : int or random.Random, optional If an
(self, prob, random_state=None)
| 672 | return type(self)(graph, name, self.npartitions) |
| 673 | |
| 674 | def random_sample(self, prob, random_state=None): |
| 675 | """Return elements from bag with probability of ``prob``. |
| 676 | |
| 677 | Parameters |
| 678 | ---------- |
| 679 | prob : float |
| 680 | A float between 0 and 1, representing the probability that each |
| 681 | element will be returned. |
| 682 | random_state : int or random.Random, optional |
| 683 | If an integer, will be used to seed a new ``random.Random`` object. |
| 684 | If provided, results in deterministic sampling. |
| 685 | |
| 686 | Examples |
| 687 | -------- |
| 688 | >>> import dask.bag as db |
| 689 | >>> b = db.from_sequence(range(10)) |
| 690 | >>> b.random_sample(0.5, 43).compute() |
| 691 | [0, 1, 3, 4, 7, 9] |
| 692 | >>> b.random_sample(0.5, 43).compute() |
| 693 | [0, 1, 3, 4, 7, 9] |
| 694 | """ |
| 695 | if not 0 <= prob <= 1: |
| 696 | raise ValueError("prob must be a number in the interval [0, 1]") |
| 697 | if not isinstance(random_state, Random): |
| 698 | random_state = Random(random_state) |
| 699 | |
| 700 | name = f"random-sample-{tokenize(self, prob, random_state.getstate())}" |
| 701 | state_data = random_state_data_python(self.npartitions, random_state) |
| 702 | dsk = { |
| 703 | (name, i): (reify, (random_sample, (self.name, i), state, prob)) |
| 704 | for i, state in zip(range(self.npartitions), state_data) |
| 705 | } |
| 706 | graph = HighLevelGraph.from_collections(name, dsk, dependencies=[self]) |
| 707 | return type(self)(graph, name, self.npartitions) |
| 708 | |
| 709 | def remove(self, predicate): |
| 710 | """Remove elements in collection that match predicate. |