前言
索引指的是用方括号 “[ ]” 加序号的形式引用数组中特定位置的元素,它的作用是从数组中取出一部分相应的元素重新组成一个子数组,而这个子数组就是通过索引得到的切片。
一、副本与视图
在Numpy中做数组运算时,返回的结果只有两种,一是“视图”,二是“副本”。
import numpy
as np
x
= np
.array
([1, 2, 3, 4, 5, 6, 7, 8])
y
= x
y
[0] = -1
print(x
)
print(y
)
x
= np
.array
([1, 2, 3, 4, 5, 6, 7, 8])
y
= x
.copy
()
y
[0] = -1
print(x
)
print(y
)
从上例可知,对副本数据进行修改,不会影响到原始数据,这是因为他们物理内存不在同一位置;而对视图数据进行修改,原始数据也会随之改变,这是因为视图数据与原始数据物理内存在同一位置。此处用到了一个函数:numpy.copy()其作用就是为原始数据创建一个副本。
二、索引与切片
1.整数索引
获取数组的单个元素,多维数组的子数组。
import numpy
as np
x
= np
.array
([1, 2, 3, 4, 5, 6, 7, 8])
print(x
[6])
x
= np
.array
([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[12, 34, 56, 78, 99],
[31, 32, 33, 34, 35]])
print(x
[1])
print(x
[3][2])
print(x
[3, 2])
2.切片索引
切片即从数组中抽取一部分元素组成新的数组。对Python 列表 进行切片操作,得到的数组是原数组的 副本,即修改切片后,原数组不变。对Numpy数据进行切片操作,得到的数组是指向相同缓冲区的 视图。一维数组
import numpy
as np
x
= np
.array
([1, 2, 3, 4, 5, 6, 7, 8])
print(x
[1:5:2])
print(x
[0:4])
print(x
[2:])
print(x
[:2])
print(x
[:])
print(x
[-2:])
print(x
[:-2])
print(x
[::-1])
二维数组,切片的第一片定义了行的切片,第二片定义了列的切片。
import numpy
as np
x
= np
.array
([[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]])
print(x
[2:4])
print(x
[1:5:2])
print(x
[2:])
print(x
[:2])
print(x
[-2:])
print(x
[:-2])
print(x
[:])
print(x
[1, :])
print(x
[:, 1])
print(x
[0, 1:4])
print(x
[1:4, 0])
print(x
[1:3, 2:4])
print(x
[:, :])
print(x
[::2, ::2])
x
[0::2, 1::3] = 0
print(x
)
print(x
[::-1, :])
print(x
[:, ::-1])
3.整数数组索引
方括号内传入多个索引值,可以同时选择多个元素。一维数组:
import numpy
as np
x
= np
.array
([1, 2, 3, 4, 5, 6, 7, 8])
r
= [0, 1, -1]
print(x
[r
])
x
= np
.array
([1, 2, 3, 4, 5, 6, 7, 8])
r
= np
.array
([[0, 1], [3, 4], [6, 7])
print(x
[r
])
二维数组
x
= np
.array
([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
r
= [0, 1, -1]
print(x
[r
])
x
= np
.array
([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
r
= np
.array
([[0, 1], [3, 4]])
print(x
[r
])
r
= [0, 1, 2]
c
= [2, 3, 4]
y
= x
[r
, c
]
print(y
)
r
= np
.array
([[0, 0], [4, 4]])
c
= np
.array
([[0, 4], [0, 4]])
y
= x
[r
, c
]
print(y
)
特殊函数numpy.take()
import numpy
as np
x
= np
.array
([1, 2, 3, 4, 5, 6, 7, 8])
r
= [0, 1, 2]
print(np
.take
(x
, r
))
x
= np
.array
([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
r
= [0, 1, 2]
print(np
.take
(x
, r
, axis
=0))
print(np
.take
(x
, r
, axis
=1))
4.布尔索引
import numpy
as np
x
= np
.array
([1, 2, 3, 4, 5, 6, 7, 8])
y
= x
> 5
print(y
)
print(x
[x
> 5])
x
= np
.array
([1, 9, np
.nan
, 3, 4, np
.nan
, 0])
y
= np
.logical_not
(np
.isnan
(x
))
print(x
[y
])
x
= np
.array
([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y
= x
> 25
print(y
)
print(x
[x
> 25])
三、补充知识
1.数组迭代
本块介绍函数np.apply_along_axis()
import numpy
as np
x
= np
.array
([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y
= np
.apply_along_axis
(np
.sum, 0, x
)
print(y
)
y
= np
.apply_along_axis
(np
.sum, 1, x
)
print(y
)
y
= np
.apply_along_axis
(np
.mean
, 0, x
)
print(y
)
y
= np
.apply_along_axis
(np
.mean
, 1, x
)
print(y
)
2.array换行、换列
import numpy
as np
A
= np
.arange
(9).reshape
(3, 3)
print(A
)
B
= A
[:, [2, 1, 0]]
print(B
)
C
= A
[[1, 0, 2], :]
print(C
)