1. 数组的集合运算

1.1. 并集

np.union1d(a,b)计算数组的并集:

In [1]: import numpy as np
In [2]: a = np.array([1,2,3])
In [3]: b = np.array([3,4,5])
In [4]: np.union1d(a,b)
Out[4]: array([1, 2, 3, 4, 5])

1.2. 交集

np.intersect1d(a,b)计算数组的交集:

In [10]: import numpy as np
In [11]: a = np.array([2,3,4,5])
In [12]: b = np.array([3,5,6])
In [13]: np.intersect1d(a,b)
Out[13]: array([3, 5])

1.3. 差集

np.setdiff1d(a,b)计算结果为a集合减去b集合,也就是剩下没有在b中出现的元素:

In [15]: import numpy as np
In [16]: a = [1,1,2,2,3,4,5]
In [17]: b = [2,5]
In [18]: np.setdiff1d(a,b)
Out[18]: array([1, 3, 4])

1.4. 异或

np.setxor1d(a,b)计算a、b集合的异或结果。

In [15]: import numpy as np
In [16]: a = [1,1,2,2,3,4,5]
In [17]: b = [2,5]
In [26]: np.setxor1d(a,b)
Out[26]: array([1, 3, 4])

可以注意到,一般对两个数组进行集合操作,会对数组自动进行去重。

2. np.where函数

2.1. 第一种用法

where(c, a, b),是numpy风格的if-else三元运算符。它通过一个布尔数组(c)和两个其他数组(a和b)得出数组d,数组d满足:如果c[i]为真,则d[i]=a[i],否则d[i]=b[i]。三个数组必须具有相同的形状。

例1:

In [30]: x = np.random.randn(4,4)
In [31]: x
Out[31]:
array([[ 0.47223924, -0.74427759, -1.67279362, 0.01203576],
[-0.83590182, 1.70742332, -1.01150273, 0.50330454],
[ 0.76771725, -1.38832902, -0.62673363, 0.27442247],
[-1.01199694, -0.65573435, 0.89553293, -0.74830605]])
In [32]: np.where(x>0,2,-2)
Out[32]:
array([[ 2, -2, -2, 2],
[-2, 2, -2, 2],
[ 2, -2, -2, 2],
[-2, -2, 2, -2]])

例2:

In [33]: 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])
In [34]: np.where(zarr,xarr,yarr)
Out[34]: array([1.1, 2.2, 1.3, 1.4, 2.5])

2.2. 第二种用法

where(conditions) ,相当于给出符合条件的数组元素的下标。

例子1:

In [37]: x = np.random.randn(4)
In [38]: x
Out[38]: array([-0.0860867 , 1.42173872, -1.44341208, 0.61600628])
In [39]: np.where(x>0)
Out[39]: (array([1, 3], dtype=int64),)

例子2:如果想要求a集合中的元素在b中也存在,这样的下标,需要用到np.in1d(a,b)或者np.isin(a,b):

In [40]: a = np.array([1,2,4,5,6])
In [41]: b = np.array([2,6])
In [42]: np.where(np.in1d(a, b))
Out[42]: (array([1, 4], dtype=int64),)

In [46]: np.where(np.isin(a,b))
Out[46]: (array([1, 4], dtype=int64),)

可以观察到,where(conditions)取下标,返回的是一个元组,如果需要取这个下标数组需要通过结果result[0]去取或者(result,)=np.where(conditions)

3. 参考

(1) Numpy中的集合运算

(2) python – Numpy:查找另一个数组中出现的一个数组中元素的索引

(完)

