1
編輯:更多的試驗和錯誤後,我想出了,由於某種原因,python說,1/52是0,任何人都可以解釋爲什麼,所以我可以避免在將來這個問題?ChiSquare計算返回全零
我一直在努力處理一段時間的腳本,主要是因爲我或我的同學根本找不到它出了什麼問題。
爲了使事情簡單化,我們獲得了數據和模型,並且我們必須將一些數據點重新調整爲模型,然後執行chi2square最小化以找到最佳重新縮放因子。
我已經嘗試了多件事。試圖把一切都在1個循環,當沒有工作,我試圖分裂環達等
我的代碼的相關部分看起來是這樣的:
#Here I pick the values of the model that correspond to the data
y4 = np.zeros((len(l),1))
for x in range(0,len(l)):
if l[x] < 2.16:
for y in range(0,len(lmodel)):
if lmodel[y] == l[x]:
y4[x] = y2[y]
elif lmodel[y] < l[x] < lmodel[y+1]:
y4[x] = (y2[y] + y2[y+1])/2
else:
y4[x] = y1[x]
#Do Chi2 calculation
#First, I make a matrix with all the possible rescaled values
chi2 = np.zeros((200,1))
y3 = np.zeros((len(l),len(chi2)))
for z in range(0,len(chi2)):
for x in range(0,len(l)):
if l[x] < 2.16:
y3[x,z] = y1[x]*10**(0.4*Al[x]*z/100)
else:
y3[x,z] = y1[x]
#Here I calculate the chisquare for each individual column and put it in the chi2 array
dummy = np.zeros((len(l),1))
for x in range(0,len(chi2)):
for t in range(0, len(l)):
dummy[t] = (1/52)*((y3[t,x] - y4[t])/fle[t])**2
chi2[x] = np.sum(dummy)
的事情是,不管是什麼我嘗試,出於某種原因,我的虛擬陣列總是全零,使每一個卡方值爲0.
我試圖製作'虛擬'矩陣和事後總結,我已經嘗試打印單個值爲計算虛擬[t]的,其中一些是0(如預期),有些不是,如此邏輯上,如果個別值不是全部爲0,也不應該每個值你是假的。
我只是無法找到我出問題的地方,爲什麼我總是得到零的數組。
你的圓括號在這裏是不平衡的:'dummy = np.zeros((len(l),1)' – khelwood
是的,我看到了,但那不是問題,否則它會是一個簡單的語法錯誤。我只是想出了什麼問題,但它讓我更加困惑。出於某種原因,python告訴我1/52是0,所以我改變了1/52到((52)**( - 1))和現在它的工作原理... – gormar
你使用什麼版本的python?在python 2.x中「1/52」確實是0,因爲默認是整數除法,所以你應該使它成爲「1.0/52」來強制浮點運算,在Python 3.x這是自動 –