使用statistics.stdev()时出现'numpy.float64' object has no attribute 'numerator'错误的排查求助
使用statistics.stdev()时出现'numpy.float64' object has no attribute 'numerator'错误的排查求助
我写了两个用来计算Cohen's D的函数,用于处理CSV文件的数据:
import statistics import numpy as np def cohen_d(a,b): float_a = [] float_b = [] for x in a: try: float_a.append(float(x)) except ValueError: return "NAN" for y in b: try: float_b.append(float(y)) except ValueError: return "NAN" try: res=(statistics.fmean(float_a) - statistics.fmean(float_b)) / statistics.stdev(float_a+float_b) except ZeroDivisionError: res=0 return res def cohen_d_np(a,b): float_a = [] float_b = [] for x in a: try: float_a.append(float(x)) except ValueError: return "NAN" for y in b: try: float_b.append(float(y)) except ValueError: return "NAN" float_a = np.array(float_a) float_b = np.array(float_b) try: res=(statistics.fmean(float_a) - statistics.fmean(float_b)) / statistics.stdev(np.concatenate([float_a,float_b])) except ZeroDivisionError: res=0 return res
当我在自己的数据上运行这些函数时,基于NumPy的版本会触发如下错误,而基于列表的版本则会报错'float' object has no attribute 'numerator':
RuleException: AttributeError in file /work/user/cburnard/PIPELINES/ChIPseq/Snakefile, line 111: 'numpy.float64' object has no attribute 'numerator' File "/work/user/cburnard/PIPELINES/ChIPseq/Snakefile", line 903, in __rule_calculate_multimapbw_indiv_scores File "/work/user/cburnard/PIPELINES/ChIPseq/Snakefile", line 162, in effect_size_mmb File "/work/user/cburnard/PIPELINES/ChIPseq/Snakefile", line 111, in cohen_d_np File "/tools/devel/python/Python-3.11.1/lib/python3.11/statistics.py", line 922, in stdev File "/tools/devel/python/Python-3.11.1/lib/python3.11/concurrent/futures/thread.py", line 58, in run
这个错误是怎么产生的?我不确定具体是哪一行数据导致的,但理论上即使存在非数值数据的问题,函数也应该返回"NAN",而不是抛出这个错误。
备注:内容来源于stack exchange,提问作者Whitehot
相关产品推荐
相关产品推荐

