首页 > 代码库 > NumPy常用函数(一)——构造数组函数及代码示例

NumPy常用函数(一)——构造数组函数及代码示例

  NumPy是Python的一个科学计算的基本模块。它是一个Python库,提供了一个多维数组对象,各种衍生对象(如屏蔽数组和矩阵),以及用于数组,数学,逻辑,形状操纵,排序,选择,I/O等快速操作的各种例程 离散傅里叶变换,基本线性代数,基本统计运算,随机模拟等等。

  本文主要列出构造数组常用的函数或者成为子模块

一、0-1数组

empty(shape [,dtype,order])                      返回给定形状和类型的新数组,而不初始化条目。

empty_like(a [,dtype,order,subok])        返回与给定数组相同的形状和类型的新数组。

eye(N [,M,k,dtype])                                 返回一个2-D数组,其中有一个对角线和零。

identity(n [,dtype])                                      返回标识数组。

ones(shape[,dtype,order])                          返回一个给定形状和类型的新数组,填充一个。

ones_like(a [,dtype,order,subok])             返回与给定数组相同的形状和类型的数组。

zeros(shape[,dtype,order])                        返回一个给定形状和类型的新数组,填充零。

zeros_like(a [,dtype,order,subok])         返回与给定数组相同的形状和类型的零数组。

full(shape,fill_value [,dtype,order])              返回一个给定形状和类型的新数组,填充fill_value。

full_like(a,fill_value [,dtype,order,subok])  返回与给定数组相同的形状和类型的完整数组。

代码示例:

1.empty(shape [,dtype,order])
>>> np.empty([2, 2])
array([[ -9.74499359e+001, 6.69583040e-309],
    [ 2.13182611e-314, 3.06959433e-309]]) #random
>>> np.empty([2, 2], dtype=int)
array([[-1073741821, -1067949133],
    [ 496041986, 19249760]]) #random
2.empty_like(a [,dtype,order,subok]) >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[-1073741821, -1073741821, 3], #random     [ 0, 0, -1073741821]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000],#random     [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]])
3. eye(N [,M,k,dtype]) >>> np.eye(2, dtype=int) array([[1, 0],     [0, 1]]) >>> np.eye(3, k=1) array([[ 0., 1., 0.],     [ 0., 0., 1.],     [ 0., 0., 0.]])
4.identity(n [,dtype]) >>> np.identity(3) array([[ 1., 0., 0.],     [ 0., 1., 0.],     [ 0., 0., 1.]])
5.ones(shape[,dtype,order]) >>> np.ones(5) array([ 1., 1., 1., 1., 1.]) >>> np.ones((5,), dtype=np.int) array([1, 1, 1, 1, 1]) >>> np.ones((2, 1)) array([[ 1.],     [ 1.]]) >>> s = (2,2) >>> np.ones(s) array([[ 1., 1.],     [ 1., 1.]])
6.ones_like(a, dtype=None, order=’K’, subok=True) Examples >>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> x array([[0, 1, 2],     [3, 4, 5]]) >>> np.ones_like(x) array([[1, 1, 1],     [1, 1, 1]]) >>> y = np.arange(3, dtype=np.float) >>> y array([ 0., 1., 2.]) >>> np.ones_like(y) array([ 1., 1., 1.])
7.zeros(shape, dtype=float, order=’C’) >>> np.zeros(5) array([ 0., 0., 0., 0., 0.]) >>> np.zeros((5,), dtype=np.int) array([0, 0, 0, 0, 0]) >>> np.zeros((2, 1)) array([[ 0.],     [ 0.]]) >>> s = (2,2) >>> np.zeros(s) array([[ 0., 0.],     [ 0., 0.]]) >>> np.zeros((2,), dtype=[(‘x‘, ‘i4‘), (‘y‘, ‘i4‘)]) # custom dtype array([(0, 0), (0, 0)],   dtype=[(‘x‘, ‘<i4‘), (‘y‘, ‘<i4‘)])
8.zeros_like(a, dtype=None, order=’K’, subok=True) Examples >>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> x array([[0, 1, 2],     [3, 4, 5]]) >>> np.zeros_like(x) array([[0, 0, 0],     [0, 0, 0]]) >>> y = np.arange(3, dtype=np.float) >>> y array([ 0., 1., 2.]) >>> np.zeros_like(y) array([ 0., 0., 0.])
9.full(shape, fill_value, dtype=None, order=’C’) >>> np.full((2, 2), np.inf) array([[ inf, inf],     [ inf, inf]]) >>> np.full((2, 2), 10) array([[10, 10],     [10, 10]])
10.full_like(a, fill_value, dtype=None, order=’K’, subok=True) >>> x = np.arange(6, dtype=np.int) >>> np.full_like(x, 1) array([1, 1, 1, 1, 1, 1]) >>> np.full_like(x, 0.1) array([0, 0, 0, 0, 0, 0]) >>> np.full_like(x, 0.1, dtype=np.double) array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) >>> np.full_like(x, np.nan, dtype=np.double) array([ nan, nan, nan, nan, nan, nan]) >>> y = np.arange(6, dtype=np.double) >>> np.full_like(y, 0.1) array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1])

  二、从现有数据创建数组

