From ba086b685bb5624a14f7d60abf9a262dfffbe32e Mon Sep 17 00:00:00 2001 From: VeryExcitedGremlin Date: Tue, 15 Jul 2025 18:17:31 -0700 Subject: [PATCH 1/4] Create geometric.md --- content/numpy/concepts/random-module/terms/geometric/geometric.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 content/numpy/concepts/random-module/terms/geometric/geometric.md diff --git a/content/numpy/concepts/random-module/terms/geometric/geometric.md b/content/numpy/concepts/random-module/terms/geometric/geometric.md new file mode 100644 index 00000000000..e69de29bb2d From e45cf8e83ced5530f8db36a1c7e293840c6b4741 Mon Sep 17 00:00:00 2001 From: VeryExcitedGremlin Date: Tue, 15 Jul 2025 20:35:24 -0700 Subject: [PATCH 2/4] Update geometric.md --- .../terms/geometric/geometric.md | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/content/numpy/concepts/random-module/terms/geometric/geometric.md b/content/numpy/concepts/random-module/terms/geometric/geometric.md index e69de29bb2d..c3c0fb089e3 100644 --- a/content/numpy/concepts/random-module/terms/geometric/geometric.md +++ b/content/numpy/concepts/random-module/terms/geometric/geometric.md @@ -0,0 +1,84 @@ +--- +Title: '.geometric()' +Description: 'Genereates random samples from the specified geometric distribution.' +Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! + - 'Computer Science' + - 'Data Science' + - 'Data Visualization' +Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! + - 'Data' + - 'Numpy' + - 'Random' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' + - 'paths/data-science' + - 'paths/data-science-foundations' +--- + +The **`.geometric()`** function in the numpy.random module generates random samples from the specified geometric distribution, where `p` is the probability of success of an individual trial, and `size` is the shape of the output array. + +## Syntax + +```pseudo +numpy.random.geometric(p, size=None) +``` + +**Parameters:** + +- `p` (float or array_like of floats): The probability of success of an individual trial +- `size` (int or tuple of ints, optional): Specifies output shape. Default is `None`, a single value is returned if `p` is a scalar. If integer or tuple returns an array of that shape. + +**Return Value:** + +Returns random samples from the geometric distribution in the shape specified by `size`. + +The probability mass function of the geometric distribution is: + +``` +$f(k) = (1-p) \exp(k-1) p,$ +``` + +where `p` is the probability of success of an individual trial. + +## Example + +The following code returns 10 random samples from a geometric distribution with probability of success set to 0.35. Setting the seed ensures reproduceable results. + +```py +import numpy as np +np.random.seed(42) +results = np.random.geometric(p=0.35, size=10) +print(results) +``` + +The output of this code would be: + +```shell +[2 7 4 3 1 1 1 5 3 3] +``` + +## Codebyte Example + +This codebyte simulates 100 coin flips on an unfair coin, 35% chance of Heads, then calculates how many trials succeeded on the first attempt when calling only Heads. + +```codebyte/python +import numpy as np + +# Set the seed for reproduceability +np.random.seed(33) + +# Simulate 100 trials +results = np.random.geometric(p=0.35, size=100) + +# How many trials succeeded on the first attempt? +print((results == 1).sum()) +``` + +Here: + +- `p=0.35` is the chance of success, 35% chance of the coin coming up Heads +- `size=100' generates 100 trials +- `np.random.geometric(p, size)` creates the random samples from the specified distribution + +This example demonstrates how geometriuc distributions can be used to model real-world scenarios with only 2 possible outcomes. From 8c31c765b6fc2696fe089993a01c646fb95a605d Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 24 Jul 2025 16:59:24 +0530 Subject: [PATCH 3/4] minor tweaks --- .../terms/geometric/geometric.md | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/content/numpy/concepts/random-module/terms/geometric/geometric.md b/content/numpy/concepts/random-module/terms/geometric/geometric.md index c3c0fb089e3..c1cd6106cab 100644 --- a/content/numpy/concepts/random-module/terms/geometric/geometric.md +++ b/content/numpy/concepts/random-module/terms/geometric/geometric.md @@ -1,22 +1,20 @@ --- Title: '.geometric()' Description: 'Genereates random samples from the specified geometric distribution.' -Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! +Subjects: - 'Computer Science' - 'Data Science' - 'Data Visualization' -Tags: # Please only use Tags in the tags.md file (https://github.com/Codecademy/docs/blob/main/documentation/tags.md). If that list feels insufficient, feel free to create a new Tag and add it to tags.md in your PR! +Tags: - 'Data' - 'Numpy' - 'Random' CatalogContent: - 'learn-python-3' - 'paths/computer-science' - - 'paths/data-science' - - 'paths/data-science-foundations' --- -The **`.geometric()`** function in the numpy.random module generates random samples from the specified geometric distribution, where `p` is the probability of success of an individual trial, and `size` is the shape of the output array. +The **`.geometric()`** function in `numpy.random` 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. ## Syntax @@ -26,28 +24,31 @@ numpy.random.geometric(p, size=None) **Parameters:** -- `p` (float or array_like of floats): The probability of success of an individual trial -- `size` (int or tuple of ints, optional): Specifies output shape. Default is `None`, a single value is returned if `p` is a scalar. If integer or tuple returns an array of that shape. +- `p` (float or array_like): Probability of success for each trial. Must be in the range (0, 1]. +- `size` (int or tuple of ints, optional): Output shape. If None, a single value is returned. **Return Value:** -Returns random samples from the geometric distribution in the shape specified by `size`. +Returns random samples drawn from the geometric distribution, representing the number of trials until the first success. The probability mass function of the geometric distribution is: -``` -$f(k) = (1-p) \exp(k-1) p,$ -``` +$$ +f(k) = (1 - p)^{k - 1} \ p +$$ -where `p` is the probability of success of an individual trial. +`p` in this case is the success probability of an individual trial. ## Example -The following code returns 10 random samples from a geometric distribution with probability of success set to 0.35. Setting the seed ensures reproduceable results. +The following code returns 10 random samples from a geometric distribution with the probability of success set to 0.35: ```py import numpy as np + +# Setting the seed ensures reproducible results np.random.seed(42) + results = np.random.geometric(p=0.35, size=10) print(results) ``` @@ -60,7 +61,7 @@ The output of this code would be: ## Codebyte Example -This codebyte simulates 100 coin flips on an unfair coin, 35% chance of Heads, then calculates how many trials succeeded on the first attempt when calling only Heads. +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: ```codebyte/python import numpy as np @@ -77,8 +78,8 @@ print((results == 1).sum()) Here: -- `p=0.35` is the chance of success, 35% chance of the coin coming up Heads -- `size=100' generates 100 trials -- `np.random.geometric(p, size)` creates the random samples from the specified distribution +- `p=0.35`: Probability of success (e.g., the coin landing on Heads). +- `size=100`: Generates 100 random samples. +- `np.random.geometric(p, size)`: Returns the number of trials needed to get the first success. -This example demonstrates how geometriuc distributions can be used to model real-world scenarios with only 2 possible outcomes. +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. From 47613b0bae967d8a9feecacbe68da1350f05feae Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 24 Jul 2025 17:00:19 +0530 Subject: [PATCH 4/4] Update geometric.md --- .../concepts/random-module/terms/geometric/geometric.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/numpy/concepts/random-module/terms/geometric/geometric.md b/content/numpy/concepts/random-module/terms/geometric/geometric.md index c1cd6106cab..88b816572b7 100644 --- a/content/numpy/concepts/random-module/terms/geometric/geometric.md +++ b/content/numpy/concepts/random-module/terms/geometric/geometric.md @@ -25,13 +25,13 @@ numpy.random.geometric(p, size=None) **Parameters:** - `p` (float or array_like): Probability of success for each trial. Must be in the range (0, 1]. -- `size` (int or tuple of ints, optional): Output shape. If None, a single value is returned. +- `size` (int or tuple of ints, optional): Output shape. If `None`, a single value is returned. -**Return Value:** +**Return value:** Returns random samples drawn from the geometric distribution, representing the number of trials until the first success. -The probability mass function of the geometric distribution is: +The probability mass function of geometric distribution is: $$ f(k) = (1 - p)^{k - 1} \ p