numpy的基本API(三)——索引

numpy的基本索引API

  iwehdio的博客园:https://www.cnblogs.com/iwehdio/

1、单个元素的索引

  对于一维数组,索引方式与内置的List相同。正索引从0开始,负索引从-1开始。

>>> x = np.arange(10)>>> xarray([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x[2]
2
>>> x[-2]
8

  

  在多维数组中也是同理。x[0,2] 与 x[0][2]是等价的,但是后者的效率更低。

>>> x = np.arange(10).reshape(2,5)>>> xarray([[0, 1, 2, 3, 4],       [5, 6, 7, 8, 9]])
>>> x[1,3]
8
>>> x[1,-1]
9

  

  如果想要索引时对x的个轴不作限制。可以用冒号“:”或省略号“...”表示选取该行或列的所有数据。在这里x[0]等价于x[0, :]。

>>> x[0]
array([0, 1, 2, 3, 4])
>>> x[:, 1]
array([1, 6])

  

  切片方式与List类似。a:b:c表示以a为起点,b为终点,步长为c。对于多维数组同样类似,:表示选取该个维度的所有数据。由于-1表示负步长,可以使用x[::-1]来反转数组。

>>> x[:,1:3]
array([[1, 2],
       [6, 7]])
>>> x[:,::-1]
array([[4, 3, 2, 1, 0],
       [9, 8, 7, 6, 5]])

  

  使用np.newaxis,可以在数组中插入某个长度为1的轴。

>>> x.shape
(3, 2)
>>> x[:,np.newaxis,:].shape
(3, 1, 2)

2、数组索引

  可以通过整数数组定义的索引值,来自定义索引。即索引了数组x中的元素x[0,0]、x[1,1]、x[2,0],并返回一个数组。

>>> x = np.array([[1, 2], [3, 4], [5, 6]])
>>> x
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> x[[0, 1, 2], [0, 1, 0]]
array([1, 4, 5])

  

  也可以用广播来创建索引数据。比如用newaxis或者ix_。最终都索引到了x[0,0]、x[0,2]、x[3,0]、x[3,2]。

>>> x = np.array([[ 0,  1,  2],
                  [ 3,  4,  5],
                  [ 6,  7,  8],
                  [ 9, 10, 11]])
>>> rows = np.array([[0, 0],
                     [3, 3]])
>>> columns = np.array([[0, 2],
                        [0, 2]])
>>> x[rows, columns]
array([[ 0,  2],
       [ 9, 11]])    

>>> rows = np.array([0, 3])
>>> columns = np.array([0, 2])
>>> rows[:, np.newaxis]
array([[0],
          [3]])
>>> x[rows[:, np.newaxis], columns]
array([[ 0,  2],
       [ 9, 11]])       

>>> x[np.ix_(rows, columns)]
array([[ 0,  2],
       [ 9, 11]])             

3、布尔数组索引

  使用布尔数组索引时,[ ] 内的为True或Flase或结果为True或Flase的表达式。下边第一个例子表示实际上是 [ ] 内的布尔值为True时进行索引。第二个例子表示对 x<0 为True的元素进行索引,并对其加20。第三个例子是,索引元素行的和小于等于2的行。

>>> x = np.array([1., -1., -2., 3])
>>> x[[True,False,False,True]]
array([1., 3.])
>>> x[x < 0] += 20
>>> x
array([ 1., 19., 18.,  3.])
>>> x = np.array([[0, 1], [1, 1], [2, 2]])
>>> rowsum = x.sum(-1)
>>> x[rowsum <= 2, :]
array([[0, 1],
       [1, 1]])

4、元组索引

  用元组可以整体设置数组的索引,处理可变数量的索引。相比而言用数组索引会出现错误。以索引下面的z的z[1,1,1,1]为例。用元组只需写z[(1,1,1,1)]。用数组写z[[1,1,1,1]],则是索引到了4个z[1]数组组成的shape为(4,1)的数组。

>>> z = np.arange(81).reshape(3,3,3,3)
>>> indices = (1,1,1,1)
>>> z[indices]
40

>>> indices = (1,1,1,slice(0,2))
>>> z[indices]
array([39, 40])

  同时,也可以用元组进行切片,使用slice(a,b)函数,等价于a:b(但不能在元组中使用冒号)。

5、索引相关函数

  

  np.r_[ ‘form‘, a, b]  np.r_方法可以把输入的多个数组索引进行拼接。‘form‘是按逗号分隔的整数字符串,当只有一个整数时,表示按第几个轴进行拼接。

>>> np.r_[-1:1:6j, [0]*3, 5, 6]
array([-1. , -0.6, -0.2,  0.2,  0.6,  1. ,  0. ,  0. ,  0. ,  5. ,  6. ])

>>> np.r_[‘-1‘, a, a]
array([[0, 1, 2, 0, 1, 2],
       [3, 4, 5, 3, 4, 5]])

>>> np.r_[‘0‘, a, a]
array([[0, 1, 2],
       [3, 4, 5],
       [0, 1, 2],
       [3, 4, 5]])

 

  ‘form‘整数字符串中有效的是其前三位整数,如 ‘q, w, e ‘。以下用数组 x = np.arange(24).reshape(2,3,4)为例。

  第一个整数q表示所要拼接的是哪一个轴。若对两个相同的输入数组进行拼接,输入数组shape为(2,3,4),则q取0,1,2所拼接的数组shape分别(4,3,4)、(2,6,4)、(2,3,8)。

>>> np.r_[‘0‘, x, x].shape
(4, 3, 4)
>>> np.r_[‘1‘, x, x].shape
(2, 6, 4)
>>> np.r_[‘2‘, x, x].shape
(2, 3, 8)

  第二个整数w表示输出的最小维数,若大于输入数组的维数则补充。如果w比输入维数大,如对前例有w=4,则输出数组ans的维数为四维。默认状态下新添加的轴的位置为-1,即在输入shape之后。如对q=0,w=4有输出shape为(2,2,3,4)。如果w比输入维度大n(>1),如对前例有w=5,则输出输出数组ans的shape为(2,1,2,3,4)。

>>> np.r_[‘0,4‘, x, x].shape
(2, 2, 3, 4)
>>> np.r_[‘0,5‘, x, x].shape
(2, 1, 2, 3, 4)

  第三个整数e表示新增的轴在shape中的排列方式,默认值为‘-1’表示新增轴都在原数组的shape之前,之前对第二个整数w举例时都在此种情况。e的取值范围最大为w-输入数组维度,‘0’表示新增的shape为1的轴全在输入数组的shape之后,‘1’表示新增的shape为1的轴有一个轴在输入数组的shape之前,‘2’表示新增的shape为1的轴有两个轴在输入数组的shape之前,以此类推。

>>> np.r_[‘0,5,0‘, x, x].shape
(4, 3, 4, 1, 1)
>>> np.r_[‘0,5,1‘, x, x].shape
(2, 2, 3, 4, 1)
>>> np.r_[‘0,5,2‘, x, x].shape
(2, 1, 2, 3, 4)

  需要说明的是,虽然输入的‘form’整数字符串是按 q、w、e 的顺序输入的,但是numpy对参数的处理顺序却是 w、e、q。也就是说,首先按输入的w计算需要补几个shape为1的轴,然后按e对shape进行顺序设置,最后用q去索引设置更新好的shape的数组。这就是q=0,w=4,e=-1时输出shape为(2,2,3,4)的原因。

  如果是不同shape的数组连接,一定要注意好连接的轴的索引。

>>> y = np.arange(12).reshape(1,3,4)
>>> np.r_[‘1,4‘, x, y].shape
(1, 3, 3, 4)
>>> np.r_[‘2,5,2‘, x, y].shape
(1, 1, 3, 3, 4)

  np.c_[a, b]  np.c_方法可以把输入的索引数组返回二维数组的输出,实际上等价于np.r_[‘-1,2,0‘, a, b]。

>>> np.c_[np.array([1,2,3]), np.array([4,5,6])]
array([[1, 4],
       [2, 5],
       [3, 6]])
>>> np.r_[‘-1,2,0‘,np.array([1,2,3]), np.array([4,5,6])]
array([[1, 4],
       [2, 5],
       [3, 6]])

  np.s_[ a:b:c ]  np.s_方法可以创建索引元组,a:b:c表示从a到b步长为c的数组索引,与直接索引数组的方法类似。

>>> np.s_[2::1]
slice(2, None, 1)
>>> np.array([0, 1, 2, 3, 4])[np.s_[2::1]]
array([2, 3, 4])
>>> np.s_[:,2::1]
(slice(None, None, None), slice(2, None, 1))>>> x = np.arange(24).reshape(2, 3, 4)
>>> x[np.s_[:,2::1]]
array([[[ 8,  9, 10, 11]],
       [[20, 21, 22, 23]]])

  np.nonzero(a)  np.nonzero方法返回数组中非零元素的索引。也可以用于查找数组中满足某条件的元素的索引。

>>> x = np.array([[3, 0, 0], [0, 4, 0], [5, 6, 0]])
>>> x
array([[3, 0, 0],
       [0, 4, 0],
       [5, 6, 0]])
>>> np.nonzero(x)
(array([0, 1, 2, 2]), array([0, 1, 0, 1]))
>>> x[np.nonzero(x)]
array([3, 4, 5, 6])

>>> a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> a > 3
array([[False, False, False],
       [ True,  True,  True],
       [ True,  True,  True]])
>>> np.nonzero(a > 3)
(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
>>> a[np.nonzero(a > 3)]
array([4, 5, 6, 7, 8, 9])

  np.where( condition, x, y )  np.where方法根据condition的条件,为真时返回x中对应的值,为假时返回y中对应的值。

>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(a < 5, a, 10*a)
array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])

>>> x, y = np.ogrid[:3, :4]
>>> np.where(x < y, x, 10 + y)
array([[10,  0,  0,  0],
       [10, 11,  1,  1],
       [10, 11, 12,  2]])

  np.ix_(a, b, c)  np.ix_方法根据输入的一维数组a、b、c构造索引网格。

>>> x = np.arange(24).reshape(2, 3, 4)
>>> index = np.ix_([0, 1], [1, 2],[2, 3])
>>> index
(array([[[0]],
        [[1]]]),
 array([[[1],
         [2]]]),
 array([[[2, 3]]]))
>>> x[index]
array([[[ 6,  7],
        [10, 11]],
       [[18, 19],
        [22, 23]]])

  np.take(a, index, axis=None)  np.take方法按轴索引元素。当axis=None时,将数组看作是展平的。当axis指定时,输出对该轴索引的结果。index输入的只是对指定轴的一维索引,如果输入为二维数组,会导致输出为二维数组中一维数组索引结果的数组拼接。

>>> x = np.arange(24).reshape(2, 3, 4)>>> np.take(x,[0,5,10,15,20])
array([ 0,  5, 10, 15, 20])

>>> np.take(x, [0,1], axis=1)
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],
       [[12, 13, 14, 15],
        [16, 17, 18, 19]]])

>>> np.take(a, [[0, 1], [2, 3]])
array([[4, 3],
       [5, 7]])

  np.compress(index, a, axis=None)  np.compress方法返回按给定轴的数组的选定切片。index表示在该轴上的布尔数组切片索引,a表示输入数组,axis表示选定的轴。

>>> x = np.arange(24).reshape(2,3,4)
>>> np.compress([False, False, True, True],x,axis=2)
array([[[ 2,  3],
        [ 6,  7],
        [10, 11]],
       [[14, 15],
        [18, 19],
        [22, 23]]])

6、根据索引条件改变数组值的函数

  np.select(condlist, choicelist, default=0)  np.select方法根据条件返回符合列表条件的元素数组。condlist表示输入的条件列表,choicelist表示输入的选择列表,与condlist是一一对应的,当x中元素符合condlist中的第一个条件时,输出数组中就选择choicelist中第一个选择作为输出,如符合多个条件则选择第一个。default表示不符合condlist中所有条件时填充什么元素,默认为0,选x时填充原数组的元素。

>>> x = np.arange(10)
>>> condlist = [x<3, x>5]
>>> choicelist = [x, x**2]
>>> np.select(condlist, choicelist)
>>> np.select(condlist, choicelist,default=x)
array([ 0,  1,  2,  3,  4,  5, 36, 49, 64, 81])

>>> np.select(condlist, choicelist)
array([ 0,  1,  2,  0,  0,  0, 36, 49, 64, 81])

  np.place(a, mask, b)  np.place方法根据布尔数组mask的索引,将索引到的输入数组a的元素的值更改为b,如果b的长度不够则重复b。

>>> arr = np.arange(6).reshape(2, 3)
>>> np.place(arr, arr>2, [44, 55])
>>> arr
array([[ 0,  1,  2],
       [44, 55, 44]])

  np.put(a, index, b)  np.put方法将用给定值b替换输入数组a中索引为index的元素的值。index是按展平后的数组索引的,替换的值按index中的顺序排列b,如果b的长度不够则重复b。

>>> arr = np.arange(6).reshape(2, 3)
>>> np.put(arr,[0,1,5,4],[44,55])
>>> arr
array([[44, 55,  2],
       [ 3, 55, 44]])

参考:numpy中文文档:https://www.numpy.org.cn/reference/

   numpy英文文档:https://numpy.org/doc/1.17/reference/index.html

iwehdio的博客园:https://www.cnblogs.com/iwehdio/

原文地址:https://www.cnblogs.com/iwehdio/p/12003285.html

时间: 2024-08-08 06:08:18

numpy的基本API(三)——索引的相关文章

numpy的基本API(四)——拼接、拆分、添加、删除