array(object[, dtype, copy, order, subok, ndmin])       创建数组

asarray(a[, dtype, order])                                         将输入转为数组

asanyarray(a[, dtype, order])                                 将输入转换为ndarray,但通过ndarray子类.

asmatrix(data[, dtype])                                            将输入解释为矩阵。

copy(a[, order])                                                        返回一个从已有数组的复制数组

frombuffer(buffer[, dtype, count, offset])                    将缓冲区解释为一维数组。.

fromfile(file[, dtype, count, sep])                                从文本或二进制文件中的数据构造数组.

fromfunction(function, shape, **kwargs)                     通过在每坐标上执行一个函数来构造数组。

fromiter(iterable, dtype[, count])                                从一个可迭代的对象创建一个新的1维数组。

fromstring(string[, dtype, count, sep])                         从原始二进制或字符串中的文本数据初始化的新的1-D数组。

loadtxt(fname[, dtype, comments, delimiter, ...])         从文本文件加载数据。

1. array
>>> np.array([1, 2, 3])
array([1, 2, 3])
Upcasting:
>>> np.array([1, 2, 3.0])
array([ 1., 2., 3.])
More than one dimension:
>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
    [3, 4]])
Minimum dimensions 2:
>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])
Type provided:
>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j, 2.+0.j, 3.+0.j])
Data-type consisting of more than one element:
>>> x = np.array([(1,2),(3,4)],dtype=[(‘a‘,‘<i4‘),(‘b‘,‘<i4‘)])
>>> x[‘a‘]
array([1, 3])
Creating an array from sub-classes:
>>> np.array(np.mat(‘1 2; 3 4‘))
array([[1, 2],
    [3, 4]])
>>> np.array(np.mat(‘1 2; 3 4‘), subok=True)
matrix([[1, 2],
    [3, 4]])

2. asarray
Convert a list into an array:
>>> a = [1, 2]
>>> np.asarray(a)
array([1, 2])
Existing arrays are not copied:
>>> a = np.array([1, 2])
>>> np.asarray(a) is a
True
If dtype is set, array is copied only if dtype does not match:
>>> a = np.array([1, 2], dtype=np.float32)
>>> np.asarray(a, dtype=np.float32) is a
True
>>> np.asarray(a, dtype=np.float64) is a
False
Contrary to asanyarray, ndarray subclasses are not passed through:
>>> issubclass(np.matrix, np.ndarray)
True
>>> a = np.matrix([[1, 2]])
>>> np.asarray(a) is a
False
>>> np.asanyarray(a) is a
True

3.asanyarray
Convert a list into an array:
>>> a = [1, 2]
>>> np.asanyarray(a)
array([1, 2])
Instances of ndarray subclasses are passed through as-is:
>>> a = np.matrix([1, 2])
>>> np.asanyarray(a) is a
True

4. asmatrix
>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2],
    [3, 4]])

5. copy
Create an array x, with a reference y and a copy z:
>>> x = np.array([1, 2, 3])
>>> y = x
>>> z = np.copy(x)
Note that, when we modify x, y changes, but not z:
>>> x[0] = 10
>>> x[0] == y[0]
True
>>> x[0] == z[0]
False

6.frombuffer
>>> s = ‘hello world‘
>>> np.frombuffer(s, dtype=‘S1‘, count=5, offset=6)
array([‘w‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘],
dtype=‘|S1‘)

