1、问题

对全为 nan 的数组进行统计计算(比如 np.nanstd())时,会报错:

In[87]:
import numpy as nparr = [[5.0, 1.0, 6.0, np.nan], [2.0, 2.0, 1.0, np.nan], [np.nan, np.nan, np.nan, np.nan], [3.0, 4.0, np.nan, np.nan], [np.nan, np.nan, np.nan, np.nan]]np.nanstd(arr, axis=1)
Out[87]:
D:\Anaconda531\lib\site-packages\numpy\lib\nanfunctions.py:1628: RuntimeWarning: Degrees of freedom <= 0 for slice.keepdims=keepdims)
Out[16]: array([2.1602469 , 0.47140452,        nan, 0.5       ,        nan])

经过检查,发现对全为nan的数据进行统计计算时,会触发这个警告。

In[88]:
k = arr[:, 3]
print(k)
np.nanstd(arr[:, 3])
Out[85]:
[nan nan nan nan nan]
nan
D:\Anaconda531\lib\site-packages\numpy\lib\nanfunctions.py:1628: RuntimeWarning: Degrees of freedom <= 0 for slice.keepdims=keepdims)

如何消除它?

2、codes
In[88]:
def calc_std2(arr: np.ndarray) :std = []for row in range(arr.shape[0]):k = arr[row, :]bool_arr = np.isfinite(k)chk = not any(bool_arr)if chk:k = np.nan_to_num(k, copy=True)st = np.nanstd(k)if chk:st = np.nanstd.append(st)return stdcalc_std2(arr)
Out[88]:
Out[86]: [2.160246899469287, 0.4714045207910317, nan, 0.5, nan]
3、效率比较

我们将数据 arr 扩大3000倍(数组扩充到15000行,4列),然后比较两种方法的用时

from time import timearr2 = np.tile(arr, 3000).reshape(150, 4)t1 = time()
res1 = list(np.nanstd(arr2, axis=1))
print(f'方法一用时 {time() - t1}')t1 = time()
res2 = calc_std2(arr2)
print(f'方法二用时 {time() - t1}')
D:\Anaconda531\lib\site-packages\numpy\lib\nanfunctions.py:1628: RuntimeWarning: Degrees of freedom <= 0 for slice.keepdims=keepdims)
方法一用时 0.0029916763305664062
方法二用时 1.0209534168243408

可以看到,数据增大后,方法二的耗时也大大增加。
因此,如果数据量比较大,而且没有洁癖的话,还是继续用方法一吧。

参考资料:
https://www.pythonheidong.com/blog/article/542591/394c0dd3e623551b01a1/

警告处理 RuntimeWarning: Degrees of freedom <= 0 for slice. keepdims=keepdims)相关推荐

  1. python经典字体警告:RuntimeWarning: Glyph missing from current font. font.set_text(s, 0.0, flags=flags)

    文章目录 前言 python经典字体警告:RuntimeWarning: Glyph missing from current font. font.set_text(s, 0.0, flags=fl ...

  2. 统计学中的自由度 Degrees of freedom (statistics)

    拾人牙慧: wiki:In statistics, the number of degrees of freedom is the number of values in the final calc ...

  3. python经典字体警告:RuntimeWarning: Glyph 33337 missing from current font. font.set_text(s, 0, flags=flag

    一.报错分析 所运行的YOLOv5 github地址:https://github.com/ultralytics/yolov5/ 在执行YOLOv5的val.py的时候 控制台有字体警告 打开生成的 ...

  4. c语言零错误零警告,C语言 g警告:无符号表达式的比较0始终为false

    要编译我的C代码,我使用-W标志,它会导致警告: warning: comparison of unsigned expression < 0 is always false 我认为这被视为一个 ...

  5. 微信小程序 调试器的 console提示框 的 无用黄色警告关闭 --- 根据 sitemap 的规则[0],当前页面 [pages/index/index] 将被索引

    内容 Sat Jul 11 2020 17:51:58 GMT+0800 (中国标准时间) sitemap 索引情况提示 VM18961:1 根据 sitemap 的规则[0],当前页面 [pages ...

  6. php模块出现警告,PHP警告:模块已在第0行的Unknown中加载

    在运行OSA php命令的Mac OSX Mavericks上,每当我运行php命令时,我都会收到以下错误消息(一切正常,这很烦人) PHP Warning: Module 'intl' alread ...

  7. Tomcat出现警告:[RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion

    解决方案 tomcat.JDK.web.xml 对应关系,向下兼容 web.xml--version2.2--JDK1.1--Tomcat3.3 web.xml--version2.3--JDK1.3 ...

  8. Python 科学计算—— 数值问题

    1. RuntimeWarning: Degrees of freedom <= 0 for slice 切片(list,tuple或ndarray)的自由度 <= 0,如下: >& ...

  9. sklearn.feature_selection.VarianceThreshold 方差过滤踩过的坑

    报错信息: Input contains NaN, infinity or a value too large for dtype('float64'). Input X must be non-ne ...

最新文章

  1. Objective-C:GCC+GNUstep配置
  2. 安装maven之后,cmd提示mvn不是内部命令的解决办法
  3. GridView使用技巧.txt
  4. 一条SQL语句实现二进制到十进制的转换
  5. WCF技术剖析之二十一:WCF基本异常处理模式[中篇]
  6. RuoYiConfig中加入自定义属性值获取不到解决办法?
  7. 2020-01-14 IP/TCP/UDP 对应的RFC编号
  8. Content-Disposition的使用和注意事项
  9. java计算机毕业设计辅导员班级量化管理系统源码+mysql数据库+系统+lw文档+部署
  10. 解决Ubuntu16.04 wineQQ和wps office 不能输入中文的问题
  11. 软工实践 - 第十一次作业 Alpha 冲刺 (3/10)
  12. ZYNQ启动流程分析之BootROM
  13. 用NDK-r25交叉编译zlib-1.2.12
  14. python图片马赛克_python检测图片是否有马赛克内容
  15. 《戏妻族语不正》胡曾
  16. 立体仓库 堆垛机 输送机 智能物流 wcs在和客户的wms进行对接,是典型的智能仓库的案例
  17. 克己慎独 2008-9-23 13:32:00 (21ic)
  18. 蚂蚁金服大规模分布式事务实践和开源详解
  19. Android Studio无法下载
  20. 解决JDBC-ODBC驱动桥导致JVM crash.

热门文章

  1. 计算机音乐单恋一枝花,单恋一枝花-张宇
  2. Github | 如何在Github上只下载一个文件或文件夹!?
  3. Java OOP 第二章 继承
  4. 一个平庸程序员自白:我不牛逼但那又怎样?
  5. 如何把mo3格式的文件转换成mp3格式
  6. java 全盘搜索文件_java递归思想实现全盘搜索文件之高速版
  7. 国产化服务器内网安装onlyoffice
  8. 年度最抓马的恋综《单身即地狱》,里面缺了它
  9. JavaSE入门0之java起源与发展历程
  10. “闪购”神话的牛皮吹出了泡沫