NumPy

在本课中,我们将学习使用NumPy包进行数值分析的基础知识。

NumPy 基础知识

In [0]:
import numpy as np
In [0]:
# 使得多次生成的随机数相同
np.random.seed(seed=1234)
In [3]:
# 标量(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
In [4]:
# 一维数组(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
In [5]:
# 三维数组(矩阵(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
In [6]:
# 函数
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]]

索引

In [7]:
# 索引(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]
In [8]:
# 切片(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]]
In [9]:
# 整数数组索引
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]
In [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]

数组基础知识

In [11]:
# 数学基础
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.]]

In [12]:
# 点积
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.]]
In [13]:
# 跨维度求和
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]
In [14]:
# 转置
print ("x:\n", x)
print ("x.T:\n", x.T)
x:
 [[1 2]
 [3 4]]
x.T:
 [[1 3]
 [2 4]]

数组高级知识

In [15]:
# 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]]
In [16]:
# 广播(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]]
In [17]:
# 改变维度
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]]
In [18]:
# 删除维度
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]]
In [19]:
# 添加维度
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.

In [0]: