| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
NumPy 是一个运行速度非常快的 Python 数学库,主要用于数组计算。 这里总结一些常用的功能,供查阅。
对于使用 Python 库,第一步必然是import:
import numpy as npNumPy 的核心是数组 (arrays)。具体来说是多维数组 (ndarrays)。其中几个常用的属性和方法:
np.array([1,2,3,4])np.array([(1.5,2,3), (4,5,6)])np.array( [ [1,2], [3,4] ], dtype=complex )for row in b: # loop 每行
print(row)
for element in b.flat: # loop 每个元素
print(element)数组所有元素的和的一元操作。通过指定 axis 参数可以将操作应用于数组的某一具体 axis 。
全局函数操作数组中每个元素,输出一个数组。
ndarray.reshape(shape)
ndarray.resize(shape)
ndarray.T
>>> a = np.floor(10*np.random.random((2,2)))
>>> a
array([[ 8., 8.],
[ 0., 0.]])
>>> b = np.floor(10*np.random.random((2,2)))
>>> b
array([[ 1., 8.],
[ 0., 4.]])
>>> np.vstack((a,b))
array([[ 8., 8.],
[ 0., 0.],
[ 1., 8.],
[ 0., 4.]])
>>> np.hstack((a,b))
array([[ 8., 8., 1., 8.],
[ 0., 0., 0., 4.]])>>> a = np.floor(10*np.random.random((2,12)))
>>> a
array([[ 9., 5., 6., 3., 6., 8., 0., 7., 9., 7., 2., 7.],
[ 1., 4., 9., 2., 2., 1., 0., 6., 2., 2., 4., 0.]])
>>> np.hsplit(a,3) # 水平拆分为3个数组
[array([[ 9., 5., 6., 3.],
[ 1., 4., 9., 2.]]),
array([[ 6., 8., 0., 7.],
[ 2., 1., 0., 6.]]),
array([[ 9., 7., 2., 7.],
[ 2., 2., 4., 0.]])]在操作数组的时候,数据有时拷贝到新的数组,有时候又不拷贝。
>>> c = a.view()
>>> c is a
False
>>> c.base is a # c is a view of the data owned by a
True
>>> c.flags.owndata
False
>>>
>>> c.shape = 2,6 # a's shape doesn't change
>>> a.shape
(3, 4)
>>> c[0,4] = 1234 # a's data changes>>> d = a.copy() # a new array object with new data is created
>>> d is a
False
>>> d.base is a # d doesn't share anything with a
False数组创建 Array Creation
转换 Conversions
操作 Manipulations
探测 Questions
排序 Ordering
运算 Operations
基本统计 Basic Statistics
基本线性代数 Basic Linear Algebra
广播允许全局函数 (universal functions) 输入不相同的形状的数组。
Image (3d array): 256 x 256 x 3 Scale (1d array): 3 Result (3d array): 256 x 256 x 3 A (4d array): 8 x 1 x 6 x 1 B (3d array): 7 x 1 x 5 Result (4d array): 8 x 7 x 6 x 5 A (2d array): 5 x 4 B (1d array): 1 Result (2d array): 5 x 4 A (2d array): 15 x 3 x 5 B (1d array): 15 x 1 x 5 Result (2d array): 15 x 3 x 5
下面是 NumPy 官方的几个说明图:
>>> a = np.arange(12)**2
>>> i = np.array([1,1,3,8,5]) # an array of indices
>>> a[i]
array([ 1, 1, 9, 64, 25])
>>>
>>> j = np.array([[3,4],
[9,7]])
>>> a[j]
array([[ 9, 16],
[81, 49]])
>>> a = np.arange(12).reshape(3,4)
>>> a
array([[0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> i = np.array([[0,1], # indices for the first dim of a
... [1,2]])
>>> j = np.array([[2,1], # indices for the second dim
... [3,3]])
>>>
>>> a[i,j] # i and j must have equal shape
array([[ 2, 5],
[ 7, 11]])使用索引数组对数组赋值:
>>> a = np.arange(5)
>>> a
array([0, 1, 2, 3, 4])
>>> a[[1,3,4]] = 0
>>> a
array([0, 0, 2, 0, 0])我们可以通过一个布尔数组来索引目标数组,以此找出与布尔数组中值为True的对应的目标数组中的数据。 布尔数组的长度必须与目标数组对应的轴的长度一致。
>>> a = np.arange(12).reshape(3,4)
>>> b = a > 4
>>> b
array([[False, False, False, False],
[False, True, True, True],
[ True, True, True, True]], dtype=bool)
>>> a[b]
array([ 5, 6, 7, 8, 9, 10, 11])选择性赋值:
>>> a[b] = 0
>>> a
array([[0, 1, 2, 3],
[4, 0, 0, 0],
[0, 0, 0, 0]])NumPy 可以实现大量的矩阵操作,例如:
linalg中的常用函数:
为了改变数组的维度,你可以省略一个可以自动被推算出来的大小的参数。
>>> a = np.arange(30)
>>> a.shape = 2,-1,3 # -1 means "whatever is needed"
>>> a.shape
(2, 5, 3)NumPy 的 histogram 函数应用于数组,返回两个vector:数组的柱状图和 bins 的vector
(n, bins) = np.histogram(v, bins=50, density=True) # NumPy version (no plot)
plt.plot(.5*(bins[1:]+bins[:-1]), n)
plt.show()| Back | FazBrowse Home | New Git URL |