Skip to content

Instantly share code, notes, and snippets.

@Cartman0
Created April 16, 2016 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cartman0/bc062d1a162589e40a02c21e0ada7776 to your computer and use it in GitHub Desktop.
Save Cartman0/bc062d1a162589e40a02c21e0ada7776 to your computer and use it in GitHub Desktop.
Numpy Tutorial メモ2(ShapeManipulation)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"toc": "true"
},
"cell_type": "markdown",
"source": "# Table of Contents\n <p><div class=\"lev1\"><a href=\"#NumPy-Tutorial-2-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>NumPy Tutorial 2</a></div><div class=\"lev2\"><a href=\"#Shape-Manipulation-(shape-の操作)-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Shape Manipulation (shape の操作)</a></div><div class=\"lev3\"><a href=\"#Changing-the-shape-of-an-array-(配列のshapeを変更)-1.1.1\"><span class=\"toc-item-num\">1.1.1&nbsp;&nbsp;</span>Changing the shape of an array (配列のshapeを変更)</a></div><div class=\"lev3\"><a href=\"#Stacking-together-different-arrays(異なる配列を一緒にスタック)-1.1.2\"><span class=\"toc-item-num\">1.1.2&nbsp;&nbsp;</span>Stacking together different arrays(異なる配列を一緒にスタック)</a></div><div class=\"lev4\"><a href=\"#Note-1.1.2.1\"><span class=\"toc-item-num\">1.1.2.1&nbsp;&nbsp;</span>Note</a></div><div class=\"lev3\"><a href=\"#Splitting-one-array-into-several-smaller-ones-(1つの配列をより小さな複数の配列に分割)-1.1.3\"><span class=\"toc-item-num\">1.1.3&nbsp;&nbsp;</span>Splitting one array into several smaller ones (1つの配列をより小さな複数の配列に分割)</a></div><div class=\"lev2\"><a href=\"#参考リンク-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>参考リンク</a></div>"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# NumPy Tutorial 2"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [QuickStartTutorial](https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)\n- [私訳「暫定的 NumPy チュートリアル」](http://naoyat.hatenablog.jp/entry/2011/12/29/021414)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Shape Manipulation (shape の操作)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Changing the shape of an array (配列のshapeを変更)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "An array has a shape given by the number of elements along each axis\n(配列は、各軸に沿った要素の数によって与えられた shape をもつ):"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "import numpy as np\na = np.floor(10 * np.random.random((3, 4)))\na",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 6., 9., 3.],\n [ 8., 3., 3., 2.],\n [ 4., 4., 2., 9.]])"
},
"metadata": {},
"execution_count": 1
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a.shape",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "(3, 4)"
},
"metadata": {},
"execution_count": 2
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The shape of an array can be changed with various commands\n配列のshape は、様々なコマンドで変更できる:"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a.ravel() # flatten the array (配列を解きほぐして(ravel)平らにする)",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0., 6., 9., 3., 8., 3., 3., 2., 4., 4., 2., 9.])"
},
"metadata": {},
"execution_count": 3
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a.shape = (6, 2) # 6×2行列\na",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 6.],\n [ 9., 3.],\n [ 8., 3.],\n [ 3., 2.],\n [ 4., 4.],\n [ 2., 9.]])"
},
"metadata": {},
"execution_count": 4
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a.T # 転置行列 2 ×6",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 9., 8., 3., 4., 2.],\n [ 6., 3., 3., 2., 4., 9.]])"
},
"metadata": {},
"execution_count": 5
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The order of the elements in the array resulting from `ravel()` is normally “C-style”, that is, the rightmost index “changes the fastest”, \nso the element after `a[0, 0]` is `a[0, 1]`. \n(`ravel()` からの結果として得られる配列中の要素の順番は、普通に \"C-style\"、\n即ち、一番右のindex が「最初に変わる」ので、\n`a[0, 0]` の次に来る要素は `a[0, 1]` である。)\n\nIf the array is reshaped to some other shape, again the array is treated as “C-style”. \n(もし、その配列が、他のshape にreshape されても、配列は再び \"C-style\" として扱われる。)\n\nNumpy normally creates arrays stored in this order,\nso `ravel()` will usually not need to copy its argument, \nbut if the array was made by taking slices of another array or created with unusual options, \nit may need to be copied. \n(Numpyは、普通`ravel()`が引数(順序)をコピーしなくてもいいように、この順序で格納して配列を作成するが、\nもし、その配列が他の配列をスライスして作られたものであったり、\n一般的でないオプションで作られたものである場合、\nコピーが必要になるかもしれない。)\n\nThe functions `ravel()` and `reshape()` can also be instructed, using an optional argument, to use FORTRAN-style arrays, \nin which the leftmost index changes the fastest.\n(関数 `ravel()`, `reshape()` は、オプション引数を使うことで、\nFORTRAN形式の配列を使うこともできる。\nその場合、一番左の添字が最初に変わる。)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The `reshape` function returns its argument with a modified shape, \nwhereas the `ndarray.resize` method modifies the array itself\n(`reshape`関数は、その引数を修正されたshape (新しい配列)で返す\n一方で、`ndarray.resize`メソッドは配列そのものを修正する):"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 6.],\n [ 9., 3.],\n [ 8., 3.],\n [ 3., 2.],\n [ 4., 4.],\n [ 2., 9.]])"
},
"metadata": {},
"execution_count": 6
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a.resize((2, 6))\na",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 6., 9., 3., 8., 3.],\n [ 3., 2., 4., 4., 2., 9.]])"
},
"metadata": {},
"execution_count": 7
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "If a dimension is given as `-1` in a reshaping operation, \nthe other dimensions are automatically calculated \n(もし、次元が、reshape操作において、-1で与えられた場合、\n他の次元は自動的に計算される):"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a.reshape(3, -1)",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 6., 9., 3.],\n [ 8., 3., 3., 2.],\n [ 4., 4., 2., 9.]])"
},
"metadata": {},
"execution_count": 8
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "See also:\n\n- [ndarray.shape](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.ndarray.shape.html#numpy.ndarray.shape)\n- [reshape](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.reshape.html#numpy.reshape) \n- [resize](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.resize.html#numpy.resize)\n- [ravel](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.ravel.html#numpy.ravel)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Stacking together different arrays(異なる配列を一緒にスタック)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Several arrays can be stacked together along different axes\n(それぞれの配列は、異なる軸に沿って一緒にスタックされる):"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a = np.floor(10 * np.random.random((2, 2)))\na",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 4.],\n [ 1., 3.]])"
},
"metadata": {},
"execution_count": 9
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "b = np.floor(10 * np.random.random((2, 2)))\nb",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 2., 9.],\n [ 1., 1.]])"
},
"metadata": {},
"execution_count": 10
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.vstack((a,b)) # vertical stack",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 4.],\n [ 1., 3.],\n [ 2., 9.],\n [ 1., 1.]])"
},
"metadata": {},
"execution_count": 11
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.hstack((a,b)) # horizontal stack",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 4., 2., 9.],\n [ 1., 3., 1., 1.]])"
},
"metadata": {},
"execution_count": 12
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The function [`column_stack`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.column_stack.html#numpy.column_stack) stacks 1D arrays as columns into a 2D array.(`column_stack`関数は1次元配列を列として積み上げて2次元配列を作る。) \n\nIt is equivalent to [`vstack`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.vstack.html#numpy.vstack) (hstackの間違い?)only for 1D arrays(1次元配列の場合に限れば、これは`vstack` と等価である):"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.column_stack((a,b)) # With 2D arrays",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 4., 2., 9.],\n [ 1., 3., 1., 1.]])"
},
"metadata": {},
"execution_count": 13
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a = np.array([4., 1.])\nb = np.array([2., 8.])\na",
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([ 4., 1.])"
},
"metadata": {},
"execution_count": 14
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a[:, np.newaxis]# This allows to have a 2D columns vector (2Dの列ベクトルをもつ)",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 4.],\n [ 1.]])"
},
"metadata": {},
"execution_count": 15
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.column_stack((a, b))",
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 4., 2.],\n [ 1., 8.]])"
},
"metadata": {},
"execution_count": 16
}
]
},
{
"metadata": {
"trusted": true,
"scrolled": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.column_stack((a[:, np.newaxis], b[:, np.newaxis]))",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 4., 2.],\n [ 1., 8.]])"
},
"metadata": {},
"execution_count": 17
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.vstack((a, b))",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 4., 1.],\n [ 2., 8.]])"
},
"metadata": {},
"execution_count": 18
}
]
},
{
"metadata": {
"trusted": true,
"scrolled": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.vstack((a[:, np.newaxis], b[:, np.newaxis])) # The behavior of vstack is different",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 4.],\n [ 1.],\n [ 2.],\n [ 8.]])"
},
"metadata": {},
"execution_count": 19
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.hstack((a[:, np.newaxis], b[:, np.newaxis]))",
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 4., 2.],\n [ 1., 8.]])"
},
"metadata": {},
"execution_count": 20
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "For arrays of with more than two dimensions, `hstack` stacks along their second axes, \n`vstack` stacks along their first axes, \nand `concatenate` allows for an optional arguments giving the number of the axis along which the concatenation should happen.\n(\n- 2次元を超える配列では、`hstack` は2番目の軸(第1軸)に沿ってスタックし、\n- `vstack` は1番目の軸(第0軸)に沿ってスタックし、\n- `concatenate` はどの軸に沿って結合が起こるべきかを示す数をオプション引数に与えることができる。)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### Note"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "In complex cases, `r_` and `c_` are useful for creating arrays by stacking numbers along one axis. \n(複雑なケースにおいては、`r_` と `c_` が、ある軸に沿って配列を作る際に 便利。\n\nThey allow the use of range literals `(”:”)`\n(これらは、範囲を表すリテラル `(\":\")` を使うことができる) :"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.r_[1:4, 0, 4] # 行ベクトル(配列)",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([1, 2, 3, 0, 4])"
},
"metadata": {},
"execution_count": 21
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "b = np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])\nb",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "(array([[1, 2, 3]]), 0, 0, array([[4, 5, 6]]))"
},
"metadata": {},
"execution_count": 22
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.c_[b] # Translates slice objects to concatenation along the second axis.",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 2, 3, 0, 0, 4, 5, 6]])"
},
"metadata": {},
"execution_count": 23
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "See also\n\n- [stack](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.stack.html#numpy.stack)\nJoin a sequence of arrays along a new axis."
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a = np.array([1, 2, 3])\nb = np.array([4, 5, 6])\nnp.stack((a, b))",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 2, 3],\n [4, 5, 6]])"
},
"metadata": {},
"execution_count": 24
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.stack((a, b), axis=0)",
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 2, 3],\n [4, 5, 6]])"
},
"metadata": {},
"execution_count": 25
}
]
},
{
"metadata": {
"trusted": true,
"scrolled": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.stack((a, b), axis=1)",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 4],\n [2, 5],\n [3, 6]])"
},
"metadata": {},
"execution_count": 26
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [vstack](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.vstack.html#numpy.vstack)\nStack arrays in sequence vertically (row wise)."
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [dstack](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.dstack.html#numpy.dstack)\nStack arrays in sequence depth wise (along third axis)."
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a = np.array((1, 2, 3))\nb = np.array((4, 5, 6))\nnp.dstack((a, b)).shape # third軸目に積むので2 ",
"execution_count": 27,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "(1, 3, 2)"
},
"metadata": {},
"execution_count": 27
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.dstack((a, b))",
"execution_count": 28,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[[1, 4],\n [2, 5],\n [3, 6]]])"
},
"metadata": {},
"execution_count": 28
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [concatenate](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.concatenate.html#numpy.concatenate)\nJoin a sequence of arrays along an existing axis."
},
{
"metadata": {
"trusted": true,
"collapsed": true
},
"cell_type": "code",
"source": "a = np.array([\n [1, 2], \n [3, 4]\n ])\nb = np.array([\n [5, 6]\n ])",
"execution_count": 29,
"outputs": []
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a.shape",
"execution_count": 30,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "(2, 2)"
},
"metadata": {},
"execution_count": 30
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "b.shape",
"execution_count": 31,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "(1, 2)"
},
"metadata": {},
"execution_count": 31
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.concatenate((a, b), axis=0)",
"execution_count": 32,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 2],\n [3, 4],\n [5, 6]])"
},
"metadata": {},
"execution_count": 32
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.concatenate((a, b), axis=1) # bが1*2行列なので積めない",
"execution_count": 33,
"outputs": [
{
"output_type": "error",
"ename": "ValueError",
"evalue": "all the input array dimensions except for the concatenation axis must match exactly",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-33-7d73420ac2d6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconcatenate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# bが1*2行列なので積めない\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mValueError\u001b[0m: all the input array dimensions except for the concatenation axis must match exactly"
]
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.concatenate((a, b.T), axis=1)",
"execution_count": 34,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[1, 2, 5],\n [3, 4, 6]])"
},
"metadata": {},
"execution_count": 34
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [`c_`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.c_.html#numpy.c_) Translates slice objects to concatenation along the second axis.\n- [`r_`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.r_.html#numpy.r_) "
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Splitting one array into several smaller ones (1つの配列をより小さな複数の配列に分割)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Using [`hsplit`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.hsplit.html#numpy.hsplit), \nyou can split an array along its horizontal axis, \neither by specifying the number of equally shaped arrays to return, \nor by specifying the columns after which the division should occur\n(`hsplit` を用いれば、同じshape の配列をいくつ返すか、\nあるいはどの列で分割が起こるべきかを指定して、配列を水平軸沿いに分割できる:):"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "a = np.floor(10 * np.random.random((2, 12)))\na",
"execution_count": 35,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 6., 7., 0., 5., 0., 3., 7., 6., 9., 6., 0., 8.],\n [ 1., 8., 8., 4., 8., 5., 5., 2., 2., 3., 3., 8.]])"
},
"metadata": {},
"execution_count": 35
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.hsplit(a, 3) # Split a into 3, 2×4 3つ",
"execution_count": 36,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "[array([[ 6., 7., 0., 5.],\n [ 1., 8., 8., 4.]]), array([[ 0., 3., 7., 6.],\n [ 8., 5., 5., 2.]]), array([[ 9., 6., 0., 8.],\n [ 2., 3., 3., 8.]])]"
},
"metadata": {},
"execution_count": 36
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.hsplit(a, (3, 4)) # Split a after the third and the fourth column 3列目と4列目に分割",
"execution_count": 37,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "[array([[ 6., 7., 0.],\n [ 1., 8., 8.]]), array([[ 5.],\n [ 4.]]), array([[ 0., 3., 7., 6., 9., 6., 0., 8.],\n [ 8., 5., 5., 2., 2., 3., 3., 8.]])]"
},
"metadata": {},
"execution_count": 37
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "[`vsplit`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.vsplit.html#numpy.vsplit) splits along the vertical axis, \n(`vsplit` は垂直軸沿いに分割し、"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "x = np.arange(16.).reshape(4, 4)\nx",
"execution_count": 38,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([[ 0., 1., 2., 3.],\n [ 4., 5., 6., 7.],\n [ 8., 9., 10., 11.],\n [ 12., 13., 14., 15.]])"
},
"metadata": {},
"execution_count": 38
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.vsplit(x, 2)",
"execution_count": 39,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "[array([[ 0., 1., 2., 3.],\n [ 4., 5., 6., 7.]]), array([[ 8., 9., 10., 11.],\n [ 12., 13., 14., 15.]])]"
},
"metadata": {},
"execution_count": 39
}
]
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.vsplit(x, (3, 6))",
"execution_count": 40,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "[array([[ 0., 1., 2., 3.],\n [ 4., 5., 6., 7.],\n [ 8., 9., 10., 11.]]),\n array([[ 12., 13., 14., 15.]]),\n array([], shape=(0, 4), dtype=float64)]"
},
"metadata": {},
"execution_count": 40
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "and [`array_split`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.array_split.html#numpy.array_split) allows one to specify along which axis to split.\n([`array_split`](https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.array_split.html#numpy.array_split) は、どの軸沿いに分割するかを指定できる。)"
},
{
"metadata": {
"trusted": true,
"collapsed": false
},
"cell_type": "code",
"source": "x = np.arange(8.)\nx",
"execution_count": 41,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "array([ 0., 1., 2., 3., 4., 5., 6., 7.])"
},
"metadata": {},
"execution_count": 41
}
]
},
{
"metadata": {
"trusted": true,
"scrolled": true,
"collapsed": false
},
"cell_type": "code",
"source": "np.array_split(x, 3)",
"execution_count": 42,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "[array([ 0., 1., 2.]), array([ 3., 4., 5.]), array([ 6., 7.])]"
},
"metadata": {},
"execution_count": 42
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## 参考リンク"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [QuickStartTutorial](https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)\n- [私訳「暫定的 NumPy チュートリアル」](http://naoyat.hatenablog.jp/entry/2011/12/29/021414)"
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"nbconvert_exporter": "python",
"file_extension": ".py",
"mimetype": "text/x-python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"version": "3.5.1",
"name": "python",
"pygments_lexer": "ipython3"
},
"toc": {
"toc_window_display": false,
"toc_threshold": "6",
"toc_cell": true,
"toc_number_sections": true
},
"gist": {
"id": "",
"data": {
"description": "Numpy Tutorial メモ2(ShapeManipulation)",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment