numpy-查找操作大全
本文记录日常工作中遇到的查找操作,持续更新。
注意:输入必须是 数组,不能是 list
极值
min,max 返回极值
argmin(a, axis=None, out=None), 返回极值所在的位置;不带 axis,先拉直,再找极值;带 axis,找某个维度的极值
b = np.array([[1, 2, 3, 5], [4, 6, 2, 6]])
print(np.max(b)) # 返回最大值 6
print(np.min(b)) # 返回最小值 1
print(np.argmax(b)) # 返回第一个最大值的位置 5
print(np.argmin(b)) # 返回第一个最小值的位置 0 print(np.argmin(b, axis=1)) # [0 2]
NaN值
nan 值由多种表达形式,如 None,np.nan,np.NaN等
isnan,输入可以是 一维,也可以是 二维,返回布尔索引
x = np.array(range(10), dtype=np.float)
y = np.array(range(10,20))
print(x.shape) # (10,)
print(x) # [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
print(y) # [10 11 12 13 14 15 16 17 18 19]
x[3] = None # 插入 nan
x[5] = np.NaN # 插入 nan
print(x) # [ 0. 1. 2. nan 4. nan 6. 7. 8. 9.] # isnan 返回索引
print(np.isnan(x)) # [False False False True False True False False False False]
print(y[np.isnan(x)]) # [13 15]
print(y[~np.isnan(x)]) # [10 11 12 14 16 17 18 19]
如果想返回数值索引,可如下操作
data4 = np.array([1, 3, np.nan, 5]) ## isnan 返回 nan 值的布尔下标
print np.isnan(data4) # [False False True False] ## where 找到 nan 值的数值下标
print np.where(np.isnan(data4)) # (array([2]),)
print np.where(~np.isnan(data4)) # (array([0, 1, 3]),)
注意,nan 值 不能用 where 查找
print(np.where(x != np.NaN)) # (array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),) 这样不行
经常遇到这么一个错误
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
错误原因:有异常的数据类型,非 int float
解决方法:转换数据类型,.astype('float')
where 条件
where,返回tuple,第一个值是索引,第二个是空值
1. 输入必须是 数组,不能是 list
2. 输入一般是一维,行向量或者列向量都可以
3. 输入多维,将返回两个索引,行向量或者列向量返回不同
argwhere,直接返回索引,返回为二维数组,列向量
# list 返回错误
data = range(10)
print np.where(data>6) # (array([0]),) # 一维数组
data1 = np.array(range(0, 20, 2))
print np.where(data1>6) # (array([7, 8, 9]),)
print np.where(data1.T>6) # (array([7, 8, 9]),) # 二维数组
data2 = np.array([range(0, 20, 2)])
print np.where(data2>6) # (array([0, 0, 0]), array([7, 8, 9])) # 多行多列
data3 = np.array([range(10), range(10)])
print(data3)
print np.where(data3>6) # (array([0, 0, 0, 1, 1, 1]), array([7, 8, 9, 7, 8, 9]))
print np.where(data3.T>6) # (array([7, 7, 8, 8, 9, 9]), array([0, 1, 0, 1, 0, 1])) ## argwhere 直接返回索引
print np.argwhere(data1>6)
# [[4]
# [5]
# [6]
# [7]
# [8]
# [9]]
print np.argwhere(data1.T>6)
# [[4]
# [5]
# [6]
# [7]
# [8]
# [9]]
where 也可输入多个条件
# 求公共部分
print np.intersect1d([1, 4, 3], [3, 4, 5]) # [3 4] # 多个条件
data2 = np.array([1,5, 11,16,20])
print np.where(data2>10) # (array([2, 3, 4]),) print np.where((data2>10) & (data2<18)) # (array([2, 3]),)
print np.where(np.logical_and(data2>10, data2<18)) # (array([2, 3]),)
print np.intersect1d(np.where(data2>10)[0], np.where(data2<18)[0]) # [2 3]
extract 条件
extract(condition, arr),按某条件查找,返回元素
print(np.extract(np.isnan(x), x)) # [nan nan]
print(np.extract(np.isnan(x), y)) # [13 15]
print(np.extract(x>8, x)) # [9.]
非0元素
nonzero,返回tuple,第一个值是索引,第二个是空值
x = [1, 0, 3, 0]
print(np.nonzero(x)) # (array([0, 2]),)
未完待续...
numpy-查找操作大全的更多相关文章
- python中numpy矩阵运算操作大全(非常全)!
python中numpy矩阵运算操作大全(非常全) //2019.07.10晚python矩阵运算大全1.矩阵的输出形式:对于任何一个矩阵,python输出的模板是:import numpy as n ...
- PHP数组操作大全
<?php /** * File: phpstudy : array_test.php * Created by PhpStorm. * User: IhMfLy Pheonix@jtv-070 ...
- SQL语句操作大全
SQL语句操作大全 本文分为以下六个部分: 基础部分 提升部分 技巧部分 数据开发–经典部分 SQL Server基本函数部分 常识部分 一.基础 1.说明:创建数据库CREATE DATABAS ...
- MATLAB命令大全和矩阵操作大全
转载自: http://blog.csdn.net/dengjianqiang2011/article/details/8753807 MATLAB矩阵操作大全 一.矩阵的表示在MATLAB中创建矩阵 ...
- numpy 基础操作
Numpy 基础操作¶ 以numpy的基本数据例子来学习numpy基本数据处理方法 主要内容有: 创建数组 数组维度转换 数据选区和切片 数组数据计算 随机数 数据合并 数据统计计算 In [1]: ...
- SQLite3命令操作大全
SQLite3命令操作大全 SQLite库包含一个名字叫做sqlite3的命令行,它可以让用户手工输入并执行面向SQLite数据库的SQL命令.本文档提供一个样使用sqlite3的简要说明. 一.ql ...
- Numpy 数组操作
Numpy 数组操作 Numpy 中包含了一些函数用于处理数组,大概可分为以下几类: 修改数组形状 翻转数组 修改数组维度 连接数组 分割数组 数组元素的添加与删除 修改数组形状 函数 描述 resh ...
- MATLAB矩阵操作大全
转载自:http://blog.csdn.net/dengjianqiang2011/article/details/8753807 MATLAB矩阵操作大全 一.矩阵的表示 在MATLAB中创建矩阵 ...
- 二叉排序树(BST)创建,删除,查找操作
binary search tree,中文翻译为二叉搜索树.二叉查找树或者二叉排序树.简称为BST 一:二叉搜索树的定义 他的定义与树的定义是类似的,也是一个递归的定义: 1.要么是一棵空树 2.如果 ...
- Delphi Excel 操作大全
Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...
随机推荐
- maven安装问题解决
出现: The JAVA_HOME environment variable is not defined correctly This environment variable is needed ...
- JDK_API剖析之java.lang包
java.lang是Java语言的基础包,默认包中的所有来自动import到你写的类中.(按照字母排序) 1.AbstractMethodError 类.错误 自1.0开始有 继承自Incompati ...
- VMware Guest customization fails on Linux
1.1 症状现象 登录Guest OS,在/var/log/vmware-imc/toolsDeployPkg.log文件中,您会看到以下条目: Customization command fail ...
- Unity3D_(数据)JsonUtility创建和解析Json
Json 百度百科:传送门 LitJson创建和解析Json 传送门 Json数据解析在Unity3d中的应用 传送门 一.使用JsonUnity创建Json using System.Collect ...
- C++入门经典-例4.1-声明、定义和使用函数
1:代码如下: // 4.1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using ...
- jQuery .prop()
.prop() .prop( propertyName )Returns: Anything Description: Get the value of a property for the firs ...
- 认识一下java神器Btrace
转载: http://calvin1978.blogcn.com/articles/btrace1.html BTrace是神器,每一个需要每天解决线上问题,但完全不用BTrace的Java工程师,都 ...
- asp.net 获取服务器及客户端的相关信息
1. 在ASP.NET中专用属性:获取服务器电脑名:Page.Server.ManchineName获取用户信息:Page.User获取客户端电脑名:Page.Request.UserHostName ...
- HttpURLConnection 和HttpClient 哪个好
最近在研究Volley框架的源码,发现它在HTTP请求的使用上比较有意思,在Android 2.3及以上版本,使用的是HttpURLConnection,而在Android 2.2及以下版本,使用的是 ...
- PHP CI框架数据传递渲染
实例: //控制器 class Index extends CI_Controller { //因为类名是特殊字,所以为了运行正常添加构造函数 function __construct(){ pare ...