7.fromfile
>>> dt = np.dtype([(‘time‘, [(‘min‘, int), (‘sec‘, int)]),
... (‘temp‘, float)])
>>> x = np.zeros((1,), dtype=dt)
>>> x[‘time‘][‘min‘] = 10; x[‘temp‘] = 98.25
>>> x
array([((10, 0), 98.25)],
dtype=[(‘time‘, [(‘min‘, ‘<i4‘), (‘sec‘, ‘<i4‘)]), (‘temp‘, ‘<f8‘)])
Save the raw data to disk:
>>> import os
>>> fname = os.tmpnam()
>>> x.tofile(fname)
Read the raw data from disk:
>>> np.fromfile(fname, dtype=dt)
array([((10, 0), 98.25)],
dtype=[(‘time‘, [(‘min‘, ‘<i4‘), (‘sec‘, ‘<i4‘)]), (‘temp‘, ‘<f8‘)])
The recommended way to store and load data:
>>> np.save(fname, x)
>>> np.load(fname + ‘.npy‘)
array([((10, 0), 98.25)],
dtype=[(‘time‘, [(‘min‘, ‘<i4‘), (‘sec‘, ‘<i4‘)]), (‘temp‘, ‘<f8‘)])
8.fromfunction >>> np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int) array([[ True, False, False], [False, True, False], [False, False, True]], dtype=bool) >>> np.fromfunction(lambda i, j: i + j, (3, 3), dtype=int) array([[0, 1, 2], [1, 2, 3], [2, 3, 4]]) 9.fromiter >>> iterable = (x*x for x in range(5)) >>> np.fromiter(iterable, np.float) array([ 0., 1., 4., 9., 16.]) 10.fromstring >>> np.fromstring(‘\x01\x02‘, dtype=np.uint8) array([1, 2], dtype=uint8) >>> np.fromstring(‘1 2‘, dtype=int, sep=‘ ‘) array([1, 2]) >>> np.fromstring(‘1, 2‘, dtype=int, sep=‘,‘) array([1, 2]) >>> np.fromstring(‘\x01\x02\x03\x04\x05‘, dtype=np.uint8, count=3) array([1, 2, 3], dtype=uint8) 11. loadtxt >>> from io import StringIO # StringIO behaves like a file object >>> c = StringIO("0 1\n2 3") >>> np.loadtxt(c) array([[ 0., 1.], [ 2., 3.]]) >>> d = StringIO("M 21 72\nF 35 58") >>> np.loadtxt(d, dtype={‘names‘: (‘gender‘, ‘age‘, ‘weight‘), ... ‘formats‘: (‘S1‘, ‘i4‘, ‘f4‘)}) array([(‘M‘, 21, 72.0), (‘F‘, 35, 58.0)], dtype=[(‘gender‘, ‘|S1‘), (‘age‘, ‘<i4‘), (‘weight‘, ‘<f4‘)]) >>> c = StringIO("1,0,2\n3,0,4") >>> x, y = np.loadtxt(c, delimiter=‘,‘, usecols=(0, 2), unpack=True) >>> x array([ 1., 3.]) >>> y array([ 2., 4.])

  

三、生成序列

arange([start,] stop[, step,][, dtype])                            返回给定间隔内均匀间隔的值。

linspace(start, stop[, num, endpoint, ...])                      在指定的间隔内返回均匀间隔的数字。

logspace(start, stop[, num, endpoint, base, ...])            返回数字在对数量级上均匀间隔。

geomspace(start, stop[, num, endpoint, dtype])          返回数字在对数刻度上平均间隔(几何级数)。

meshgrid(*xi, **kwargs)                                     从坐标向量返回坐标矩阵。.

mgrid                                                                        实例返回一个密集的多维“meshgrid”。.

ogrid                                                                         实例返回一个开放的多维“meshgrid”

1.arange 
>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3.0)
array([ 0., 1., 2.])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5])

2. linspace
>>> np.linspace(2.0, 3.0, num=5)
array([ 2. , 2.25, 2.5 , 2.75, 3. ])
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([ 2. , 2.2, 2.4, 2.6, 2.8])
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
Graphical illustration:
>>> import matplotlib.pyplot as plt
>>> N = 8
>>> y = np.zeros(N)
>>> x1 = np.linspace(0, 10, N, endpoint=True)
>>> x2 = np.linspace(0, 10, N, endpoint=False)
>>> plt.plot(x1, y, ‘o‘)
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(x2, y + 0.5, ‘o‘)
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim([-0.5, 1])
(-0.5, 1)
>>> plt.show()
 