numpy 数组集合运算及下标操作的更多相关文章

  1. 5_PHP数组_3_数组处理函数及其应用_9_数组集合运算函数

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组集合运算函数 1. array_merge() 函数 程序: <?php $array1 = array ...

  2. numpy数组的运算

    numpy数组的运算 数组的乘法 >>> import numpy as np >>> arr=np.array([[1,2,3],[4,5,6]]) >&g ...

  3. numpy数组之读写文件

    目录 通过 numpy 读写 txt 或 csv 文件 通过 numpy 读写 npy 或 npz 文件 读写 npy 文件 读写 npz 文件 通过 h5py 读写 hdf5 文件 简单读取 通过切 ...

  4. SQL集合运算参考及案例(一):列值分组累计求和

    概述 目前企业应用系统使用的大多数据库都是关系型数据库,关系数据库依赖的理论就是针对集合运算的关系代数.关系代数是一种抽象的查询语言,是关系数据操纵语言的一种传统表达方式.不过我们在工作中发现,很多人 ...

  5. numpy数组的操作

    numpy - 介绍.基本数据类型.多维数组ndarray及其内建函数 http://blog.csdn.net/pipisorry/article/details/22107553 http://w ...

  6. Numpy数组对象的操作-索引机制、切片和迭代方法

    前几篇博文我写了数组创建和数据运算,现在我们就来看一下数组对象的操作方法.使用索引和切片的方法选择元素,还有如何数组的迭代方法. 一.索引机制 1.一维数组 In [1]: a = np.arange ...

  7. Numpy数组的基本运算操作

    一.算术运算符 In [3]: a = np.arange(0,5) Out[3]array([0, 1, 2, 3, 4]) In [4]: a+4 Out[4]: array([4, 5, 6, ...

  8. 操作 numpy 数组的常用函数

    操作 numpy 数组的常用函数 where 使用 where 函数能将索引掩码转换成索引位置: indices = where(mask) indices => (array([11, 12, ...

  9. NumPy 中的集合运算

    怎样快速找出两个数组中相同的元素? numpy.isin(element,test_elements,assume_unique = False,invert = False ) 计算test_ele ...

随机推荐

  1. 性能测试学习第十天-----性能案例分析之CPU消耗过高&响应时间较长

    一.现象  /pinter/case/cpu?type=1   使用google的gjson.tojson性能较差    type=2 使用性能好的阿里巴巴的fastjson库 压测过程中,发现应用服 ...

  2. css,js 学习记录

    记录一些自己曾经阅读,值得收藏的网址 --(css3新特性) https://segmentfault.com/a/1190000010780991#articleHeader41 --CSS3 3D ...

  3. C语言JSON序列化/反序列化

    最近想找一个C语言处理嵌套结构体和结构体数组的json库,理想的是能够很容易处理复杂结构体嵌套,并且使用简单的,但是没找到比较合适的,于是打算自己封装一个: 两个问题: C语言结构体本身没有元数据,这 ...

  4. python反射hasattr getattr setattr delattr

    反射 : 是用字符串类型的名字 去操作 变量 相比于用eval('print(name)') 留有 安全隐患 反射 就没有安全问题 hasattr 语法: hasattr(object, name)o ...

  5. python 与开源Gis 书本知识点测试

    # -*- coding: utf-8 -*- print(u"python与开源QGis课题研究组")#print("汉字") #++++++++++++++ ...

  6. Problems with Localtime

    http://pytz.sourceforge.net/#problems-with-localtime https://docs.djangoproject.com/en/2.2/topics/i1 ...

  7. 范围指示器Extent Indicators

    范围指示器Extent Indicators 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com   商务合作,科技咨询,版权转让:向日葵,135- ...

  8. UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

    需求:python如何实现普通用户登录服务器后切换到root用户再执行命令 解决参考: 代码: def verification_ssh(host,username,password,port,roo ...

  9. android ------ 实现高德定位并获取相应信息 ( 最新版高德SDK 和 Android SDK版本)

    Android开发项目时常常会遇到定位这个功能, 很久以前写过一篇了,官方也更新了一些东西,我也更新下 以前使用的是jar包 导入来实现高德定位 老版本 链接:https://www.cnblogs. ...

  10. flutter 运行别人项目 包无法导入报错:Target of URI doesn't exist 'package:flutter/material.dart' 解决方法

    命令行里运行  flutter packages get