首页 > 其他 > 详细

三元运算符 np.where,裁剪clip

时间:2020-03-29 18:18:34      阅读:51      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 np.where(t<5,0,1)的意思就是:把t中小于5的替换成0,其他的替换成1.

 

 

 

技术分享图片

 

 t.clip(5,8)的意思就是:把t中小于5的替换为5,大于8的替换为8。

 

一个用列平均值来替换nan的小例子:

 1 import numpy as np
 2 def fill_ndarray(t1):
 3     for i in range(t1.shape[1]):   #一列一列的循环
 4         temp_col = t1[:,i]
 5         nan_num = np.count_nonzero(temp_col!=temp_col)
 6         if nan_num !=0:  #不为0 说明当前这一列有nan
 7             tem_not_nan_col = temp_col[temp_col==temp_col] #当前一列不为nan的array
 8             #选中当前nan的位置,替换为当前列不为nan的值的均值
 9             temp_col[np.isnan(temp_col)] = tem_not_nan_col.mean()
10     return t1
11 
12 if __name__ == __main__:
13     t1 = np.arange(12).reshape(3,4).astype(float)
14     t1[1,2:] = np.nan
15     t2 = fill_ndarray(t1)
16     print(t2)

 

三元运算符 np.where,裁剪clip

原文:https://www.cnblogs.com/GouQ/p/12593450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!