|
| 1 | +--- |
| 2 | +Title: '.histogram()' |
| 3 | +Description: 'Computes the histogram of an array, summarizing the distribution of its values.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Functions' |
| 10 | + - 'Linear Algebra' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.histogram()`** function in NumPy is used to compute the frequency distribution of data by dividing values into bins and counting how many fall into each bin. It’s commonly used in data analysis, statistical modeling, and visualization to understand the distribution of numerical data. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +numpy.histogram(a, bins=10, range=None, density=None, weights=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `a` (array_like): Input data. The histogram is computed over the flattened array. |
| 28 | +- `bins` (`int`, sequence of scalars, or `str`, optional): |
| 29 | + - If an `int`, it defines the number of equal-width bins (default is `10`). |
| 30 | + - If a sequence, it specifies the bin edges. |
| 31 | + - If a str, it defines the method used to calculate the optimal bin width. |
| 32 | +- `range` (tuple, optional): Lower and upper range of the bins. Defaults to `(a.min(), a.max())`. |
| 33 | +- `density` (`bool`, optional): |
| 34 | + - If `False`, returns the count of samples in each bin. |
| 35 | + - If `True`, returns the probability density function. |
| 36 | +- `weights` (array_like, optional): Weights for each value in `a`. If `density=True`, weights are normalized. |
| 37 | + |
| 38 | +**Return value:** |
| 39 | + |
| 40 | +The `.histogram()` function in NumPy returns a tuple of two arrays: |
| 41 | + |
| 42 | +```pseudo |
| 43 | +(hist, bin_edges) |
| 44 | +``` |
| 45 | + |
| 46 | +- `hist`: A NumPy array of counts or probability densities (depending on the `density` parameter). It tells you how many elements from the input fell into each bin. |
| 47 | +- `bin_edges`: A NumPy array of bin edge values. It has one more element than `hist`, representing the edges of the bins, including the rightmost edge. |
| 48 | + |
| 49 | +## Example |
| 50 | + |
| 51 | +This example demonstrates the use of `.histogram()` in NumPy: |
| 52 | + |
| 53 | +```py |
| 54 | +import numpy as np |
| 55 | +# Creating a NumPy histogram from array and set bins |
| 56 | +a = np.histogram([1, 2, 1, 3, 2, 4, 3, 0, 0, 5], bins=[0, 1, 2, 3, 4, 5, 6]) |
| 57 | + |
| 58 | +# Print the histogram |
| 59 | +print(str(1) + ':' + str(a)) |
| 60 | + |
| 61 | +# Creating a NumPy histogram with numpy.arange() and setting density as True |
| 62 | +b = np.histogram(np.arange(4), bins=np.arange(5), density=True) |
| 63 | + |
| 64 | +# Print the histogram |
| 65 | +print(str(2) + ':' + str(b)) |
| 66 | +``` |
| 67 | + |
| 68 | +The output for the example will be: |
| 69 | + |
| 70 | +```shell |
| 71 | +1:(array([2, 2, 2, 2, 1, 1]), array([0, 1, 2, 3, 4, 5, 6])) |
| 72 | +2:(array([0.25, 0.25, 0.25, 0.25]), array([0, 1, 2, 3, 4])) |
| 73 | +``` |
| 74 | + |
| 75 | +## Codebyte Example |
| 76 | + |
| 77 | +This codebyte example shows the use of NumPy `.histogram()` to display an array in a histogram display: |
| 78 | + |
| 79 | +```codebyte/python |
| 80 | +import numpy as np |
| 81 | +import random |
| 82 | +
|
| 83 | +# Create an array of data |
| 84 | +data = np.array([12, 14, 18, 19, 25]) |
| 85 | +
|
| 86 | +# Create bin to set the interval |
| 87 | +bin = [0, 10, 20, 30] |
| 88 | +
|
| 89 | +# Create the histogram |
| 90 | +graph_1 = np.histogram(data, bin) |
| 91 | +
|
| 92 | +# Print the histogram |
| 93 | +print(graph_1) |
| 94 | +
|
| 95 | +# Create a random integer between 1-10 |
| 96 | +p = random.randint(1, 10) |
| 97 | +
|
| 98 | +# Create a histogram with the integer and default bin |
| 99 | +graph_2 = np.histogram(p) |
| 100 | +
|
| 101 | +# Print the histogram |
| 102 | +print(graph_2) |
| 103 | +
|
| 104 | +# Create a sample of 10 elements between 1 and 20 |
| 105 | +data = np.random.random_integers(1, 20, 1000) |
| 106 | +
|
| 107 | +# Create bins with .linspace() with 2 intervals |
| 108 | +bins = np.linspace(0, 20, 2, dtype = int) |
| 109 | +
|
| 110 | +# Create a histogram with density set as True |
| 111 | +graph_3 = np.histogram(data, bins, density = True) |
| 112 | +
|
| 113 | +# Print the histogram |
| 114 | +print(graph_3) |
| 115 | +``` |
0 commit comments