前导知识
- 斯坦福计算机视觉提供tutorial:Python Numpy Tutorial (with Jupyter and Colab)
- Python中numpy包安装:
(53条消息) Numpy的简明安装_yyuand的博客-CSDN博客_numpy安装
3. 使用numpy:
import numpy as np
Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays.
- numpy是Python中科学计算的核心库,在多维数组的操作和计算中有十分出色的表现
Arrays
A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.
- numpy数组是具有相同类型的值的网格,并由非负整数元组进行索引。维数为数组的秩;数组的形状是一个整数元组,表示数组沿每个维度的大小。
shape可以求出数组的行数和列数,即数组的维数,数组里的各元素通过下标加方括号访问
import numpy as np
#一维数组
a = np.array([1,2,3])
print(type(a),a.shape,a[0],a[1],a[2])
a[0]=5;
print(a)
#二维数组
b=np.array([[1,2,3],[4,5,6]])
print(b)
print(b.shape)
print(b[0,0],b[0,1],b[0,2])
<class 'numpy.ndarray'> (3,) 1 2 3
[5 2 3]
[[1 2 3]
[4 5 6]]
(2, 3)
1 2 3
numpy中提供了很多函数来创建新数组
identity matrix 单位矩阵:左对角线都是1,右对角线都是0,使用np.eye(n)创建
import numpy as np
a=np.zeros((2,2)) #创建一个两行两列的数组,元素均为0
print(a)
b=np.ones((1,2)) #创建一个一行两列的数组,元素均为1
print(b)
c=np.full((2,2),7) #创建一个2行2列的值都为7的数组
print(c)
d=np.eye(2) #创建一个2*2的单位矩阵(identity matrix)
print(d)
e=np.random.random((2,2)) #创建一个2*2数组,被随机数填充
print(e)
[[0. 0.]
[0. 0.]]
[[1. 1.]]
[[7 7]
[7 7]]
[[1. 0.]
[0. 1.]]
[[0.8890121 0.55838264]
[0.51245496 0.56848541]]
Array indexing
切片
- numpy提供了多种方法去引用数组中的元素,数组也可以被切片,由于是多维数组,因此切片时必须指明数组的维度
import numpy as np
a=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(a)
#使用切片,行数取前两行,列数取1,2列(从0开始)
b=a[:2,1:3]
print(b)
a:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]b:
[[2 3]
[6 7]]
- 对数组的切片进行操作就是对源数组进行操作:
修改了b的值,a的值也相应改变!
import numpy as np
a=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(a)
#使用切片,行数取前两行,列数取1,2列(从0开始)
b=a[:2,1:3]
print(b)
print(a[0,1])
b[0,0]=77
print(a[0,1])
切片行、列
访问数组中间行数据的两种方法:
- 使用slices混合整数索引将得到一个低秩数组。
- 而只使用slices将得到一个与原始数组相同秩的数组。
访问行元素:
import numpy as np
a=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(a)
print()
row1=a[1,:]
row2=a[1:2,:]
row3=a[[1],:]
print(row1,row1.shape)
print(row2,row2.shape)
print(row3,row3.shape)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]][5 6 7 8] (4,)
[[5 6 7 8]] (1, 4)
[[5 6 7 8]] (1, 4)
访问列元素:
import numpy as np
a=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
print(a)
print()
col1=a[:,1]
col2=a[:,1:2]
print(col1,col1.shape)
print()
print(col2,col2.shape)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]][ 2 6 10] (3,)
[[ 2]
[ 6]
[10]] (3, 1)
整数索引
Integer array indexing: When you index into numpy arrays using slicing, the resulting array view will always be a subarray of the original array. In contrast, integer array indexing allows you to construct arbitrary arrays using the data from another array.
- 整数数组索引:当使用切片对numpy数组进行索引时,得到的数组视图将始终是原始数组的一个子数组。相反,整数数组索引允许使用来自另一个数组的数据构造任意数组。
import numpy as np
a=np.array([[1,2],[3,4],[5,6]])
print(a[[0,1,2],[0,1,0]])
a1=np.array([a[0,0],a[1,1],a[2,0]])
print(a1)
print(a1.shape)
print()
print(a[[0,0],[1,1]])
print(np.array([a[0,1],a[0,1]]))
[1 4 5]
[1 4 5]
(3,)[2 2]
[2 2]