3. logspace
>>> y = np.linspace(start, stop, num=num, endpoint=endpoint)
...
>>> power(base, y).astype(dtype)
...
Examples
>>> np.logspace(2.0, 3.0, num=4)
array([ 100. , 215.443469 , 464.15888336, 1000. ])
>>> np.logspace(2.0, 3.0, num=4, endpoint=False)
array([ 100. , 177.827941 , 316.22776602, 562.34132519])
>>> np.logspace(2.0, 3.0, num=4, base=2.0)
array([ 4. , 5.0396842 , 6.34960421, 8. ])
Graphical illustration:
>>> import matplotlib.pyplot as plt
>>> N = 10
>>> x1 = np.logspace(0.1, 1, N, endpoint=True)
>>> x2 = np.logspace(0.1, 1, N, endpoint=False)
>>> y = np.zeros(N)
>>> plt.plot(x1, y, ‘o‘)
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(x2, y + 0.5, ‘o‘)
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim([-0.5, 1])
(-0.5, 1)
>>> plt.show()

4.geomspace
>>> np.geomspace(1, 1000, num=4)
array([ 1., 10., 100., 1000.])
>>> np.geomspace(1, 1000, num=3, endpoint=False)
array([ 1., 10., 100.])
>>> np.geomspace(1, 1000, num=4, endpoint=False)
array([ 1. , 5.62341325, 31.6227766 , 177.827941 ])
>>> np.geomspace(1, 256, num=9)
array([ 1., 2., 4., 8., 16., 32., 64., 128., 256.])
Note that the above may not produce exact integers:
>>> np.geomspace(1, 256, num=9, dtype=int)
array([ 1, 2, 4, 7, 16, 32, 63, 127, 256])
>>> np.around(np.geomspace(1, 256, num=9)).astype(int)
array([ 1, 2, 4, 8, 16, 32, 64, 128, 256])
Negative, decreasing, and complex inputs are allowed:
>>> geomspace(1000, 1, num=4)
array([ 1000., 100., 10., 1.])
>>> geomspace(-1000, -1, num=4)
array([-1000., -100., -10., -1.])
>>> geomspace(1j, 1000j, num=4) # Straight line
array([ 0. +1.j, 0. +10.j, 0. +100.j, 0.+1000.j])
>>> geomspace(-1+0j, 1+0j, num=5) # Circle
array([-1.00000000+0.j , -0.70710678+0.70710678j,
0.00000000+1.j , 0.70710678+0.70710678j,
1.00000000+0.j ])
Graphical illustration of endpoint parameter:
>>> import matplotlib.pyplot as plt
>>> N = 10
>>> y = np.zeros(N)
>>> plt.semilogx(np.geomspace(1, 1000, N, endpoint=True), y + 1, ‘o‘)
>>> plt.semilogx(np.geomspace(1, 1000, N, endpoint=False), y + 2, ‘o‘)
>>> plt.axis([0.5, 2000, 0, 3])
>>> plt.grid(True, color=‘0.7‘, linestyle=‘-‘, which=‘both‘, axis=‘both‘)
>>> plt.show()

5. meshgrid
>>> nx, ny = (3, 2)
>>> x = np.linspace(0, 1, nx)
>>> y = np.linspace(0, 1, ny)
>>> xv, yv = meshgrid(x, y)
>>> xv
array([[ 0. , 0.5, 1. ],
[ 0. , 0.5, 1. ]])
>>> yv
array([[ 0., 0., 0.],
[ 1., 1., 1.]])
>>> xv, yv = meshgrid(x, y, sparse=True) # make sparse output arrays
>>> xv
array([[ 0. , 0.5, 1. ]])
>>> yv
array([[ 0.],
[ 1.]])
meshgrid is very useful to evaluate functions on a grid.
>>> x = np.arange(-5, 5, 0.1)
>>> y = np.arange(-5, 5, 0.1)
>>> xx, yy = meshgrid(x, y, sparse=True)
>>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
>>> h = plt.contourf(x,y,z)
6.mgrid
>>> np.mgrid[0:5,0:5]
array([[[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4]],
[[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]]])
>>> np.mgrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])

7.ogrid
>>> from numpy import ogrid
>>> ogrid[-1:1:5j]
array([-1. , -0.5, 0. , 0.5, 1. ])
>>> ogrid[0:5,0:5]
[array([[0],
[1],
[2],
[3],
[4]]), array([[0, 1, 2, 3, 4]])]

  

NumPy常用函数(一)——构造数组函数及代码示例