


Calculate a histogram of a set of data using NumPy in Python
Histogram is a graphical representation of the distribution of a data set. It represents data in the form of a series of bar charts, where each bar represents a range of data values and the height of the bar represents the frequency of data values defined within that range.
These are mainly used to represent the distribution of numerical data, such as the distribution of grades in a class, population distribution or employee income distribution, etc.
In histogram, x-axis represents the range of data values, divided into intervals and the y-axis represents the frequency of the range of data values within each bin. Histograms can be normalized by dividing the frequency of each bin by the total data values, which results to the relative frequency histogram where y-axis represents the data values of each bin.
Calculating histogram using Python Numpy
In python, for creating the histograms we have numpy, matplotlib and seaborn libraries. In Numpy, we have the function named histogram() to work with the histogram data.
grammar
Following is the syntax for creating the histograms for the given range of data.
numpy.histogram(arr, bins, range, normed, weights, density)
Where,
is:Where,
arr is the input array
bins is the number of bars in the histogram used to represent the data
range Defines the range of values in the histogram
normed Preference density parameter
weights is an optional parameter, used for the weight of each data value
Density is a parameter that normalizes histogram data to probability density.
The output of the histogram function will be a tuple containing the histogram counts and bin edges.
Example
In the example below, we create a histogram using Numpy’s histogram() function. Here, we are taking an array as input parameter and defining bins as 10 so that the histogram will be created with 10 bins and the rest of the parameters can be kept as none.
import numpy as np arr = np.array([10,20,25,40,35,23]) hist = np.histogram(arr,bins = 10) print("The histogram created:",hist)
Output
The histogram created: (array([1, 0, 0, 1, 1, 1, 0, 0, 1, 1], dtype=int64), array([10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40.]))
Example
Let us look at an example to understand the histogram() function of numpy library.
import numpy as np arr = np.array([[20,20,25],[40,35,23],[34,22,1]]) hist = np.histogram(arr,bins = 20) print("The histogram created:",hist)
Output
The histogram created: (array([1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1], dtype=int64), array([ 1. , 2.95, 4.9 , 6.85, 8.8 , 10.75, 12.7 , 14.65, 16.6 , 18.55, 20.5 , 22.45, 24.4 , 26.35, 28.3 , 30.25, 32.2 , 34.15, 36.1 , 38.05, 40. ]))</p><p>
Example
In this example, we create a histogram by specifying the bins and the data range to use. The following code can be used as a reference.
import numpy as np arr = np.array([[20,20,25],[40,35,23],[34,22,1]]) hist = np.histogram(arr,bins = 20, range = (1,10)) print("The histogram created:", hist)
Output
The histogram created: (array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0], dtype=int64), array([ 1. , 1.45, 1.9 , 2.35, 2.8 , 3.25, 3.7 ,4.15, 4.6 , 5.05, 5.5 , 5.95, 6.4 , 6.85, 7.3 , 7.75, 8.2 , 8.65, 9.1 , 9.55, 10. ]))
The above is the detailed content of Calculate a histogram of a set of data using NumPy in Python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Numpy is an important mathematics library in Python. It provides efficient array operations and scientific calculation functions and is widely used in data analysis, machine learning, deep learning and other fields. When using numpy, we often need to check the version number of numpy to determine the functions supported by the current environment. This article will introduce how to quickly check the numpy version and provide specific code examples. Method 1: Use the __version__ attribute that comes with numpy. The numpy module comes with a __

General Matrix Multiplication (GEMM) is a vital part of many applications and algorithms, and is also one of the important indicators for evaluating computer hardware performance. In-depth research and optimization of the implementation of GEMM can help us better understand high-performance computing and the relationship between software and hardware systems. In computer science, effective optimization of GEMM can increase computing speed and save resources, which is crucial to improving the overall performance of a computer system. An in-depth understanding of the working principle and optimization method of GEMM will help us better utilize the potential of modern computing hardware and provide more efficient solutions for various complex computing tasks. By optimizing the performance of GEMM

WORD is a powerful word processor. We can use word to edit various texts. In Excel tables, we have mastered the calculation methods of addition, subtraction and multipliers. So if we need to calculate the addition of numerical values in Word tables, How to subtract the multiplier? Can I only use a calculator to calculate it? The answer is of course no, WORD can also do it. Today I will teach you how to use formulas to calculate basic operations such as addition, subtraction, multiplication and division in tables in Word documents. Let's learn together. So, today let me demonstrate in detail how to calculate addition, subtraction, multiplication and division in a WORD document? Step 1: Open a WORD, click [Table] under [Insert] on the toolbar, and insert a table in the drop-down menu.

How to upgrade numpy version: Easy-to-follow tutorial, requires concrete code examples Introduction: NumPy is an important Python library used for scientific computing. It provides a powerful multidimensional array object and a series of related functions that can be used to perform efficient numerical operations. As new versions are released, newer features and bug fixes are constantly available to us. This article will describe how to upgrade your installed NumPy library to get the latest features and resolve known issues. Step 1: Check the current NumPy version at the beginning

Teach you step by step to install NumPy in PyCharm and make full use of its powerful functions. Preface: NumPy is one of the basic libraries for scientific computing in Python. It provides high-performance multi-dimensional array objects and various functions required to perform basic operations on arrays. function. It is an important part of most data science and machine learning projects. This article will introduce you to how to install NumPy in PyCharm, and demonstrate its powerful features through specific code examples. Step 1: Install PyCharm First, we

With the rapid development of fields such as data science, machine learning, and deep learning, Python has become a mainstream language for data analysis and modeling. In Python, NumPy (short for NumericalPython) is a very important library because it provides a set of efficient multi-dimensional array objects and is the basis for many other libraries such as pandas, SciPy and scikit-learn. In the process of using NumPy, you are likely to encounter compatibility issues between different versions, then

The secret of how to quickly uninstall the NumPy library is revealed. Specific code examples are required. NumPy is a powerful Python scientific computing library that is widely used in fields such as data analysis, scientific computing, and machine learning. However, sometimes we may need to uninstall the NumPy library, whether to update the version or for other reasons. This article will introduce some methods to quickly uninstall the NumPy library and provide specific code examples. Method 1: Use pip to uninstall pip is a Python package management tool that can be used to install, upgrade and

Detailed explanation of numpy slicing operation method and practical application guide Introduction: Numpy is one of the most popular scientific computing libraries in Python, providing powerful array operation functions. Among them, slicing operation is one of the commonly used and powerful functions in numpy. This article will introduce the slicing operation method in numpy in detail, and demonstrate the specific use of slicing operation through practical application guide. 1. Introduction to numpy slicing operation method Numpy slicing operation refers to obtaining a subset of an array by specifying an index interval. Its basic form is:
