NumPy Python中的基本切片和高级索引

2021年3月30日11:25:25 发表评论 633 次浏览

先决条件:Numpy in Python简介

NumPy或Numeric Python是用于在均匀n维数组上进行计算的软件包。在numpy中, 尺寸称为轴。

为什么我们需要NumPy?

出现一个问题, 为什么在已经存在python列表的情况下我们为什么需要NumPy。答案是我们不能直接对两个列表的所有元素执行操作。例如, 我们不能直接将两个列表相乘, 而必须逐个元素地进行。这就是NumPy发挥作用的地方。

# Python program to demonstrate a need of NumPy
  
list1 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
list2 = [ 10 , 9 , 8 , 7 , 6 , 5 ]
  
# Multiplying both lists directly would give an error.
print (list1 * list2)

输出:

TypeError: can't multiply sequence by non-int of type 'list'

使用NumPy数组可以轻松完成此操作。

另一个例子,

# Python program to demonstrate the use of NumPy arrays
import numpy as np
  
list1 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
list2 = [ 10 , 9 , 8 , 7 , 6 , 5 ]
  
# Convert list1 into a NumPy array
a1 = np.array(list1)
  
# Convert list2 into a NumPy array
a2 = np.array(list2)
  
print (a1 * a2)

输出:

array([10, 18, 24, 28, 30, 30])

本文将帮助你熟悉NumPy中的索引。 python的Numpy软件包具有以不同方式进行索引的强大功能。

使用索引数组建立索引

通过使用数组作为索引, 可以在numpy中完成索引。如果是切片, 则返回数组的视图或浅表副本, 但在索引数组中, 返回原始数组的副本。 Numpy数组可以与其他数组或任何其他序列(元组除外)建立索引。最后一个元素的索引为-1秒, 最后一个索引为-2, 依此类推。

# Python program to demonstrate 
# the use of index arrays.
import numpy as np
  
# Create a sequence of integers from 10 to 1 with a step of -2
a = np.arange( 10 , 1 , - 2 ) 
print ( "\n A sequential array with a negative step: \n" , a)
  
# Indexes are specified inside the np.array method.
newarr = a[np.array([ 3 , 1 , 2 ])]
print ( "\n Elements at these indices are:\n" , newarr)

输出:

A sequential array with a negative step:
[10  8  6  4  2]

Elements at these indices are:
[4 8 6]

另一个例子,

import numpy as np
  
# NumPy array with elements from 1 to 9
x = np.array([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ])
  
# Index values can be negative.
arr = x[np.array([ 1 , 3 , - 3 ])]
print ( "\n Elements are : \n" , arr)

输出:

Elements are:
[2 4 7]

索引类型

索引有两种类型:

基本切片和索引编制:

考虑语法x [obj], 其中x是数组, 而obj是索引。切片对象是基本切片时的索引。基本切片发生在obj为时:

  1. 一个切片对象, 其形式为start:stop:step
  2. 一个整数
  3. 或切片对象和整数的元组

通过基本切片生成的所有阵列始终都是原始阵列的视图。

# Python program for basic slicing.
import numpy as np
  
# Arrange elements from 0 to 19
a = np.arange( 20 )
print ( "\n Array is:\n " , a)
  
# a[start:stop:step]
print ( "\n a[-8:17:1]  = " , a[ - 8 : 17 : 1 ]) 
  
# The : operator means all elements till the end.
print ( "\n a[10:]  = " , a[ 10 :])

输出:

Array is:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

a[-8:17:1]  =  [12 13 14 15 16]

a[10:] = [10 11 12 13 14 15 16 17 18 19]

省略号也可以与基本切片一起使用。省略号(…)是使选择元组的长度与数组维数相同的:对象的数量。

# Python program for indexing using basic slicing with ellipsis
import numpy as np
  
# A 3 dimensional array.
b = np.array([[[ 1 , 2 , 3 ], [ 4 , 5 , 6 ]], [[ 7 , 8 , 9 ], [ 10 , 11 , 12 ]]])
  
print (b[..., 1 ]) #Equivalent to b[: , : , 1 ]

输出:

[[ 2  5]
 [ 8 11]]

高级索引:

当obj为时, 将触发高级索引编制:

  1. 整数或布尔类型的ndarray
  2. 或具有至少一个序列对象的元组
  3. 是一个非元组序列对象

高级索引返回数据的副本, 而不是其视图。高级索引有整数和布尔两种类型。

纯整数索引:当整数用于索引时。第一维的每个元素与第二维的元素配对。因此, 在这种情况下元素的索引为(0, 0), (1, 0), (2, 1), 并选择了相应的元素。

# Python program showing advanced indexing
import numpy as np
  
a = np.array([[ 1 , 2 ], [ 3 , 4 ], [ 5 , 6 ]])
print (a[[ 0 , 1 , 2 ], [ 0 , 0 , 1 ]])

输出:

[1 3 6]

布尔索引

该索引具有一些布尔表达式作为索引。返回满足该布尔表达式的那些元素。它用于过滤所需的元素值。

# You may wish to select numbers greater than 50
import numpy as np
  
a = np.array([ 10 , 40 , 80 , 50 , 100 ])
print (a[a> 50 ])

输出:

[80 100]
# You may wish to square the multiples of 40 
import numpy as np
  
a = np.array([ 10 , 40 , 80 , 50 , 100 ])
print (a[a % 40 = = 0 ] * * 2 )

输出:

[1600 6400])
# You may wish to select those elements whose
# sum of row is a multiple of 10.
import numpy as np
  
b = np.array([[ 5 , 5 ], [ 4 , 5 ], [ 16 , 4 ]])
sumrow = b. sum ( - 1 )
print (b[sumrow % 10 = = 0 ])

输出:

array([[ 5, 5], [16, 4]])

参考:SciPy.org

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: