📜  Python numpy.histogram(1)

📅  最后修改于: 2023-12-03 15:04:06.780000             🧑  作者: Mango

Python numpy.histogram

The numpy.histogram function is used to compute the histogram of a dataset. A histogram represents the frequency distribution of a set of values into specified intervals or bins. It returns two arrays: the histogram values and the bin edges.

Syntax
numpy.histogram(a, bins=10, range=None, weights=None, density=False)
  • a: The input array containing the data.
  • bins: It can be an integer specifying the number of equal-width bins to create, or an array defining the bin edges.
  • range: The range of values that will be used to determine the bin edges. If not provided, the minimum and maximum values of the input data will be considered.
  • weights: An array of weights associated with the values in a.
  • density: If True, the returned histogram values represent the probability density function (normalized histogram).
Returns

The numpy.histogram function returns a tuple containing two arrays:

  1. hist: The values of the histogram. It represents the number of times each value occurs in the dataset.
  2. bins: The edges of the bins. It represents the boundaries of the intervals used in the histogram.
Example

Let's consider an example to understand the usage of numpy.histogram:

import numpy as np

# Generate a random dataset with 1000 values between 0 and 100
data = np.random.randint(0, 100, size=1000)

# Compute the histogram with 10 bins
hist, bins = np.histogram(data, bins=10)

print("Histogram values:", hist)
print("Bin edges:", bins)

Output:

Histogram values: [ 88  93  98 105  95 107  96 112 101 105]
Bin edges: [ 0.   9.9 19.8 29.7 39.6 49.5 59.4 69.3 79.2 89.1 99. ]

In this example, we generate a random dataset of 1000 values between 0 and 100. Then, we compute the histogram with 10 equal-width bins. The resulting histogram values represent the frequency of values falling within each bin, and the bin edges denote the boundaries of each bin.

Conclusion

The numpy.histogram function is a powerful tool for analyzing datasets and understanding the distribution of values within them. It provides a convenient way to compute histograms with customizable binning options. By using this function, programmers can gain insights into the distribution and frequency of their data, which can be valuable in various data analysis and visualization tasks.