numpy的基本拼接.拆分.添加.删除API iwehdio的博客园:https://www.cnblogs.com/iwehdio/ 1.np.concatenate((a, b), axis=0) np.concatenate方法沿现有的轴拼接一系列数组.输入数组(a.b.c)等要以元组形式输入,要求除了在所指定的拼接轴外形状一致.axis表示所指示的拼接轴,默认为0,为None时表示拼接前将所有输入数组展平. >>> a = np.arange(8).reshape(4, 2) &

Atitit.index&#160;manager&#160;api&#160;design&#160;索引管理api设计

Atitit.index manager api design 索引管理api设计 1. kw1 1.1. 索引类型 unique,normal,fulltxt1 1.2. 聚集索引(clustered index,也称聚类索引1 1.3. 索引方式:btree,hashtable2 1.4. 索引使用所有的页面规模百分比2 2. Ui2 3. api2 3.1. createIndex(indexName,cols)2 3.2. Rebuild2 3.3. Del2 3.4. Up2 4. -

[ElasticSearch]Java API 之 索引管理

ElasticSearch为了便于处理索引管理(Indices administration)请求,提供了 org.elasticsearch.client.IndicesAdminClient接口.通过如下代码从 Client 对象中获得这个接口的实现: IndicesAdminClient indicesAdminClient = client.admin().indices(); IndicesAdminClient定义了好几种prepareXXX()方法作为创建请求的入口点. 1. 索引

Express4.x API (三):Response (译)

Express4.x API (一):application (译) -- 进行中 Express4.x API (二):request (译) -- 完成 Express4.x API (三):Response (译) -- 完成 Express4.x API (四):router (译) -- 进行中 写在前面 技术库更迭较快,很难使译文和官方的API保持同步,更何况更多的大神看英文和中文一样的流畅,不会花时间去翻译--,所以我们看到express中文网更多的还是英文,我们只有提升自己的英语

Elasticsearch5.0 Java Api(三) -- 删除索引

测试删除索引的功能 1 package com.juyun.test; 2 3 import java.net.InetAddress; 4 import java.net.UnknownHostException; 5 6 import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; 7 import org.elasticsearch.action.admin.indices.create.CreateInd

Elasticsearch Bulk API批量索引

这篇博客介绍一下Elasticsearch对多个文档进行索引的简便方法.Bulk api的支持可以实现一次请求执行批量的添加.删除.更新等操作.Bulk操作使用的是UDP协议,UDP无法确保与ElasticSearch服务器通信时不丢失数据. 一.Bulk API 使用bulk命令时,REST API以_bulk结尾,批量操作写在json文件中,官网给出的语法格式: action_and_meta_data\n optional_source\n action_and_meta_data\n o

ElasticSearch Java Api -检索索引库

上篇博客记录了如何用java调用api把数据写入索引,这次记录下如何搜索. 一.准备数据 String data1 = JsonUtil.model2Json(new Blog(1, "git简介", "2016-06-19", "SVN与Git最主要的区别...")); String data2 = JsonUtil.model2Json(new Blog(2, "Java中泛型的介绍与简单使用", "2016-0

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

前几篇博文我写了数组创建和数据运算,现在我们就来看一下数组对象的操作方法.使用索引和切片的方法选择元素,还有如何数组的迭代方法. 一.索引机制 1.一维数组 In [1]: a = np.arange(10,16) In [2]: a Out[2]: array([10, 11, 12, 13, 14, 15]) #使用正数作为索引 In [3]: a[3] Out[3]: 13 #还可以使用负数作为索引 In [4]: a[-4] Out[4]: 12 #方括号中传入多数索引值,可同时选择多个

用JSON-server模拟REST API(三) 进阶使用

前面演示了如何安装并运行 json server, 和使用第三方库真实化模拟数据 , 下面将展开更多的配置项和数据操作. 配置项 在安装好json server之后,通过 json-server -h可以看到如下配置项: json-server [options] <source> Options: --config, -c 指定 config 文件 [默认: "json-server.json"] --port, -p 设置端口号 [default: 3000] --ho