在本课中,我们将学习使用NumPy包进行数值分析的基础知识。
import numpy as np
# 使得多次生成的随机数相同
np.random.seed(seed=1234)
# 标量(scalars)
x = np.array(6) # scalar
print ("x: ", x)
print("x ndim: ", x.ndim)
print("x shape:", x.shape)
print("x size: ", x.size)
print ("x dtype: ", x.dtype)
x: 6 x ndim: 0 x shape: () x size: 1 x dtype: int64
# 一维数组(array)
x = np.array([1.3 , 2.2 , 1.7])
print ("x: ", x)
print("x ndim: ", x.ndim)
print("x shape:", x.shape)
print("x size: ", x.size)
print ("x dtype: ", x.dtype) # notice the float datatype
x: [1.3 2.2 1.7] x ndim: 1 x shape: (3,) x size: 3 x dtype: float64
# 三维数组(矩阵(matrix))
x = np.array([[[1,2,3], [4,5,6], [7,8,9]]])
print ("x:\n", x)
print("x ndim: ", x.ndim)
print("x shape:", x.shape)
print("x size: ", x.size)
print ("x dtype: ", x.dtype)
x: [[[1 2 3] [4 5 6] [7 8 9]]] x ndim: 3 x shape: (1, 3, 3) x size: 9 x dtype: int64
# 函数
print ("np.zeros((2,2)):\n", np.zeros((2,2)))
print ("np.ones((2,2)):\n", np.ones((2,2)))
print ("np.eye((2)):\n", np.eye((2)))
print ("np.random.random((2,2)):\n", np.random.random((2,2)))
np.zeros((2,2)): [[0. 0.] [0. 0.]] np.ones((2,2)): [[1. 1.] [1. 1.]] np.eye((2)): [[1. 0.] [0. 1.]] np.random.random((2,2)): [[0.19151945 0.62210877] [0.43772774 0.78535858]]
# 索引(indexing)
x = np.array([1, 2, 3])
print ("x[0]: ", x[0])
x[0] = 0
print ("x: ", x)
x[0]: 1 x: [0 2 3]
# 切片(slicing)
x = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print (x)
print ("x column 1: ", x[:, 1])
print ("x row 0: ", x[0, :])
print ("x rows 0,1,2 & cols 1,2: \n", x[:3, 1:3])
[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] x column 1: [ 2 6 10] x row 0: [1 2 3 4] x rows 0,1,2 & cols 1,2: [[ 2 3] [ 6 7] [10 11]]
# 整数数组索引
print (x)
rows_to_get = np.arange(len(x))
print ("rows_to_get: ", rows_to_get)
cols_to_get = np.array([0, 2, 1])
print ("cols_to_get: ", cols_to_get)
print ("indexed values: ", x[rows_to_get, cols_to_get])
[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] rows_to_get: [0 1 2] cols_to_get: [0 2 1] indexed values: [ 1 7 10]
# 布尔数组索引
x = np.array([[1,2], [3, 4], [5, 6]])
print ("x:\n", x)
print ("x > 2:\n", x > 2)
print ("x[x > 2]:\n", x[x > 2])
x: [[1 2] [3 4] [5 6]] x > 2: [[False False] [ True True] [ True True]] x[x > 2]: [3 4 5 6]
# 数学基础
x = np.array([[1,2], [3,4]], dtype=np.float64)
y = np.array([[1,2], [3,4]], dtype=np.float64)
print ("x + y:\n", np.add(x, y)) # or x + y
print ("x - y:\n", np.subtract(x, y)) # or x - y
print ("x * y:\n", np.multiply(x, y)) # or x * y
x + y: [[2. 4.] [6. 8.]] x - y: [[0. 0.] [0. 0.]] x * y: [[ 1. 4.] [ 9. 16.]]
# 点积
a = np.array([[1,2,3], [4,5,6]], dtype=np.float64) # 我们可以指定dtype
b = np.array([[7,8], [9,10], [11, 12]], dtype=np.float64)
print (a.dot(b))
[[ 58. 64.] [139. 154.]]
# 跨维度求和
x = np.array([[1,2],[3,4]])
print (x)
print ("sum all: ", np.sum(x)) # 将所有元素相加
print ("sum by col: ", np.sum(x, axis=0)) # 逐列将元素相加
print ("sum by row: ", np.sum(x, axis=1)) # 逐行将元素相加
[[1 2] [3 4]] sum all: 10 sum by col: [4 6] sum by row: [3 7]
# 转置
print ("x:\n", x)
print ("x.T:\n", x.T)
x: [[1 2] [3 4]] x.T: [[1 3] [2 4]]
# np.tile:重复维度
x = np.array([[1,2], [3,4]])
y = np.array([5, 6])
addent = np.tile(y, (len(x), 1))
print ("addent: \n", addent)
z = x + addent
print ("z:\n", z)
addent: [[5 6] [5 6]] z: [[ 6 8] [ 8 10]]
# 广播(broadcasting)
x = np.array([[1,2], [3,4]])
y = np.array([5, 6])
z = x + y
print ("z:\n", z)
z: [[ 6 8] [ 8 10]]
# 改变维度
x = np.array([[1,2], [3,4], [5,6]])
print (x)
print ("x.shape: ", x.shape)
y = np.reshape(x, (2, 3))
print ("y.shape: ", y.shape)
print ("y: \n", y)
[[1 2] [3 4] [5 6]] x.shape: (3, 2) y.shape: (2, 3) y: [[1 2 3] [4 5 6]]
# 删除维度
x = np.array([[[1,2,1]],[[2,2,3]]])
print ("x.shape: ", x.shape)
y = np.squeeze(x, 1) # 删除维度1
print ("y.shape: ", y.shape)
print ("y: \n", y)
x.shape: (2, 1, 3) y.shape: (2, 3) y: [[1 2 1] [2 2 3]]
# 添加维度
x = np.array([[1,2,1],[2,2,3]])
print ("x.shape: ", x.shape)
y = np.expand_dims(x, 1) # 扩展维度1
print ("y.shape: ", y.shape)
print ("y: \n", y)
x.shape: (2, 3) y.shape: (2, 1, 3) y: [[[1 2 1]] [[2 2 3]]]
你不必熟悉本文中任何东西,因为我们将在后面的课程中仔细研究NumPy。如果你对Numpy感到好奇,可以自行查看 NumPy reference manual.