From f9e9117fe936b81dd060817cb147232007b412b1 Mon Sep 17 00:00:00 2001 From: AtulDeshpande09 Date: Fri, 4 Jul 2025 19:38:54 +0530 Subject: [PATCH 1/2] [Term Entry] NumPy Random Module: .binomial() --- .../random-module/terms/binomial/binomial.md | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 content/numpy/concepts/random-module/terms/binomial/binomial.md diff --git a/content/numpy/concepts/random-module/terms/binomial/binomial.md b/content/numpy/concepts/random-module/terms/binomial/binomial.md new file mode 100644 index 00000000000..1f9ed395f04 --- /dev/null +++ b/content/numpy/concepts/random-module/terms/binomial/binomial.md @@ -0,0 +1,79 @@ +--- +Title: '.binomial()' +Description: 'Draw samples from a binomial distribution' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Arrays' + - 'Data' + - 'Functions' + - 'Numpy' + - 'Probability' + - 'Statistics' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +In the `random` module of NumPy, the **`.binomial()`** method generates random samples from a binomial distribution. It is commonly used to simulate the number of successes in a sequence of independent trials, where each trial has the same probability of success. + +## Syntax + +```pseudo +numpy.random.binomial(n, p, size=None) +```` + +**Parameters:** + +* `n` *(int or array-like of ints)* : Number of trials. Must be **non-negative**. +* `p` *(float or array-like of floats)* : Probability of success for each trial (between 0 and 1). +* `size` *(Optional)* : The shape of the output array. If not specified, a single value is returned. + +**Return value:** + +The `.binomial()` function returns one or more random values representing the number of successes across `n` trials with success probability `p`. + +* If `size` is `None`, a single integer is returned. +* If `size` is specified, an array of integers is returned with the given shape. + +## Example: Generating Random Binomial Samples in NumPy + +The following example simulates the number of successful outcomes in 10 trials, repeated 5 times: + +```py +import numpy as np + +# Generate 5 random samples from a binomial distribution +result = np.random.binomial(n=10, p=0.5, size=5) + +print(result) +``` + +A possible output of this code could be: + +```shell +array([4 6 5 3 7]) +``` + +Each value represents the number of successful outcomes (e.g., "heads") in 10 independent trials. + +## Codebyte Example + +In this Codebyte example, we generate a single random value from a binomial distribution with 15 trials and a 60% chance of success per trial: + +```codebyte/python +import numpy as np + +# Define number of trials and probability +n = 15 +p = 0.6 + +# Generate one random sample +sample = np.random.binomial(n, p) + +print(sample) +# Returns single value output as size is not specified . +``` + +> **Note:** The result may vary with each execution, as the output is randomly sampled. \ No newline at end of file From 90f368aae7cf15d3679a496535d318c482645ce1 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 8 Jul 2025 18:02:16 +0530 Subject: [PATCH 2/2] Update binomial.md --- .../random-module/terms/binomial/binomial.md | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/content/numpy/concepts/random-module/terms/binomial/binomial.md b/content/numpy/concepts/random-module/terms/binomial/binomial.md index 1f9ed395f04..f13dd11fd0c 100644 --- a/content/numpy/concepts/random-module/terms/binomial/binomial.md +++ b/content/numpy/concepts/random-module/terms/binomial/binomial.md @@ -1,6 +1,6 @@ --- Title: '.binomial()' -Description: 'Draw samples from a binomial distribution' +Description: 'Draw random samples from a binomial distribution.' Subjects: - 'Computer Science' - 'Data Science' @@ -16,30 +16,30 @@ CatalogContent: - 'paths/computer-science' --- -In the `random` module of NumPy, the **`.binomial()`** method generates random samples from a binomial distribution. It is commonly used to simulate the number of successes in a sequence of independent trials, where each trial has the same probability of success. +In NumPy's `random` module, the **`.binomial()`** method generates random samples from a binomial distribution, representing the number of successful outcomes in `n` independent trials, each with a fixed probability `p` of success. ## Syntax ```pseudo numpy.random.binomial(n, p, size=None) -```` +``` **Parameters:** -* `n` *(int or array-like of ints)* : Number of trials. Must be **non-negative**. -* `p` *(float or array-like of floats)* : Probability of success for each trial (between 0 and 1). -* `size` *(Optional)* : The shape of the output array. If not specified, a single value is returned. +- `n` (int or array-like of ints): Number of trials (must be non-negative). +- `p` (float or array-like of floats): Probability of success on each trial (must be between 0 and 1 inclusive). +- `size` (Optional): The shape of the output array. If `None`, a single value is returned. If given as a tuple, it specifies the shape of the output array. **Return value:** -The `.binomial()` function returns one or more random values representing the number of successes across `n` trials with success probability `p`. +The `.binomial()` function returns one or more random integers representing the number of successes across `n` trials with success probability `p`. -* If `size` is `None`, a single integer is returned. -* If `size` is specified, an array of integers is returned with the given shape. +- If `size` is `None`, a single integer is returned. +- If `size` is specified, an array of integers is returned with the given shape. ## Example: Generating Random Binomial Samples in NumPy -The following example simulates the number of successful outcomes in 10 trials, repeated 5 times: +The following example simulates 10 independent trials, repeated 5 times. Each value in the result represents the number of successes in one set of 10 trials: ```py import numpy as np @@ -53,14 +53,12 @@ print(result) A possible output of this code could be: ```shell -array([4 6 5 3 7]) +[3 5 4 5 4] ``` -Each value represents the number of successful outcomes (e.g., "heads") in 10 independent trials. - ## Codebyte Example -In this Codebyte example, we generate a single random value from a binomial distribution with 15 trials and a 60% chance of success per trial: +In this Codebyte example, we generate a single random value from a binomial distribution with 15 trials and a 60% chance of success: ```codebyte/python import numpy as np @@ -73,7 +71,6 @@ p = 0.6 sample = np.random.binomial(n, p) print(sample) -# Returns single value output as size is not specified . ``` -> **Note:** The result may vary with each execution, as the output is randomly sampled. \ No newline at end of file +> **Note:** The output will vary with each execution since it is randomly sampled.