|
| 1 | +--- |
| 2 | +Title: '.geometric()' |
| 3 | +Description: 'Generates random samples from the specified geometric distribution.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | + - 'Data Visualization' |
| 8 | +Tags: |
| 9 | + - 'Data' |
| 10 | + - 'Numpy' |
| 11 | + - 'Random' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.geometric()`** function in [`numpy.random`](https://www.codecademy.com/resources/docs/numpy/random-module) returns random samples from a geometric distribution based on a given probability of success, with the option to control the number of samples through the `size` parameter. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +numpy.random.geometric(p, size=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `p` (float or array_like): Probability of success for each trial. Must be in the range (0, 1]. |
| 28 | +- `size` (int or tuple of ints, optional): Output shape. If `None`, a single value is returned. |
| 29 | + |
| 30 | +**Return value:** |
| 31 | + |
| 32 | +Returns random samples drawn from the geometric distribution, representing the number of trials until the first success. |
| 33 | + |
| 34 | +The probability mass function of geometric distribution is: |
| 35 | + |
| 36 | +$$ |
| 37 | +f(k) = (1 - p)^{k - 1} \ p |
| 38 | +$$ |
| 39 | + |
| 40 | +`p` in this case is the success probability of an individual trial. |
| 41 | + |
| 42 | +## Example |
| 43 | + |
| 44 | +The following code returns 10 random samples from a geometric distribution with the probability of success set to 0.35: |
| 45 | + |
| 46 | +```py |
| 47 | +import numpy as np |
| 48 | + |
| 49 | +# Setting the seed ensures reproducible results |
| 50 | +np.random.seed(42) |
| 51 | + |
| 52 | +results = np.random.geometric(p=0.35, size=10) |
| 53 | +print(results) |
| 54 | +``` |
| 55 | + |
| 56 | +The output of this code would be: |
| 57 | + |
| 58 | +```shell |
| 59 | +[2 7 4 3 1 1 1 5 3 3] |
| 60 | +``` |
| 61 | + |
| 62 | +## Codebyte Example |
| 63 | + |
| 64 | +This Codebyte simulates 100 coin flips with an unfair coin (35% chance of Heads), then counts how many trials resulted in a success on the first attempt: |
| 65 | + |
| 66 | +```codebyte/python |
| 67 | +import numpy as np |
| 68 | +
|
| 69 | +# Set the seed for reproduceability |
| 70 | +np.random.seed(33) |
| 71 | +
|
| 72 | +# Simulate 100 trials |
| 73 | +results = np.random.geometric(p=0.35, size=100) |
| 74 | +
|
| 75 | +# How many trials succeeded on the first attempt? |
| 76 | +print((results == 1).sum()) |
| 77 | +``` |
| 78 | + |
| 79 | +Here: |
| 80 | + |
| 81 | +- `p=0.35`: Probability of success (e.g., the coin landing on Heads). |
| 82 | +- `size=100`: Generates 100 random samples. |
| 83 | +- `np.random.geometric(p, size)`: Returns the number of trials needed to get the first success. |
| 84 | + |
| 85 | +This example demonstrates how geometric distributions can model binary outcome processes like coin tosses where the goal is to measure how many attempts are needed to succeed once. |
0 commit comments