1. 程式人生 > >NumPy常用函數(一)——構造數組函數及代碼示例

NumPy常用函數(一)——構造數組函數及代碼示例

unp tom 形狀 top instance produce base dom 新的

  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常用函數(一)——構造數組函數及代碼示例