np.where()[0] 和 np.where()[1]的具体使用
作者:ysh1026 发布时间:2023-04-21 20:21:29
本文主要介绍了np.where()[0] 和 np.where()[1]的具体使用,以及np.where()的具体用法,废话不多说,具体如下:
import numpy as np
a = np.arange(12).reshape(3,4)
print('a:', a)
print('np.where(a > 5):', np.where(a > 5))
print('a[np.where(a > 5)]:', a[np.where(a > 5)])
print('np.where(a > 5)[0]:', np.where(a > 5)[0])
print('np.where(a > 5)[1]:', np.where(a > 5)[1])
print(a[np.where(a > 5)[0], np.where(a > 5)[1]])
a: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
np.where(a > 5): (array([1, 1, 2, 2, 2, 2]), array([2, 3, 0, 1, 2, 3]))
a[np.where(a > 5)]: [ 6 7 8 9 10 11]
np.where(a > 5)[0]: [1 1 2 2 2 2]
np.where(a > 5)[1]: [2 3 0 1 2 3]
[ 6 7 8 9 10 11]
np.where()[0] 表示行索引,np.where()[1]表示列索引
numpy.where() 有两种用法:
1. np.where(condition, x, y)
满足条件(condition),输出x,不满足输出y。
如果是一维数组,相当于[xv if c else yv for (c,xv,yv) in zip(condition,x,y)]
>>> aa = np.arange(10)
>>> np.where(aa,1,-1)
array([-1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # 0为False,所以第一个输出-1
>>> np.where(aa > 5,1,-1)
array([-1, -1, -1, -1, -1, -1, 1, 1, 1, 1])
>>> np.where([[True,False], [True,True]], # 官网上的例子
[[1,2], [3,4]],
[[9,8], [7,6]])
array([[1, 8],
[3, 4]])
上面这个例子的条件为[[True,False], [True,False]],分别对应最后输出结果的四个值。第一个值从[1,9]中选,因为条件为True,所以是选1。第二个值从[2,8]中选,因为条件为False,所以选8,后面以此类推。类似的问题可以再看个例子:
>>> a = 10
>>> np.where([[a > 5,a < 5], [a == 10,a == 7]],
[["chosen","not chosen"], ["chosen","not chosen"]],
[["not chosen","chosen"], ["not chosen","chosen"]])
array([['chosen', 'chosen'],
['chosen', 'chosen']], dtype='<U10')
2. np.where(condition)
只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标 (等价于numpy.nonzero)。这里的坐标以tuple的形式给出,通常原数组有多少维,输出的tuple中就包含几个数组,分别对应符合条件元素的各维坐标。
>>> a = np.array([2,4,6,8,10])
>>> np.where(a > 5) # 返回索引
(array([2, 3, 4]),)
>>> a[np.where(a > 5)] # 等价于 a[a>5]
array([ 6, 8, 10])
>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))
上面这个例子条件中[[0,1],[1,0]]的真值为两个1,各自的第一维坐标为[0,1],第二维坐标为[1,0] 。
下面看个复杂点的例子:
>>> a = np.arange(27).reshape(3,3,3)
>>> a
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
>>> np.where(a > 5)
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]),
array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]))
# 符合条件的元素为
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]]
所以np.where会输出每个元素的对应的坐标,因为原数组有三维,所以tuple中有三个数组。
需要注意的一点是,输入的不能直接是list,需要转为array或者为array才行。比如range(10)和np.arange(10)后者返回的是数组,使用np.where才能达到效果。
来源:https://blog.csdn.net/ysh1026/article/details/109559981


猜你喜欢
- 导入pandas模块:import pandas as pd使用import读入pandas模块,并且为了方便使用其缩写pd指代。读入待处理
- Python pip安装lxml出错的问题解决办法1. 在使用pip安装lxml过程中出现了一下错误: &
- 环境:Ubuntu16.4 python版本:3.6.4 库:wordcloud这次我们要讲的是爬取QQ音乐的评论并制成云词图,我们这里拿周
- 1、时间戳转换为指定格式日期import timet = time.strftime("%Y-%m-%d %H:%M:%S&quo
- 本文实例讲述了Python网络编程基于多线程实现多用户全双工聊天功能。分享给大家供大家参考,具体如下:在前面一篇《Python网络编程使用s
- 在django的views中不论是用类方式还是用装饰器方式来使用rest框架,django_rest_frame实现权限管理都需要两个东西的
- 详细介绍Scrapy shell的使用Scrapy shell是Scrapy框架提供的一个非常有用的工具,可以帮助开发者快速地测试和调试Sc
- 那么四年一度的世界杯即将要在卡塔尔开幕了,对于不少热爱足球运动的球迷来说,这可是十分难得的盛宴,而对于最后大力神杯的归属,相信很多人都满怀着
- 目录1.导入tf.keras2.构建简单模型2.1模型堆叠2.2网络配置3.训练和评估3.1设置训练流程3.2输入Numpy数据3.3tf.
- 前言深度学习涉及很多向量或多矩阵运算,如矩阵相乘、矩阵相加、矩阵-向量乘法等。深层模型的算法,如BP,Auto-Encoder,CNN等,都
- 本文实例为大家分享了js实现本地持久化存储登录注册的具体代码,供大家参考,具体内容如下1.登录html文件<!DOCTYPE html
- 1、jieba库基本介绍(1)、jieba库概述jieba是优秀的中文分词第三方库- 中文文本需要通过分词获得单个的词语- jieba是优秀
- 方法一【推荐】、用js插入flash,可防止虚线框激活建立一个ShowFlash.js文件,拷贝以下代码:function sho
- public static char doVerify(String id) { char pszSrc[]=id.toCharArray(
- mysql安装好经常发现无法正常启动碰到最多的是error 2003的错误,以下为解决方法: mysqld -nt -remove mysq
- 1. 简介 追踪某些软件运行时所发生事件的方法, 可以在代码中调用日志中某些方法来记录发生的事情一个事件可以用一个可包含可选变量数
- ASP有一个最重要的功能,就是它可以让你非常轻松地连接数据库。通常都是和一个Access或者一个SQL数据库相连。因为Access是最容易起
- 前言Kettle下载与安装保姆级教程(最新)Kettle下载安装pdi-ce-7.1.0.0-12教程win10环境安装kettle与lin
- 报错信息应该是这样的webpackEmptyContext (eval at ./src/store/modules sync recurs
- 压缩数据库文件可以提高数据库的性能,但是有些时候在压缩数据库时,系统会提醒用户该数据库不能压缩。如果在Access数据库中删除数据库对象,或