{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5]\n"
]
}
],
"source": [
"import numpy as np #np.作为开头\n",
"\n",
"array = np.array([1,2,3,4,5])\n",
"\n",
"print(array)\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5,)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array.shape"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array.ndim"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array[1]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array[4]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array[-1]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1 2 -1 4 5]\n"
]
}
],
"source": [
"array[-3] = -1\n",
"print(array)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 常见矩阵的创建"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0. 0.]\n",
" [0. 0. 0.]\n",
" [0. 0. 0.]]\n"
]
}
],
"source": [
"data1 = np.matrix(np.zeros((3,3)))\n",
"print(data1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"\n",
"(3, 3)\n"
]
}
],
"source": [
"print(data1.ndim,data1.shape,sep='\\n\\n')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 1. 1. 1.]\n",
" [1. 1. 1. 1.]]\n"
]
}
],
"source": [
"data2 = np.matrix(np.ones((2,4)))\n",
"print(data2)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.31649128 0.73303736 0.57778256 0.13639505]\n",
" [0.97718723 0.90824372 0.01491877 0.05402118]\n",
" [0.48543903 0.18164597 0.95843084 0.93829958]]\n"
]
}
],
"source": [
"data3 = np.matrix(np.random.rand(3,4))\n",
"print(data3)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [3 0 0]]\n"
]
}
],
"source": [
"#randint(low,high=none,size =None)\n",
"data4 = np.matrix(np.random.randint(5,size=(2,3)))\n",
"print(data4)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 2 3 2 3 7]\n",
" [ 8 9 8 12 7]\n",
" [ 5 2 10 2 3]]\n"
]
}
],
"source": [
"data5 = np.matrix(np.random.randint(2,14,size=(3,5)))\n",
"print(data5)\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function eye in module numpy.lib.twodim_base:\n",
"\n",
"eye(N, M=None, k=0, dtype=, order='C')\n",
" Return a 2-D array with ones on the diagonal and zeros elsewhere.\n",
" \n",
" Parameters\n",
" ----------\n",
" N : int\n",
" Number of rows in the output.\n",
" M : int, optional\n",
" Number of columns in the output. If None, defaults to `N`.\n",
" k : int, optional\n",
" Index of the diagonal: 0 (the default) refers to the main diagonal,\n",
" a positive value refers to an upper diagonal, and a negative value\n",
" to a lower diagonal.\n",
" dtype : data-type, optional\n",
" Data-type of the returned array.\n",
" order : {'C', 'F'}, optional\n",
" Whether the output should be stored in row-major (C-style) or\n",
" column-major (Fortran-style) order in memory.\n",
" \n",
" .. versionadded:: 1.14.0\n",
" \n",
" Returns\n",
" -------\n",
" I : ndarray of shape (N,M)\n",
" An array where all elements are equal to zero, except for the `k`-th\n",
" diagonal, whose values are equal to one.\n",
" \n",
" See Also\n",
" --------\n",
" identity : (almost) equivalent function\n",
" diag : diagonal 2-D array from a 1-D array specified by the user.\n",
" \n",
" Examples\n",
" --------\n",
" >>> np.eye(2, dtype=int)\n",
" array([[1, 0],\n",
" [0, 1]])\n",
" >>> np.eye(3, k=1)\n",
" array([[ 0., 1., 0.],\n",
" [ 0., 0., 1.],\n",
" [ 0., 0., 0.]])\n",
"\n"
]
}
],
"source": [
"help(np.eye)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0. 0.]\n",
" [1. 0. 0.]\n",
" [0. 1. 0.]]\n"
]
}
],
"source": [
"data6 = np.eye(3,k=-1)\n",
"print(data6)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0 2 0 0]\n",
" [0 0 4 0]\n",
" [0 0 0 5]\n",
" [0 0 0 0]]\n"
]
}
],
"source": [
"a1 = [2,4,5]\n",
"a2 = np.matrix(np.diag(a1,k=1))\n",
"print(a2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## reshape"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function reshape in module numpy.core.fromnumeric:\n",
"\n",
"reshape(a, newshape, order='C')\n",
" Gives a new shape to an array without changing its data.\n",
" \n",
" Parameters\n",
" ----------\n",
" a : array_like\n",
" Array to be reshaped.\n",
" newshape : int or tuple of ints\n",
" The new shape should be compatible with the original shape. If\n",
" an integer, then the result will be a 1-D array of that length.\n",
" One shape dimension can be -1. In this case, the value is\n",
" inferred from the length of the array and remaining dimensions.\n",
" order : {'C', 'F', 'A'}, optional\n",
" Read the elements of `a` using this index order, and place the\n",
" elements into the reshaped array using this index order. 'C'\n",
" means to read / write the elements using C-like index order,\n",
" with the last axis index changing fastest, back to the first\n",
" axis index changing slowest. 'F' means to read / write the\n",
" elements using Fortran-like index order, with the first index\n",
" changing fastest, and the last index changing slowest. Note that\n",
" the 'C' and 'F' options take no account of the memory layout of\n",
" the underlying array, and only refer to the order of indexing.\n",
" 'A' means to read / write the elements in Fortran-like index\n",
" order if `a` is Fortran *contiguous* in memory, C-like order\n",
" otherwise.\n",
" \n",
" Returns\n",
" -------\n",
" reshaped_array : ndarray\n",
" This will be a new view object if possible; otherwise, it will\n",
" be a copy. Note there is no guarantee of the *memory layout* (C- or\n",
" Fortran- contiguous) of the returned array.\n",
" \n",
" See Also\n",
" --------\n",
" ndarray.reshape : Equivalent method.\n",
" \n",
" Notes\n",
" -----\n",
" It is not always possible to change the shape of an array without\n",
" copying the data. If you want an error to be raised when the data is copied,\n",
" you should assign the new shape to the shape attribute of the array::\n",
" \n",
" >>> a = np.zeros((10, 2))\n",
" # A transpose makes the array non-contiguous\n",
" >>> b = a.T\n",
" # Taking a view makes it possible to modify the shape without modifying\n",
" # the initial object.\n",
" >>> c = b.view()\n",
" >>> c.shape = (20)\n",
" AttributeError: incompatible shape for a non-contiguous array\n",
" \n",
" The `order` keyword gives the index ordering both for *fetching* the values\n",
" from `a`, and then *placing* the values into the output array.\n",
" For example, let's say you have an array:\n",
" \n",
" >>> a = np.arange(6).reshape((3, 2))\n",
" >>> a\n",
" array([[0, 1],\n",
" [2, 3],\n",
" [4, 5]])\n",
" \n",
" You can think of reshaping as first raveling the array (using the given\n",
" index order), then inserting the elements from the raveled array into the\n",
" new array using the same kind of index ordering as was used for the\n",
" raveling.\n",
" \n",
" >>> np.reshape(a, (2, 3)) # C-like index ordering\n",
" array([[0, 1, 2],\n",
" [3, 4, 5]])\n",
" >>> np.reshape(np.ravel(a), (2, 3)) # equivalent to C ravel then C reshape\n",
" array([[0, 1, 2],\n",
" [3, 4, 5]])\n",
" >>> np.reshape(a, (2, 3), order='F') # Fortran-like index ordering\n",
" array([[0, 4, 3],\n",
" [2, 1, 5]])\n",
" >>> np.reshape(np.ravel(a, order='F'), (2, 3), order='F')\n",
" array([[0, 4, 3],\n",
" [2, 1, 5]])\n",
" \n",
" Examples\n",
" --------\n",
" >>> a = np.array([[1,2,3], [4,5,6]])\n",
" >>> np.reshape(a, 6)\n",
" array([1, 2, 3, 4, 5, 6])\n",
" >>> np.reshape(a, 6, order='F')\n",
" array([1, 4, 2, 5, 3, 6])\n",
" \n",
" >>> np.reshape(a, (3,-1)) # the unspecified value is inferred to be 2\n",
" array([[1, 2],\n",
" [3, 4],\n",
" [5, 6]])\n",
"\n"
]
}
],
"source": [
"help(np.reshape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ndarray.reshape np.reshape"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 2 3 2]\n",
" [ 3 7 8]\n",
" [ 9 8 12]\n",
" [ 7 5 2]\n",
" [10 2 3]]\n"
]
}
],
"source": [
"r = data5.reshape((5,3))\n",
"print(r)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on built-in function arange in module numpy.core.multiarray:\n",
"\n",
"arange(...)\n",
" arange([start,] stop[, step,], dtype=None)\n",
" \n",
" Return evenly spaced values within a given interval.\n",
" \n",
" Values are generated within the half-open interval ``[start, stop)``\n",
" (in other words, the interval including `start` but excluding `stop`).\n",
" For integer arguments the function is equivalent to the Python built-in\n",
" `range `_ function,\n",
" but returns an ndarray rather than a list.\n",
" \n",
" When using a non-integer step, such as 0.1, the results will often not\n",
" be consistent. It is better to use ``linspace`` for these cases.\n",
" \n",
" Parameters\n",
" ----------\n",
" start : number, optional\n",
" Start of interval. The interval includes this value. The default\n",
" start value is 0.\n",
" stop : number\n",
" End of interval. The interval does not include this value, except\n",
" in some cases where `step` is not an integer and floating point\n",
" round-off affects the length of `out`.\n",
" step : number, optional\n",
" Spacing between values. For any output `out`, this is the distance\n",
" between two adjacent values, ``out[i+1] - out[i]``. The default\n",
" step size is 1. If `step` is specified as a position argument,\n",
" `start` must also be given.\n",
" dtype : dtype\n",
" The type of the output array. If `dtype` is not given, infer the data\n",
" type from the other input arguments.\n",
" \n",
" Returns\n",
" -------\n",
" arange : ndarray\n",
" Array of evenly spaced values.\n",
" \n",
" For floating point arguments, the length of the result is\n",
" ``ceil((stop - start)/step)``. Because of floating point overflow,\n",
" this rule may result in the last element of `out` being greater\n",
" than `stop`.\n",
" \n",
" See Also\n",
" --------\n",
" linspace : Evenly spaced numbers with careful handling of endpoints.\n",
" ogrid: Arrays of evenly spaced numbers in N-dimensions.\n",
" mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions.\n",
" \n",
" Examples\n",
" --------\n",
" >>> np.arange(3)\n",
" array([0, 1, 2])\n",
" >>> np.arange(3.0)\n",
" array([ 0., 1., 2.])\n",
" >>> np.arange(3,7)\n",
" array([3, 4, 5, 6])\n",
" >>> np.arange(3,7,2)\n",
" array([3, 5])\n",
"\n"
]
}
],
"source": [
"help(np.arange)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
" [[12 13 14 15]\n",
" [16 17 18 19]\n",
" [20 21 22 23]]]\n"
]
}
],
"source": [
"r_2 = np.arange(24).reshape(2,3,4)\n",
"print(r_2)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 2 3 4 8]\n",
" [15 17 9 12]\n",
" [19 6 8 8]]\n",
"\n",
" [[ 6 13 15 6]\n",
" [10 3 18 19]\n",
" [12 16 22 13]]]\n"
]
}
],
"source": [
"r_3 = np.random.randint(24,size=(2,3,4))\n",
"print(r_3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### slice 和 index"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 一维数组"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3 4 5 6 7 8 9]\n"
]
}
],
"source": [
"arr = np.arange(10)\n",
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[3]"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[-3]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 4, 5, 6, 7])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[3:8]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0 1 2 -2 -2 -2 -2 -2 8 9]\n"
]
}
],
"source": [
"arr[3:8] = -2\n",
"print(arr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 多维数组"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
" [[12 13 14 15]\n",
" [16 17 18 19]\n",
" [20 21 22 23]]]\n"
]
}
],
"source": [
"arr_2 = np.arange(24).reshape(2,3,4)\n",
"print(arr_2)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr_2[0]"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([4, 5, 6, 7])"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr_2[0,1]"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 9, 10, 11])"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr_2[0,2,1:]"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 5, 9],\n",
" [13, 17, 21]])"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr_2[:,:,1]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [5, 6, 7]])"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr_2[0,0:2,1:]"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 2],\n",
" [ 8, 10]])"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr_2[0,::2,::2]"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 2, 8, 10])"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr_2[[0,0,0,0],[0,0,2,2],[0,2,0,2]]"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function ix_ in module numpy.lib.index_tricks:\n",
"\n",
"ix_(*args)\n",
" Construct an open mesh from multiple sequences.\n",
" \n",
" This function takes N 1-D sequences and returns N outputs with N\n",
" dimensions each, such that the shape is 1 in all but one dimension\n",
" and the dimension with the non-unit shape value cycles through all\n",
" N dimensions.\n",
" \n",
" Using `ix_` one can quickly construct index arrays that will index\n",
" the cross product. ``a[np.ix_([1,3],[2,5])]`` returns the array\n",
" ``[[a[1,2] a[1,5]], [a[3,2] a[3,5]]]``.\n",
" \n",
" Parameters\n",
" ----------\n",
" args : 1-D sequences\n",
" Each sequence should be of integer or boolean type.\n",
" Boolean sequences will be interpreted as boolean masks for the\n",
" corresponding dimension (equivalent to passing in\n",
" ``np.nonzero(boolean_sequence)``).\n",
" \n",
" Returns\n",
" -------\n",
" out : tuple of ndarrays\n",
" N arrays with N dimensions each, with N the number of input\n",
" sequences. Together these arrays form an open mesh.\n",
" \n",
" See Also\n",
" --------\n",
" ogrid, mgrid, meshgrid\n",
" \n",
" Examples\n",
" --------\n",
" >>> a = np.arange(10).reshape(2, 5)\n",
" >>> a\n",
" array([[0, 1, 2, 3, 4],\n",
" [5, 6, 7, 8, 9]])\n",
" >>> ixgrid = np.ix_([0, 1], [2, 4])\n",
" >>> ixgrid\n",
" (array([[0],\n",
" [1]]), array([[2, 4]]))\n",
" >>> ixgrid[0].shape, ixgrid[1].shape\n",
" ((2, 1), (1, 2))\n",
" >>> a[ixgrid]\n",
" array([[2, 4],\n",
" [7, 9]])\n",
" \n",
" >>> ixgrid = np.ix_([True, True], [2, 4])\n",
" >>> a[ixgrid]\n",
" array([[2, 4],\n",
" [7, 9]])\n",
" >>> ixgrid = np.ix_([True, True], [False, False, True, False, True])\n",
" >>> a[ixgrid]\n",
" array([[2, 4],\n",
" [7, 9]])\n",
"\n"
]
}
],
"source": [
"help(np.ix_)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 2],\n",
" [ 8, 10]]])"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#a[np.ix_([1,3],[2,5])]`\n",
"arr_2[np.ix_([0],[0,2],[0,2])]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## np.vstack\n"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function vstack in module numpy.core.shape_base:\n",
"\n",
"vstack(tup)\n",
" Stack arrays in sequence vertically (row wise).\n",
" \n",
" This is equivalent to concatenation along the first axis after 1-D arrays\n",
" of shape `(N,)` have been reshaped to `(1,N)`. Rebuilds arrays divided by\n",
" `vsplit`.\n",
" \n",
" This function makes most sense for arrays with up to 3 dimensions. For\n",
" instance, for pixel-data with a height (first axis), width (second axis),\n",
" and r/g/b channels (third axis). The functions `concatenate`, `stack` and\n",
" `block` provide more general stacking and concatenation operations.\n",
" \n",
" Parameters\n",
" ----------\n",
" tup : sequence of ndarrays\n",
" The arrays must have the same shape along all but the first axis.\n",
" 1-D arrays must have the same length.\n",
" \n",
" Returns\n",
" -------\n",
" stacked : ndarray\n",
" The array formed by stacking the given arrays, will be at least 2-D.\n",
" \n",
" See Also\n",
" --------\n",
" stack : Join a sequence of arrays along a new axis.\n",
" hstack : Stack arrays in sequence horizontally (column wise).\n",
" dstack : Stack arrays in sequence depth wise (along third dimension).\n",
" concatenate : Join a sequence of arrays along an existing axis.\n",
" vsplit : Split array into a list of multiple sub-arrays vertically.\n",
" block : Assemble arrays from blocks.\n",
" \n",
" Examples\n",
" --------\n",
" >>> a = np.array([1, 2, 3])\n",
" >>> b = np.array([2, 3, 4])\n",
" >>> np.vstack((a,b))\n",
" array([[1, 2, 3],\n",
" [2, 3, 4]])\n",
" \n",
" >>> a = np.array([[1], [2], [3]])\n",
" >>> b = np.array([[2], [3], [4]])\n",
" >>> np.vstack((a,b))\n",
" array([[1],\n",
" [2],\n",
" [3],\n",
" [2],\n",
" [3],\n",
" [4]])\n",
"\n"
]
}
],
"source": [
"help(np.vstack)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[3 4 6]\n",
" [3 6 2]\n",
" [3 6 1]]\n"
]
}
],
"source": [
"arr_3 = np.array([3,4,6])\n",
"arr_4 = np.array([[3,6,2],\n",
" [3,6,1]])\n",
"arr_34 = np.vstack((arr_3,arr_4))\n",
"print(arr_34)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
" [[12 13 14 15]\n",
" [16 17 18 19]\n",
" [20 21 22 23]]]\n",
"\n",
"[[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
" [[12 13 14 15]\n",
" [16 17 18 19]\n",
" [20 21 22 23]]\n",
"\n",
" [[24 25 26 27]\n",
" [28 29 30 31]\n",
" [32 33 34 35]]]\n",
"\n",
"[[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
" [[12 13 14 15]\n",
" [16 17 18 19]\n",
" [20 21 22 23]]\n",
"\n",
" [[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
" [[12 13 14 15]\n",
" [16 17 18 19]\n",
" [20 21 22 23]]\n",
"\n",
" [[24 25 26 27]\n",
" [28 29 30 31]\n",
" [32 33 34 35]]]\n"
]
}
],
"source": [
"arr_5 = np.arange(24).reshape(2,3,4)\n",
"arr_6 = np.arange(36).reshape(3,3,-1)\n",
"arr_56 = np.vstack((arr_5,arr_6))\n",
"print(arr_5,arr_6,arr_56,sep='\\n\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## np.hstack"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 0 1 2 3]\n",
" [ 4 5 6 7]\n",
" [ 8 9 10 11]]\n",
"\n",
" [[ 8 9 10 11]\n",
" [12 13 14 15]\n",
" [12 13 14 15]\n",
" [16 17 18 19]\n",
" [20 21 22 23]]\n",
"\n",
" [[16 17 18 19]\n",
" [20 21 22 23]\n",
" [24 25 26 27]\n",
" [28 29 30 31]\n",
" [32 33 34 35]]]\n"
]
}
],
"source": [
"arr_7 = arr_5.reshape(3,2,4)\n",
"arr_8 = arr_6\n",
"arr_78 = np.hstack((arr_7,arr_8))\n",
"print(arr_78)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#np.dstack()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## np.concatenate"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on built-in function concatenate in module numpy.core.multiarray:\n",
"\n",
"concatenate(...)\n",
" concatenate((a1, a2, ...), axis=0, out=None)\n",
" \n",
" Join a sequence of arrays along an existing axis.\n",
" \n",
" Parameters\n",
" ----------\n",
" a1, a2, ... : sequence of array_like\n",
" The arrays must have the same shape, except in the dimension\n",
" corresponding to `axis` (the first, by default).\n",
" axis : int, optional\n",
" The axis along which the arrays will be joined. If axis is None,\n",
" arrays are flattened before use. Default is 0.\n",
" out : ndarray, optional\n",
" If provided, the destination to place the result. The shape must be\n",
" correct, matching that of what concatenate would have returned if no\n",
" out argument were specified.\n",
" \n",
" Returns\n",
" -------\n",
" res : ndarray\n",
" The concatenated array.\n",
" \n",
" See Also\n",
" --------\n",
" ma.concatenate : Concatenate function that preserves input masks.\n",
" array_split : Split an array into multiple sub-arrays of equal or\n",
" near-equal size.\n",
" split : Split array into a list of multiple sub-arrays of equal size.\n",
" hsplit : Split array into multiple sub-arrays horizontally (column wise)\n",
" vsplit : Split array into multiple sub-arrays vertically (row wise)\n",
" dsplit : Split array into multiple sub-arrays along the 3rd axis (depth).\n",
" stack : Stack a sequence of arrays along a new axis.\n",
" hstack : Stack arrays in sequence horizontally (column wise)\n",
" vstack : Stack arrays in sequence vertically (row wise)\n",
" dstack : Stack arrays in sequence depth wise (along third dimension)\n",
" \n",
" Notes\n",
" -----\n",
" When one or more of the arrays to be concatenated is a MaskedArray,\n",
" this function will return a MaskedArray object instead of an ndarray,\n",
" but the input masks are *not* preserved. In cases where a MaskedArray\n",
" is expected as input, use the ma.concatenate function from the masked\n",
" array module instead.\n",
" \n",
" Examples\n",
" --------\n",
" >>> a = np.array([[1, 2], [3, 4]])\n",
" >>> b = np.array([[5, 6]])\n",
" >>> np.concatenate((a, b), axis=0)\n",
" array([[1, 2],\n",
" [3, 4],\n",
" [5, 6]])\n",
" >>> np.concatenate((a, b.T), axis=1)\n",
" array([[1, 2, 5],\n",
" [3, 4, 6]])\n",
" >>> np.concatenate((a, b), axis=None)\n",
" array([1, 2, 3, 4, 5, 6])\n",
" \n",
" This function will not preserve masking of MaskedArray inputs.\n",
" \n",
" >>> a = np.ma.arange(3)\n",
" >>> a[1] = np.ma.masked\n",
" >>> b = np.arange(2, 5)\n",
" >>> a\n",
" masked_array(data = [0 -- 2],\n",
" mask = [False True False],\n",
" fill_value = 999999)\n",
" >>> b\n",
" array([2, 3, 4])\n",
" >>> np.concatenate([a, b])\n",
" masked_array(data = [0 1 2 2 3 4],\n",
" mask = False,\n",
" fill_value = 999999)\n",
" >>> np.ma.concatenate([a, b])\n",
" masked_array(data = [0 -- 2 2 3 4],\n",
" mask = [False True False False False False],\n",
" fill_value = 999999)\n",
"\n"
]
}
],
"source": [
"help(np.concatenate)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[5]\n",
" [6]\n",
" [7]]\n",
"\n",
"[[11 12]\n",
" [13 14]\n",
" [15 16]]\n"
]
}
],
"source": [
"arr_9 = np.arange(5,8).reshape(3,1)\n",
"arr_10 = np.arange(11,17).reshape(3,2)\n",
"print(arr_9,arr_10,sep='\\n\\n')"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 5 11 12]\n",
" [ 6 13 14]\n",
" [ 7 15 16]]\n"
]
}
],
"source": [
"arr_910 = np.concatenate((arr_9,arr_10),axis=1)\n",
"print(arr_910)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## broadcasting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. 所有数组都向其中shape最长的数组看齐\n",
"3x3x4\n",
"1x1x4\n",
"2. 输出数组的shape是输入数组shape的各个轴上的最大值\n",
"7x1x8\n",
"1x5x1\n",
"输出:7x5x8\n",
"7x2x8\n",
"1x5x1\n",
"输出:7x5x8\n",
"3. 输入数组的某个轴长度相同或者有1的时候,这个数组才能够用来计算,否则报错\n",
"4. 输入数组的某个轴的长度为1时,沿着此轴运算时都用这个轴上的第一组值\n"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"x = np.arange(4)# 1维矩阵 4元素\n",
"xx = x.reshape(4,1)# 2维矩阵4x1\n",
"y = np.ones(5) #1维矩阵5 元素\n"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (4,) (5,) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;31m# x.shape : 4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;31m# y.shape: 5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,) (5,) "
]
}
],
"source": [
"x+y\n",
"# x.shape : 4\n",
"# y.shape: 5"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1., 1.],\n",
" [2., 2., 2., 2., 2.],\n",
" [3., 3., 3., 3., 3.],\n",
" [4., 4., 4., 4., 4.]])"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# xx.shape: 4x1\n",
" #y.shape : 5\n",
"# y.shape: 1x5\n",
" #(xx+y).shape:4x5\n",
"xx+y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[[1,1,1,1,1]] #1x5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[[0,0,0,0,0],\n",
" [1,1,1,1,1],\n",
" [2,2,2,2,2],\n",
" [3,3,3,3,3]]\n",
"\n",
"y:[[1,1,1,1,1]]\n",
" \n",
"[[1,1,1,1,1],\n",
" [1,1,1,1,1],\n",
" [1,1,1,1,1],\n",
" [1,1,1,1,1]]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史