我有整數列表並希望得到每個整數的頻率。這是討論herePython:發生的頻率
問題是,我使用的方法給我的頻率浮動數字時,我的數據集只包含整數。爲什麼會發生這種情況,以及如何從我的數據中獲取整數的頻率?
我使用pyplot.histogram密謀與出現
import numpy as np
import matplotlib.pyplot as plt
from numpy import *
data = loadtxt('data.txt',dtype=int,usecols=(4,)) #loading 5th column of csv file into array named data.
plt.hist(data) #plotting the column as histogram
我得到直方圖的頻率直方圖,但我發現,如果我「打印」的歷史(數據)
hist=np.histogram(data)
print hist(data)
我得到這個:
(array([ 2323, 16338, 1587, 212, 26, 14, 3, 2, 2, 2]),
array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. , 11.8, 13.6, 15.4,
17.2, 19. ]))
當第二陣列代表值和第一陣列代表出現次數。
在我的數據集中,所有值都是整數,第二個數組是如何發生的,第二個數組有浮點數,我應該如何得到整數的頻率?
更新:
這就解決了這個問題,謝謝列夫的回覆。
plt.hist(data, bins=np.arange(data.min(), data.max()+1))
爲了避免創建一個新的問題我可以如何繪製每個整數的「在中間」列?再說了,我要爲整數3取空間列2.5,而不是和4
你確定你使用的是你以爲你是數據?你的評論說第4列,但索引從0開始,所以第4列實際上是第5列。 – daveydave400
是的,它是第五列,錯字。 – user40
我想它應該是'data.max()+ 2'。 'np.arange'沒有上邊框,'bins'包含範圍(元素從0-1,1-2,......) –