直方圖是資料集分佈的圖形表示。它以一系列的條形圖的形式表示數據,其中每個條形圖代表的數據值範圍,條形圖的高度代表在該範圍內定義的數據值的頻率。
這些主要用於表示數值資料的分佈,如班級中的成績分佈,人口分佈或員工收入分佈等。
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 malized quers the freby sion the fidgrams can be malized queaching the freby sion the freby be ormalized viding the frebys the fidgrams can be malized viding the frebys the fidgrams can be malized viding the freby; total data values, which results to the relative frequency histogram where y-axis represents the data values of each bin.
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.
Following is the syntax for creating the histograms for the given range of data.
numpy.histogram(arr, bins, range, normed, weights, density)
Where,
的中文翻譯為:在哪裡,
arr 是輸入陣列
#bins 是用來表示資料的長條圖中的長條數
range 定義了直方圖中的值的範圍
normed 偏好密度參數
#weights是可選參數,用於每個資料值的權重
#密度是將直方圖資料歸一化為機率密度的參數。
The output of the histogram function will be a tuple containing the histogram counts and bin edges.
在下面的範例中,我們使用Numpy的histogram()函數建立了一個直方圖。在這裡,我們將一個陣列作為輸入參數,將bins定義為10,這樣直方圖將被建立為10個bins,其餘的參數可以保持為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)
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.]))
讓我們來看一個例子來理解numpy函式庫的histogram()函數。
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)
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>
在這個例子中,我們透過指定bins和要使用的資料範圍來建立一個直方圖。以下程式碼可以作為參考。
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)
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. ]))
以上是使用Python中的NumPy計算一組資料的直方圖的詳細內容。更多資訊請關注PHP中文網其他相關文章!