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[, xy])

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.

See also

nonzerochoose

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的更多相关文章

  1. 利用Python进行数据分析(5) NumPy基础: ndarray索引和切片

    概念理解 索引即通过一个无符号整数值获取数组里的值. 切片即对数组里某个片段的描述. 一维数组 一维数组的索引 一维数组的索引和Python列表的功能类似: 一维数组的切片 一维数组的切片语法格式为a ...

  2. 利用Python进行数据分析(4) NumPy基础: ndarray简单介绍

    一.NumPy 是什么 NumPy 是 Python 科学计算的基础包,它专为进行严格的数字处理而产生.在之前的随笔里已有更加详细的介绍,这里不再赘述. 利用 Python 进行数据分析(一)简单介绍 ...

  3. 利用Python进行数据分析(6) NumPy基础: 矢量计算

    矢量化指的是用数组表达式代替循环来操作数组里的每个元素. NumPy提供的通用函数(既ufunc函数)是一种对ndarray中的数据进行元素级别运算的函数. 例如,square函数计算各元素的平方,r ...

  4. python安装numpy、scipy和matplotlib等whl包的方法

    最近装了python和PyCharm开发环境,但是在安装numpy和matplotlib等包时出现了问题,现总结一下在windows平台下的安装方法. 由于现在找不到了工具包新版本的exe文件,所以采 ...

  5. 深入理解numpy

    一.为啥需要numpy python虽然说注重优雅简洁,但它终究是需要考虑效率的.别说运行速度不是瓶颈,在科学计算中运行速度就是瓶颈. python的列表,跟java一样,其实只是一维列表.一维列表相 ...

  6. Python Numpy,Pandas基础笔记

    Numpy Numpy是python的一个库.支持维度数组与矩阵计算并提供大量的数学函数库. arr = np.array([[1.2,1.3,1.4],[1.5,1.6,1.7]])#创建ndarr ...

  7. broadcasting Theano vs. Numpy

    broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...

  8. python之numpy

    一.矩阵的拼接合并 列拼接:np.column_stack() >>> import numpy as np >>> a = np.arange(9).reshap ...

  9. win7系统下python安装numpy,matplotlib,scipy和scikit-learn

    1.安装numpy,matplotlib,scipy和scikit-learn win7系统下直接采用pip或者下载源文件进行安装numpy,matplotlib,scipy时会遇到各种问题,这是因为 ...

  10. 给numpy矩阵添加一列

    问题的定义: 首先我们有一个数据是一个mn的numpy矩阵现在我们希望能够进行给他加上一列变成一个m(n+1)的矩阵 import numpy as np a = np.array([[1,2,3], ...

随机推荐

  1. loopback文件系统

    回环设备(loop-back devices) 实验环境 centos7.2 回环设备( 'loopback device')允许用户以一个普通磁盘文件虚拟一个块设备.(磁盘文件 --> 块设备 ...

  2. Shell脚本高级编程笔记一

     http://www.cnblogs.com/ygj0930/p/8184277.html 一:函数 1:创建函数 法一: function name{ commands } 法二: name() ...

  3. armeabi和armeabi-v7a(转)

    在ANE中如果SDK调用了so库,则需要把so库放到ANE下Android-ARM/lib/armeabi (调试模式)或者 armeabi-v7a(发行模式)下. 可以贴个ADT代码说明问题: // ...

  4. 整死你个妖精,CDN西游捉妖记!

    CDN的降价潮和撕逼季已过,终于轮到小黑羊来做个科普啦. 这事儿,要从西游记取经开始…… [本图来自肖传湛个人网站:www.moko.cc/hiyoko] 1300年前,唐僧师徒取经要跋涉十万八千里, ...

  5. react-无状态组件

    import React, { Component } from "react"; //import PostItem from "./PostItem"; / ...

  6. [Big Data - Suro] Netflix开源数据流管理器Suro

    Netflix近日开源了一个叫做Suro的工具,公司可以利用它来做数据源主机到目标主机的实时定向.它不只在Netflix的数据管道上扮演重要角色,大规模下的应用场景同样令人印象深刻. Netflix各 ...

  7. C#中的Action<>和Func<>

    其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: public delegate void myDeleg ...

  8. python调用nmap探测局域网设备

    平台:linux 描述:利用os.popen()函数调用系统命令nmap进行扫描,并用grep命令对扫描结果关键内容进行提取 代码 #!/usr/bin/env pthon #--*--coding= ...

  9. C++ 智能指针三

    /* 智能指针shared_ptr注意点 */ #include <iostream> #include <string> #include <memory> // ...

  10. 在windows下执行./configure,make,makeinstall源码安装程序spice-gtk

    使用MSYS软件,在我的上一篇博客中有软件下载地址.本文使用MSYS进行源码编译spice-gtk-0.33. 首先打开MSYS软件,进入你源码所在目录,例如:cd  /c/Users/Admi... ...