我编写了这段代码来计算大样本的模式和标准偏差:
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 :(得分:5)
这些是deprecation warnings,这通常意味着您的代码可以运行,但可能会在将来的版本中停止工作。
目前您拥有此行s=sp.std(datacol)
。看起来警告建议使用numpy.std()
代替scipy.stats.std()
进行此更改可能会使此警告消失。
如果您不关心弃用警告并希望按原样使用您的代码,则可以使用warnings模块对其进行抑制。例如,如果您有一个生成DeprecationWarning的函数fxn()
,您可以像这样包装它:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
fxn() #this function generates DeprecationWarnings
答案 1 :(得分:3)
DeprecationWarnings
不会阻止您的代码正常运行,它们只是警告您正在使用的代码将很快被弃用,并且您应该将其更新为正确的语法。
在这种特殊情况下,它源于NumPy和SciPy在var
,std
...函数/方法的默认参数上的不一致。为了清理它,决定从scipy.stats
中删除函数并使用它们的NumPy对应物。
当然,只是删除这些功能会让一些代码突然无法工作的用户感到不安。因此,SciPy开发人员决定在几个版本中包含一个DeprecationWarning
,这应该为每个人留下足够的时间来更新他们的代码。
在您的情况下,您应该检查系统上scipy.stats.std
的文档字符串以查看他们使用的默认值,并按照警告说明相应地修改代码。