numpy.histogram() in PythonThe numpy module of Python provides a function called numpy.histogram(). This function represents the frequency of the number of values that are compared with a set of values ranges. This function is similar to the hist() function of matplotlib.pyplot. In simple words, this function is used to compute the histogram of the set of data. Syntax:Parameters:x: array_like This parameter defines a flattened array over which the histogram is computed. bins: int or sequence of str or scalars(optional) If this parameter is defined as an integer, then in the given range, it defines the number of equal-width bins. Otherwise, an array of bin edges which monotonically increased is defined. It also includes the rightmost edge, which allows for non-uniform bin widths. The latest version of numpy allows us to set bin parameters as a string, which defines a method for calculating optimal bin width. range : (float, float)(optional) This parameter defines the lower-upper ranges of the bins. By default, the range is (x.min(), x.max()). The values are ignored, which are outside the range. The ranges of the first element should be equal to or less than the second element. normed : bool(optional) This parameter is the same as the density argument, but it can give the wrong output for unequal bin widths. weights : array_like(optional) This parameter defines an array which contains weights and has the same shape as 'x'. density : bool(optional) If it is set to True, will result in the number of samples in every bin. If its value is False, the density function will result in the value of the probability density function in the bin. Returns:hist: array The density function returns the values of the histogram. edge_bin: an array of float dtype This function returns the bin edges (length(hist+1)). Example 1:Output: (array([0, 1, 1], dtype=int64), array([0, 1, 2, 3])) In the above code
In the output, it shows a ndarray that contain the values of the histogram. Example 2:Output: (array([0.16666667, 0.16666667, 0.16666667, 0.16666667, 0.16666667, 0.16666667]), array([0, 1, 2, 3, 4, 5, 6])) Example 3:Output: (array([0, 4, 2], dtype=int64), array([0, 1, 2, 3])) Example 4:Output: array([0.17857143, 0.17857143, 0.17857143, 0. , 0.17857143, 0.17857143, 0. , 0.17857143, 0.17857143, 0.17857143]) array([0. , 0.7, 1.4, 2.1, 2.8, 3.5, 4.2, 4.9, 5.6, 6.3, 7. ]) Example 5:Output: array([0.17857143, 0.17857143, 0.17857143, 0. , 0.17857143, 0.17857143, 0. , 0.17857143, 0.17857143, 0.17857143]) 1.4285714285714288 1.0 In the above code
In the output, it shows a ndarray that contains the values of the histogram and the sum of histogram values. Next TopicNumpy.sort |