Python—numpy模組下函式介紹(一)numpy.ones、empty等
NumPy陣列的維數稱為秩(rank),一維陣列的秩為1,二維陣列的秩為2,以此類推。在NumPy中,每一個線性的陣列稱為是一個軸(axes),秩其實是描述軸的數量。比如說,二維陣列相當於是一個一維陣列,而這個一維陣列中每個元素又是一個一維陣列。所以這個一維陣列就是NumPy中的軸(axes),而軸的數量——秩,就是陣列的維數。
首先來看看以np.ones為例的英文引數介紹
numpy.ones(shape, dtype=None, order=’C’)
Return a new array of given shape and type, filled with ones. Parameters:
shape : int or sequence of ints
Shape of the new array, e.g., (2, 3) or 2.
dtype : data-type, optional
The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
order : {‘C’, ‘F’}, optional
Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory.
Returns: out : ndarray
Array of ones with the given shape, dtype, and order.
1、empty(shape[, dtype, order])
依據給定形狀和型別(shape[, dtype, order])返回一個新的空陣列。
引數:shape : 整數或者整型元組定義返回陣列的形狀;
dtype : 資料型別,可選定義返回陣列的型別。
order : {‘C’, ‘F’}, 可選規定返回陣列元素在記憶體的儲存順序:C(C語言)-rowmajor;F(Fortran)column-major。
print('\nnp.empty([2,2])生成的array=\n{}'.format(np.empty([2,2])))
print('\nnp.empty([2,2],dtype=int)生成的array=\n{}'.format(np.empty([2,2],dtype=int)))
2、empty_like(a)
依據給定陣列(a)的形狀和型別返回一個新的空陣列
a=np.array([[1.,2.,3.],[4.,5.,6.]])
print('\nnp.empty_like(a)生成的array=\n{}'.format(np.empty_like(a)))#輸出:ndarray與陣列a形狀和型別一樣的陣列。
3、eye(N[, M, k, dtype])
返回一個對角線元素為1,其他元素為0的二維陣列。
引數:
N : 整數返回陣列的行數;
M : 整數,可選返回陣列的列數。如果不賦值的話,預設等於N;
k : 整數, 可選對角線序列號: 0 對應主對角線;,整數對應upper diagonal,負數對應lower diagonal;
dtype : dtype, 可選
返回陣列的資料型別
I : ndarray (N,M)該陣列第k個對角線的元素為1,其他元素為0。
print('\nnp.eye(2,dtype=int)生成的array=\n{}'.format(np.eye(2,dtype=int)))
print('\nnp.eye(3,k=1)生成的array=\n{}'.format(np.eye(3,k=1)))
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])
返回一個N維單位方陣。
引數:
n : 整數返回方陣的行列數;
dtype : 資料型別,可選返回方陣的資料型別,預設為float.
返回值:輸出: ndarrayn x n 單位方陣。
print('\nnp.identity(3)生成的array=\n{}'.format(np.identity(3)))
np.identity(3)生成的array=
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
5、zeros(shape[, dtype, order])
依據給定形狀和型別(shape[, dtype, order])返回一個新的元素全部為0的陣列。
引數:
shape:int或者ints元組;定義返回陣列的形狀,形如:(2, 3)或2。
dtype:資料型別,可選。
返回陣列的資料型別,例如:numpy.int8、預設numpy.float64。
order:{‘C’, ‘F’},可選,返回陣列為多維時,元素在記憶體的排列方式是按C語言還是Fortran語言順序(row- or columnwise)。
輸出:ndarray給定形狀,資料型別的陣列。
print('\nnp.zeros(5)生成的array=\n{}'.format(np.ones(5)))
print('\nnp.zeros((5,),dtype=np.int)生成的array=\n{}'.format(np.zeros((5,),dtype=np.int)))
print('\nnp.zeros((2,1))生成的array=\n{}'.format(np.zeros((2,1))))
S=(2,2)
print('\nnp.zeros(S)生成的array=\n{}'.format(np.zeros(S)))
print('\n np.zeros((2,), dtype=[(’x’, ’i4’), (’y’, ’i4’)])生成的array=\n{}'.format( np.zeros((2,), dtype=[('x','i4'), ('y','i4')])))
print(type(np.zeros((2,), dtype=[('x','i4'), ('y','i4')])))
x=np.arange(6)
x=x.reshape((2,3)) #([[0, 1, 2],[3, 4, 5]])
print('\nnp.zeros_like(x)生成的array=\n{}'.format(np.zeros_like(x)))
y=np.arange(3,dtype=np.float) #([ 0., 1., 2.])
print('\nnp.zeros_like(y)生成的array=\n{}'.format(np.zeros_like(y)))
np.zeros(5)生成的array=
[ 1. 1. 1. 1. 1.]
np.zeros((5,),dtype=np.int)生成的array=
[0 0 0 0 0]
np.zeros((2,1))生成的array=
[[ 0.]
[ 0.]]
np.zeros(S)生成的array=
[[ 0. 0.]
[ 0. 0.]]
np.zeros((2,), dtype=[(’x’, ’i4’), (’y’, ’i4’)])生成的array=
[(0, 0) (0, 0)]
<class 'numpy.ndarray'>
np.zeros_like(x)生成的array=
[[0 0 0]
[0 0 0]]
np.zeros_like(y)生成的array=
[ 0. 0. 0.]
6、ones(shape[, dtype, order])
依據給定形狀和型別(shape[, dtype, order])返回一個新的元素全部為1的陣列。相應用法同5.zeros
print('\nnp.ones(4)生成的array=\n{}'.format(np.ones(4)))
print('\nnp.ones((4,),dtype=np.int)生成的array=\n{}'.format(np.ones((4,),dtype=np.int)))
print('\nnp.ones((2,1))生成的array=\n{}'.format(np.ones((2,1))))
S=(2,2)
print('\nnp.ones(S)生成的array=\n{}'.format(np.ones(S)))
a=np.array([[1,2,3],[4,5,6]])
print('\nnp.ones_like(a)生成的array=\n{}'.format(np.ones_like(a)))#等同於a.copy().fill(1)
np.ones(4)生成的array=
[ 1. 1. 1. 1.]
np.ones((4,),dtype=np.int)生成的array=
[1 1 1 1]
np.ones((2,1))生成的array=
[[ 1.]
[ 1.]]
np.ones(S)生成的array=
[[ 1. 1.]
[ 1. 1.]]
np.ones_like(a)生成的array=
[[1 1 1]
[1 1 1]]
其中i4與i8等可以參考:直通車
Little-Endian與BIG-ENDIAN參考:直通車
其他方面陣列操作參考連線