2012-09-01 16 views
0

我寫了這個代碼來計算的模式和標準偏差爲一個大樣本:代碼工作,但有「DeprecationWarning」 -scipy.stats

import numpy as np 
import csv 
import scipy.stats as sp 
import math 

r=open('stats.txt', 'w') #file with results 
r.write('Data File'+'\t'+ 'Mode'+'\t'+'Std Dev'+'\n') 
f=open('data.ls', 'rb') #file with the data files 

for line in f: 
    dataf=line.strip() 
    data=csv.reader(open(dataf, 'rb')) 
    data.next() 
    data_list=[] 
    datacol=[] 
    data_list.extend(data) 
    for rows in data_list: 
      datacol.append(math.log10(float(rows[73]))) 
    m=sp.mode(datacol) 
    s=sp.std(datacol) 
    r.write(dataf+'\t'+str(m)+'\t'+str(s)+'\n') 
    del(datacol) 
    del(data_list) 

這是運作良好 - 我想!然而,在我運行代碼後,終端上出現錯誤消息,我想知道是否有人能告訴我這是什麼意思?

/usr/lib/python2.6/dist-packages/scipy/stats/stats.py:1328: DeprecationWarning:  scipy.stats.std is deprecated; please update your code to use numpy.std. 
    Please note that: 
    - numpy.std axis argument defaults to None, not 0 
    - numpy.std has a ddof argument to replace bias in a more general manner. 
     scipy.stats.std(a, bias=True) can be replaced by numpy.std(x, 
     axis=0, ddof=0), scipy.stats.std(a, bias=False) by numpy.std(x, axis=0, 
     ddof=1). 
    ddof=1).""", DeprecationWarning) 
/usr/lib/python2.6/dist-packages/scipy/stats/stats.py:1304: DeprecationWarning: scipy.stats.var is deprecated; please update your code to use numpy.var. 
Please note that: 
    - numpy.var axis argument defaults to None, not 0 
    - numpy.var has a ddof argument to replace bias in a more general manner. 
     scipy.stats.var(a, bias=True) can be replaced by numpy.var(x, 
     axis=0, ddof=0), scipy.stats.var(a, bias=False) by var(x, axis=0, 
     ddof=1). 
    ddof=1).""", DeprecationWarning) 
/usr/lib/python2.6/dist-packages/scipy/stats/stats.py:420: DeprecationWarning: scipy.stats.mean is deprecated; please update your code to use numpy.mean. 
Please note that: 
    - numpy.mean axis argument defaults to None, not 0 
    - numpy.mean has a ddof argument to replace bias in a more general manner. 
     scipy.stats.mean(a, bias=True) can be replaced by numpy.mean(x, 
axis=0, ddof=1). 
    axis=0, ddof=1).""", DeprecationWarning) 
+0

問題出在'scipy/stats'上,而不是你看到的代碼 – imkost

+0

@imkost,這是我需要擔心的事情嗎? – Labibah

+0

我的錯誤,對不起。錯誤不在'scipy/stats'中。我猜''data.ls'有問題。 – imkost

回答

5

這些都是deprecation warnings,這通常意味着你的代碼將工作,但可能會停止在未來版本中工作。

您目前有這條線s=sp.std(datacol)。它看起來像警告建議使用numpy.std()而不是scipy.stats.std()進行此更改可能會使此警告消失。

如果您不關心棄用警告並想按原樣使用您的代碼,則可以使用warnings模塊將其禁用。舉例來說,如果你有一個函數fxn()產生一個DeprecationWarning,你可以像這樣把它包裝:

with warnings.catch_warnings(): 
    warnings.simplefilter("ignore") 
    fxn() #this function generates DeprecationWarnings 
+0

我使用了numpy std,並且警告根本沒有顯示 – Labibah

+0

。在numpy/scipy列表中有關於包之間的不一致的評論,比如'var','std' ...函數/方法的不同默認參數。 –

3

DeprecationWarnings不會阻止你的代碼才能正常運行,他們僅僅是警告,該代碼使用的是很快就會被棄用,您應該將其更新爲正確的語法。

在這種特殊情況下,它起因於NumPy和SciPy在var,std ...函數/方法的默認參數之間的不一致性。爲了清理事情,決定從scipy.stats中刪除這些函數,並使用它們的NumPy對應函數。

當然,只是放棄這些函數會使一些代碼突然失效的用戶感到不安。因此,SciPy開發者決定在幾個版本中加入一個DeprecationWarning,這將爲每個人留出足夠的時間來更新他們的代碼。

在你的情況,你應該使用檢查的scipy.stats.std文檔字符串您的系統上,看看他們正在使用的默認值,並按照有關如何相應地修改代碼的警告提示。

相關問題