Python中numpy的where()函数

摘要:
第一种用法np.whereif:数组变xelse:数组变yimportnumpyasnp'''x=np.random.randn(4,4)print#试试效果xarr=np.arrayyarr=np.arrayzarr=np.arrayresult=[forx,y,cinzip]print#where()函数处理就相当于上面那种方案result=np.whereprint'''#发现个有趣的东西##处理2组数组##TrueandTrue=0##TrueandFalse=1##FalseandTrue=2##FalseandFalse=3cond2=np.arraycond1=np.array#第一种处理太长太丑result=[]foriinrange:if:result.append;elif:result.append;elif:result.append;else:result.append;print#第二种直接where()很快很方便result=np.whereprint#第三种更简便(好像这跟where()函数半毛钱的关系都没有result=1*(cond1&-cond2)+2*(cond2&-cond1)+3*(-(cond1|cond2))(没想到还可以这么表达吧)print(result)  ViewCode第二种用法where(conditions)相当于给出数组的下标x=np.arange(16)print(x[np.where(x˃5)])#输出:(array([6,7,8,9,10,11,12,13,14,15],dtype=int64),)x=np.arange(16).reshape(-1,4)print(np.where(x˃5))#(array([1,1,2,2,2,2,3,3,3,3],dtype=int64),array([2,3,0,1,2,3,0,1,2,3],dtype=int64))#注意这里是坐标是前面的一维的坐标,后面是二维的坐标ViewCodeix=np.array([[False,False,False],[True,True,False],[False,True,False]],dtype=bool)print(np.where(ix))#输出:(array([1,1,2],dtype=int64),array([0,1,1],dtype=int64))ViewCode

第一种用法

np.where(conditions,x,y)

if (condituons成立):

数组变x

else:

数组变y

importnumpy as np
'''x = np.random.randn(4,4)
print(np.where(x>0,2,-2))
#试试效果
xarr = np.array([1.1,1.2,1.3,1.4,1.5])
yarr = np.array([2.1,2.2,2.3,2.4,2.5])
zarr = np.array([True,False,True,True,False])
result = [(x if c else y)
          for x,y,c in zip(xarr,yarr,zarr)]
print(result)

#where()函数处理就相当于上面那种方案

result = np.where(zarr,xarr,yarr)
print(result)

'''
#发现个有趣的东西##处理2组数组##True and True = 0##True and False = 1##False and True = 2##False and False = 3
cond2 =np.array([True,False,True,False])
cond1 =np.array([True,True,False,False])
#第一种处理 太长太丑
result =[]
for i in range(4):
    if (cond1[i] &cond2[i]):   result.append(0);
    elif (cond1[i]):    result.append(1);
    elif (cond2[i]):    result.append(2);
    else :  result.append(3);
print(result)
#第二种 直接where() 很快很方便
result = np.where(cond1 & cond2,0,np.where(cond1,1,np.where(cond2,2,3)))
print(result)
#第三种 更简便(好像这跟where()函数半毛钱的关系都没有
result = 1*(cond1 & -cond2)+2*(cond2 & -cond1)+3*(-(cond1 |cond2))  (没想到还可以这么表达吧)
print(result)
  
View Code

第二种用法

where(conditions)

相当于给出数组的下标

x = np.arange(16)
print(x[np.where(x>5)])
#输出:(array([ 6,  7,  8,  9, 10, 11, 12, 13, 14, 15], dtype=int64),)
x = np.arange(16).reshape(-1,4)
print(np.where(x>5))

#(array([1, 1, 2, 2, 2, 2, 3, 3, 3, 3], dtype=int64), array([2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))#注意这里是坐标是前面的一维的坐标,后面是二维的坐标
View Code
ix =np.array([[False, False, False],
       [ True,  True, False],
       [False,  True, False]], dtype=bool)
print(np.where(ix))
#输出:(array([1, 1, 2], dtype=int64), array([0, 1, 1], dtype=int64))
View Code

免责声明:文章转载自《Python中numpy的where()函数》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇摄像头测距shell脚本之sed详解(1)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python Pystaller 将python文件打包成exe

1、PyInstaller是一个第三方库,通过对源文件打包,Python程序可以在没有安装 Python的环境中运行。 2、在命令行使用pip install pyinstaller安装即可。(win+r,输入cmd,打开命令行) 3、将cmd的目录切换至(命令:cd 文件路径(注意空格))需要打包的py文件目录下(打包生成的exe 跟 cmd 启动目录有...

2.变量

变量 变量的组成 变量的组成分为以下三个部分: 变量名:变量名用来引用变量值,但凡需要用变量值,都需要通过变量名。 赋值符号:赋值 变量值:存放数据,用来记录现实世界中的某种状态。 变量的命名应该满足以下三个规范: 变量的命名应该能反映变量值所描述的状态,切记不可用中文 变量名必须用字母数字下划线组合,并且变量名的第一个字符不能是数字。 关键字不能声...

pycharm快捷键、常用设置、配置管理

http://blog.csdn.net/pipisorry/article/details/39909057 pycharm学习技巧 Learning tips /pythoncharm/help/tip of the day:A special variant of the Code Completion feature invoked by pres...

《Python》并发编程

手工操作 —— 穿孔卡片       1946年第一台计算机诞生--20世纪50年代中期,计算机工作还在采用手工操作方式。此时还没有操作系统的概念。             程序员将对应于程序和数据的已穿孔的纸带(或卡片)装入输入机,然后启动输入机把程序和数据输入计算机内存,接着通过控制台开关启动程序针对数据运行;计算完毕,打印机输出计算结果;用户取走结果...

转载:pyqt线程间通过 信号/槽 通信

转自:http://blog.sina.com.cn/s/blog_613d5bb701016qzv.html 信号(singal)与槽(slot)用于对象相互通信,信号:当某个对象的某个事件发生时,触发一个信号,槽:响应指定信号的所做的反应,其实信号槽类似于.NET里面的委托、事件,比如Repeater控件类,当行数据绑定后,触发一个ItemDataBo...

吴裕雄 python 机器学习——模型选择参数优化随机搜索寻优RandomizedSearchCV模型

import scipy from sklearn.datasets import load_digits from sklearn.metrics import classification_report from sklearn.linear_model import LogisticRegression from sklearn.model_sel...