NumPy是Python进行科学计算的基础模块/包/库。它包含以下部分:
除了它广为人知的科学计算应用, NumPy 还可以作为泛型数据高效的多维容器. 可以定义任意的数据类型使得NumPy能够无缝高效的整合各种数据库。
要学习numpy我们要了解Python的几种内建容器类型
lists, dictionaries, sets, and tuples
列表、字典、集合、元组
We can initialize numpy arrays from nested Python lists, and access elements using square brackets:
我们可以通过嵌套的python列表来初始一个numpy数组,通过一般数组的下表结合中括号来进行元素访问:
import numpy as np
#一般导入numpy模块的格式如上
初识
>>> import numpy as np
>>> a = np.arange(15).reshape(3, 5)
#np.arange( 0, 2, 0.3 )也接受浮点参数
array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
#产生15个数组元素,然后分为三行五列
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
>>> a.itemsize
8
>>> a.size
15
>>> type(a)
'numpy.ndarray'>
>>> b = np.array([6, 7, 8])
>>> b
array([6, 7, 8])
>>> type(b)
'numpy.ndarray'>
ndarray.ndim
numpy数组的维数
ndarray.shape
带有行数和列数的维,结果类似(n,m)
ndarray.size
获得数组元素的个数
使用多个函数创建数组:
a = np.zeros((2,2)) # 创建全是0的数组
print(a) # Prints "[[ 0. 0.]
# [ 0. 0.]]"
b = np.ones((1,2)) # 创建一个全是1的数组
print(b) # Prints "[[ 1. 1.]]"
c = np.full((2,2), 7) # 创建一个常量数组
print(c) # Prints "[[ 7. 7.]
# [ 7. 7.]]"
d = np.eye(2) # 创建 2x2 的恒等矩阵
print(d) # Prints "[[ 1. 0.]
# [ 0. 1.]]"
e = np.random.random((2,2)) # Create an array filled with random values
print(e) # Might print "[[ 0.91940167 0.08143941]
# [ 0.68744134 0.87236687]]"
See also
array, zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, numpy.random.rand, numpy.random.randn, fromfunction, fromfile
数组索引 Array indexing
类似Python lists, numpy数组能够切片,因为数组可能是多维的,所以必须指定每一维的切片索引。
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
#切取数组的第一二行的二三列:
b = a[:2, 1:3]
# [[2 3]
# [6 7]]
#切片得到的是原数据的视图,所以修改切片会修改原数据
print(a[0, 1]) # Prints "2"
b[0, 0] = 77 # b[0, 0] is the same piece of data as a[0, 1]
print(a[0, 1]) # Prints "77"
对于整数所以一个有用的技巧是从一个数组选择或者转变元素
a = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
b = np.array([0, 2, 0, 1])
print(a[np.arange(4), b])
# "[ 1 6 7 11]",即获取了数组a的第一行第一(0)个,第二行第三个元素、第三行第一个、第四行第二个元素。
# Mutate one element from each row of a using the indices in b
a[np.arange(4), b] += 10
#对于数组中的元素,每隔四个数对接下来的元素进行加10操作
print(a) # prints "array([[11, 2, 3],
# [ 4, 5, 16],
# [17, 8, 9],
# [10, 21, 12]])
布尔数组索引: Boolean array indexing lets you pick out arbitrary elements of an array. Frequently this type of indexing is used to select the elements of an array that satisfy some condition.
a = np.array([[1,2], [3, 4], [5, 6]])
bool_idx = (a > 2) # 找出数组中数值大于2的元素,以数组形式返回布尔值
print(bool_idx) # Prints "[[False False]
# [ True True]
# [ True True]]"
#接下来将通过bool_idx数组来提取原数组中对应位置为True的元素返回一个一行数组的排列
print(a[bool_idx]) # Prints "[3 4 5 6]"
# 对于以上的步骤也可以一步实现:
print(a[a > 2]) # Prints "[3 4 5 6]"
数组的数学计算
直接上示例:
x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
print(x + y)
print(np.add(x, y))
#数组加法,以上操作输出均为:
# [[ 6.0 8.0]
# [10.0 12.0]]
print(x - y)
print(np.subtract(x, y))
#数组减法
# [[-4.0 -4.0]
# [-4.0 -4.0]]
print(x * y)
print(np.multiply(x, y))
#数组乘法
# [[ 5.0 12.0]
# [21.0 32.0]]
print(x / y)
print(np.divide(x, y))
# 数组除法
# [[ 0.2 0.33333333]
# [ 0.42857143 0.5 ]]
print(np.sqrt(x))
#对所有数组元素求平方根
# [[ 1. 1.41421356]
# [ 1.73205081 2. ]]
使用dot函数计算向量的内积
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
#内积计算,结果均为219
print(v.dot(w))
print(np.dot(v, w))
#向量积/数组积,结果均为:[29 67]
print(x.dot(v))
print(np.dot(x, v))
#向量/向量积,结果均为:
# [[19 22]
# [43 50]]
print(x.dot(y))
print(np.dot(x, y))
求和函数
更多函数可以查阅官方文档
x = np.array([[1,2],[3,4]])
print(np.sum(x)) #计算所有元素的和,结果为 "10"
print(np.sum(x, axis=0)) # 计算每一列的和,结果为"[4 6]"
print(np.sum(x, axis=1)) #计算每一行的和,结果为:"[3 7]"
矩阵的转置
x = np.array([[1,2], [3,4]])
print(x.T) # Prints "[[1 3]
# [2 4]]"
# 一列矩阵的转置无变换
v = np.array([1,2,3])
print(v) # Prints "[1 2 3]"
print(v.T) # Prints "[1 2 3]"
广播Broadcasting
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
v = np.array([1, 0, 1])
y = np.empty_like(x) # Create an empty matrix with the same shape as x
# Add the vector v to each row of the matrix x with an explicit loop
for i in range(4):
y[i, :] = x[i, :] + v
# Now y is the following
# [[ 2 2 4]
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]
以下为更加精简的用法
vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
print(vv) # Prints "[[1 0 1]
# [1 0 1]
# [1 0 1]
# [1 0 1]]"
y = x + vv # Add x and vv elementwise
print(y) # Prints "[[ 2 2 4
# [ 5 5 7]
# [ 8 8 10]
# [11 11 13]]"