1.The Basics
1.1 numpy 数组基础
NumPy’s array class is called ndarray.
ndarray.ndim
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
ndarray.shape
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.
ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype
an object describing the type of the elements in the array. One can create or specify dtype’s using standard
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'>
1.2 Array Creation 数组生成
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
1.3 Basic Operations 基础运算
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).
2 Shape Manipulation
2.1 Changing the shape of an array
2.2 Stacking together different arrays
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