摘自https://docs.scipy.orgpython
NumPy’s array class is called ndarray.數組
ndarray.ndim app
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.dom
ndarray.shapeide
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For
a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the
rank, or number of dimensions, ndim.ui
ndarray.sizespa
the total number of elements of the array. This is equal to the product of the elements of shape.code
ndarray.dtypeblog
an object describing the type of the elements in the array. One can create or specify dtype’s using standardip
Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64
are some examples.
Example:
>>> import numpy as np >>> a = np.arange(15).reshape(3, 5) >>> 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) <type 'numpy.ndarray'> >>> b = np.array([6, 7, 8]) >>> b array([6, 7, 8]) >>> type(b) <type 'numpy.ndarray'>
You can create an array from a regular Python list or tuple using the array function. The type of the resulting array is deduced from the type of the elements in the sequences. A frequent error consists in calling array with multiple numeric arguments, rather than providing a single list of numbers as an argument.(常見錯誤是把數值做爲參數建立數組,應該傳入list或者tuple)
>>> a = np.array(1,2,3,4) # WRONG >>> a = np.array([1,2,3,4]) # RIGHT
The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64. (常見錯誤: np.zeros(3,4) ,正確應該爲 np.zeros( (3,4) )).
To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.
It is usually better to use the function linspace that receives as an argument the number of elements that we want, instead of the step
Arithmetic operators on arrays apply elementwise.
b**2
array([0, 1, 4, 9])
numpy product:
>>> A = np.array( [[1,1], ... [0,1]] ) >>> B = np.array( [[2,0], ... [3,4]] ) >>> A*B # elementwise product array([[2, 0], [0, 4]]) >>> A.dot(B) # matrix product array([[5, 4], [3, 4]]) >>> np.dot(A, B) # another matrix product array([[5, 4], [3, 4]])
Some operations, such as += and *=, act in place to modify an existing array rather than create a new one.
When operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as upcasting).
https://www.douban.com/note/518335786/?type=like
The function column_stack stacks 1D arrays as columns into a 2D array. It is equivalent to hstack only for 2D arrays; On the other hand, the function row_stack is equivalent to vstack for any input arrays. In general, for arrays of with more than two dimensions, hstack stacks along their second axes, vstack stacks along their first axes, and concatenate allows for an optional arguments giving the number of the axis along which the concatenation should happen.
在anaconda中,python源代碼中,查看row_stack的定義結果指向了vstack,查看column_stack指向了和hstack,且hstack和vstack都是用的concatenate操做實現的。故row_stack和vstack等價,column和hstack等價。
vstack(),等價於row_stack() 和 np.concatenate(tup, axis=0)
Stack arrays in sequence vertically (row wise).
>>> a = np.array([1, 2, 3]) >>> b = np.array([2, 3, 4]) >>> np.vstack((a,b)) array([[1, 2, 3], [2, 3, 4]]) >>> a = np.array([[1], [2], [3]]) >>> b = np.array([[2], [3], [4]]) >>> np.vstack((a,b)) array([[1], [2], [3], [2], [3], [4]])
hstack(),等價於column_stack() 和 np.concatenate(tup, axis=1)
>>> a = np.array((1,2,3)) >>> b = np.array((2,3,4)) >>> np.hstack((a,b)) array([1, 2, 3, 2, 3, 4]) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[2],[3],[4]]) >>> np.hstack((a,b)) array([[1, 2], [2, 3], [3, 4]])
dstack(), 等價於np.concatenate(tup, axis=2)
>>> a = np.array((1,2,3)) >>> b = np.array((2,3,4)) >>> np.dstack((a,b)) array([[[1, 2], [2, 3], [3, 4]]]) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[2],[3],[4]]) >>> np.dstack((a,b)) array([[[1, 2]], [[2, 3]], [[3, 4]]])
concatenate() 默認axis = 0
np.c_[]
np.r_[] 分別添加行和列
np.insert