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 ...
随机推荐
- wx.setStorage、wx.getStorage和wx.getStorageSync
Page({ data: { testnum:""//设置测试参数 }, test:function(){ var Num = this.data.testnum; wx.setS ...
- getFieldDecorator用法(一)——登录表单
之前使用antd的ui表单,却没发现这么好用的用法,推荐给大家 import React from "react"; import { Card, Form, Input, But ...
- Authing新功能——小程序扫码登录
近期,Authing 发布了新功能--小程序扫码登录. 小程序扫码登录指使用Authing小程序身份管家在网页端或其它客户端执行微信登录,目前的SDK仅支持客户端JavaScript.其它语言若想使用 ...
- mysql 查询每个分组的前几名
按分组排序,并查出每个分组的前3名 单表 SELECT * FROM ( SELECT ZONEID, uid, NAME, fight, IF ( , ) AS rank, ( @zone := z ...
- BytesWritable 长度问题(多出空格)
在使用 BytesWritable 进行小文件合并时,发现长度与原类容不一致,会多出一些空格 测试代码 @Test public void test() { String str = "aa ...
- 百度地图api根据地址获取经纬度
package com.haiyisoft.cAssistant;import java.io.BufferedReader;import java.io.IOException; import ja ...
- 【5】标题上的小logo
<link rel="shortcut icon" href="logo图片的路径"> shortcut --- 捷径,近路 icon --- ...
- flutter tabbar创建与显示
效果图 main.dart import 'package:flutter/material.dart'; import 'pages/index_page.dart'; void main() =& ...
- Selenium 2自动化测试实战2(数组与字典)
一.数组与字典 1.数组 数组用方括号([])表示,里面的每一项用逗号(,)隔开 Prthon允许在数组里面任意地放置数字或字符串.需要注意的是,数组下标是从0开始的,所以,lists[0]会输出数组 ...
- Python学习之==>字符串格式化
1.第一种方式 import datetime today = datetime.date.today() username = input('请输入用户名:') welcome = '欢迎光临:' ...