numpy.where
np.where(condition[, x, y])
如果是一维,相当于[xv if c else yv for (c,xv,yv) in zip(condition,x,y)]
输入条件,类数组形式,若判断结果成立则返回x,否则为y。
返回为tuple或者array。
当条件对象为一维,返回array。
条件对象为二维,返回tuple。第一部分为矩阵行的坐标,第二部分为矩阵列的坐标。
当条件对象维高维,按照二维矩阵操作,判断其中对象。
np.eye(n)生成对象数组,在np.where中按照一维操作及返回。
以下为scipy doc原文。
numpy.where
- numpy.where(condition[, x, y])
-
Return elements, either from x or y, depending on condition.
If only condition is given, return condition.nonzero().
Parameters: condition : array_like, bool
When True, yield x, otherwise yield y.
x, y : array_like, optional
Values from which to choose. x and y need to have the same shape as condition.
Returns: out : ndarray or tuple of ndarrays
If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere.
If only condition is given, return the tuple condition.nonzero(), the indices where condition is True.
Notes
If x and y are given and input arrays are 1-D, where is equivalent to:
[xv if c else yv for (c,xv,yv) in zip(condition,x,y)]
np.where()
Examples >>>
>>> np.where([[True, False], [True, True]],
... [[1, 2], [3, 4]],
... [[9, 8], [7, 6]])
array([[1, 8],
[3, 4]])
>>>
>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))
>>>
>>> x = np.arange(9.).reshape(3, 3)
>>> np.where( x > 5 )
(array([2, 2, 2]), array([0, 1, 2]))
>>> x[np.where( x > 3.0 )] # Note: result is 1D.
array([ 4., 5., 6., 7., 8.])
>>> np.where(x < 5, x, -1) # Note: broadcasting.
array([[ 0., 1., 2.],
[ 3., 4., -1.],
[-1., -1., -1.]])
Find the indices of elements of x that are in goodvalues. >>>
>>> goodvalues = [3, 4, 7]
>>> ix = np.in1d(x.ravel(), goodvalues).reshape(x.shape)
>>> ix
array([[False, False, False],
[ True, True, False],
[False, True, False]], dtype=bool)
>>> np.where(ix)
(array([1, 1, 2]), array([0, 1, 1]))
scipy doc : np.where()
numpy.where的更多相关文章
- 利用Python进行数据分析(5) NumPy基础: ndarray索引和切片
概念理解 索引即通过一个无符号整数值获取数组里的值. 切片即对数组里某个片段的描述. 一维数组 一维数组的索引 一维数组的索引和Python列表的功能类似: 一维数组的切片 一维数组的切片语法格式为a ...
- 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍
一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 ...
- 利用Python进行数据分析(6) NumPy基础: 矢量计算
矢量化指的是用数组表达式代替循环来操作数组里的每个元素. NumPy提供的通用函数(既ufunc函数)是一种对ndarray中的数据进行元素级别运算的函数. 例如,square函数计算各元素的平方,r ...
- python安装numpy、scipy和matplotlib等whl包的方法
最近装了python和PyCharm开发环境,但是在安装numpy和matplotlib等包时出现了问题,现总结一下在windows平台下的安装方法. 由于现在找不到了工具包新版本的exe文件,所以采 ...
- 深入理解numpy
一.为啥需要numpy python虽然说注重优雅简洁,但它终究是需要考虑效率的.别说运行速度不是瓶颈,在科学计算中运行速度就是瓶颈. python的列表,跟java一样,其实只是一维列表.一维列表相 ...
- Python Numpy,Pandas基础笔记
Numpy Numpy是python的一个库.支持维度数组与矩阵计算并提供大量的数学函数库. arr = np.array([[1.2,1.3,1.4],[1.5,1.6,1.7]])#创建ndarr ...
- broadcasting Theano vs. Numpy
broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...
- python之numpy
一.矩阵的拼接合并 列拼接:np.column_stack() >>> import numpy as np >>> a = np.arange(9).reshap ...
- win7系统下python安装numpy,matplotlib,scipy和scikit-learn
1.安装numpy,matplotlib,scipy和scikit-learn win7系统下直接采用pip或者下载源文件进行安装numpy,matplotlib,scipy时会遇到各种问题,这是因为 ...
- 给numpy矩阵添加一列
问题的定义: 首先我们有一个数据是一个mn的numpy矩阵现在我们希望能够进行给他加上一列变成一个m(n+1)的矩阵 import numpy as np a = np.array([[1,2,3], ...
随机推荐
- 如何将revit模型背景设置为黑色
Revit软件建模窗口默认的背景色为白色,在用惯了CAD的新用户转到Revit软件的时候,会对Revit白色的背景不太适应,跟AutoCAD一样,Revit提供自定义工作区背景颜色的功能--其实,你只 ...
- 如何诊断windows性能问题
直接使用perfmon中的性能诊断工具
- Spark操作:Aggregate和AggregateByKey
1. Aggregate Aggregate即聚合操作.直接上代码: import org.apache.spark.{SparkConf, SparkContext} object Aggregat ...
- 数据库连接池优化配置(druid,dbcp,c3p0)
主要描述了数据库连接池参数配置的准则,针对常用的数据库连接池(c3p0,dbcp,druid)给出推荐的配置. 考虑因素 1:当前连接DB的规模 2:并发情况 3:执行db的响应时间 配置考虑 1 ...
- Xilinx 常用模块汇总(verilog)【02】
作者:桂. 时间:2018-05-08 18:35:56 链接:http://www.cnblogs.com/xingshansi/p/9010282.html [本文遗留几处细节问题,待闲下来解决 ...
- [android开发教程] 一个神奇的Demo 帮你掌握所有android控件
(本文内容来源:http://www.eoeandroid.com/thread-182392-1-1.html 转载请注明出处!) 2.jpg (23.78 KB, 下载次数: 0) 下载附件 ...
- 浅析Mysql的my.ini文件
转载:http://hunanpengdake.iteye.com/admin/blogs/1647725 今天闲的蛋疼,没事想了解mysql,大家都知道在配置Mysql的过程中,my.ini非常重要 ...
- Atitit 提升开发进度大方法--高频功能与步骤的优化 类似性能优化
Atitit 提升开发进度大方法--高频功能与步骤的优化 类似性能优化 1. 通用功能又可以组合成crud模块1 1.1. 查询(包括步骤,发送查询dsl,通讯返回结果,绑定到表格控件)2 1.2. ...
- Atitit.pagging 翻页功能解决方案专题 与 目录大纲 v3 r44.docx
Atitit.pagging 翻页功能解决方案专题 与 目录大纲 v3 r44.docx 1.1. 翻页的重要意义1 1.2. Dep废弃文档 paip.js翻页分页pageing组件.txt1 ...
- linux每日命令(8):mv命令
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 一.命令格式: mv [选项] 源文件或目 ...