Skip to content

Instantly share code, notes, and snippets.

@Cartman0
Last active April 21, 2016 13:52
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/e9f50b0f0dbc28f40938b11088ac211d to your computer and use it in GitHub Desktop.
Save Cartman0/e9f50b0f0dbc28f40938b11088ac211d to your computer and use it in GitHub Desktop.
NumPy Tutorial メモ4(Less Basic)
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-4-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>NumPy Tutorial 4</a></div><div class=\"lev2\"><a href=\"#Less-Basic(中級編)-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>Less Basic(中級編)</a></div><div class=\"lev3\"><a href=\"#Broadcasting-rules(Broadcasting-ルール)-1.1.1\"><span class=\"toc-item-num\">1.1.1&nbsp;&nbsp;</span>Broadcasting rules(Broadcasting ルール)</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 4"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "- [NumPy Tutorial メモ1(Basic)](http://nbviewer.jupyter.org/gist/Cartman0/9fa48b89664dc08ef82a55877767b5a0)\n- [NumPy Tutorial メモ2(ShapeManipulation)](http://nbviewer.jupyter.org/gist/Cartman0/bc062d1a162589e40a02c21e0ada7776)\n- [NumPy Tutorial メモ3(Copies and Views)](http://nbviewer.jupyter.org/gist/Cartman0/cd39c1bfc88f5edccd8f4523fbd33b91)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Less Basic(中級編)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "### Broadcasting rules(Broadcasting ルール)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Broadcasting allows `universal functions` to deal in a meaningful way with inputs that do not have exactly the same shape.\n(Broadcasting は、ユニバーサル関数が、正確に同一のshape を持つわけではない入力を意味のある形で扱えるようにしてくれる。)\n\nThe first rule of broadcasting is that if all input arrays do not have the same number of dimensions, \na “1” will be repeatedly prepended to the shapes of the smaller arrays until all the arrays have the same number of dimensions.\n(broadcasting の第1のルールは、もし全ての入力配列が同じ次元数でないなら、\nより小さい配列のshape に対し次元数が同じになるまで先頭に \"1\" を繰り返し追加するというもの。)\n\nThe second rule of broadcasting ensures that arrays with a size of 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. \n(broadcastingの第2のルールは、特定の次元に沿ったサイズが1の配列が、その次元において最大のshape をもつ配列と同じサイズを持っているかのように振る舞うことを保証するというもの。)\n\nThe value of the array element is assumed to be the same along that dimension for the “broadcast” array.\n(配列の要素の値は、\"broadcast\"配列にとって、その次元に沿って同じであると仮定される。)\n\nAfter application of the broadcasting rules, the sizes of all arrays must match. More details can be found in [Broadcasting](https://docs.scipy.org/doc/numpy-dev/user/basics.broadcasting.html).\n(broadcastingルールを適用した後、全ての配列のサイズが、合致していなければならない。詳細は、[Broadcasting](https://docs.scipy.org/doc/numpy-dev/user/basics.broadcasting.html)に記載されている。)"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "An example of broadcasting in practice:\n\n[Broadcasting example](https://docs.scipy.org/doc/numpy-dev/user/basics.broadcasting.html)"
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "import numpy as np\nx = np.arange(4)\nx",
"execution_count": 1,
"outputs": [
{
"execution_count": 1,
"data": {
"text/plain": "array([0, 1, 2, 3])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "xx = x.reshape(4, 1)\nxx",
"execution_count": 2,
"outputs": [
{
"execution_count": 2,
"data": {
"text/plain": "array([[0],\n [1],\n [2],\n [3]])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "y = np.ones(5)\ny",
"execution_count": 3,
"outputs": [
{
"execution_count": 3,
"data": {
"text/plain": "array([ 1., 1., 1., 1., 1.])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "z = np.ones((3, 4))\nz",
"execution_count": 4,
"outputs": [
{
"execution_count": 4,
"data": {
"text/plain": "array([[ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.],\n [ 1., 1., 1., 1.]])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "x.shape",
"execution_count": 5,
"outputs": [
{
"execution_count": 5,
"data": {
"text/plain": "(4,)"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "y.shape",
"execution_count": 6,
"outputs": [
{
"execution_count": 6,
"data": {
"text/plain": "(5,)"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "x + y # この足し算はbroadcastされない",
"execution_count": 7,
"outputs": [
{
"evalue": "operands could not be broadcast together with shapes (4,) (5,) ",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-7-584745f9c707>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0my\u001b[0m \u001b[1;31m# この足し算はbroadcastされない\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,) (5,) "
],
"ename": "ValueError"
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "xx.shape",
"execution_count": 8,
"outputs": [
{
"execution_count": 8,
"data": {
"text/plain": "(4, 1)"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "y.shape",
"execution_count": 9,
"outputs": [
{
"execution_count": 9,
"data": {
"text/plain": "(5,)"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "(xx + y).shape",
"execution_count": 10,
"outputs": [
{
"execution_count": 10,
"data": {
"text/plain": "(4, 5)"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false,
"scrolled": true
},
"cell_type": "code",
"source": "xx + y # 4 × 5になる",
"execution_count": 11,
"outputs": [
{
"execution_count": 11,
"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.]])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "x.shape",
"execution_count": 12,
"outputs": [
{
"execution_count": 12,
"data": {
"text/plain": "(4,)"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "z.shape",
"execution_count": 13,
"outputs": [
{
"execution_count": 13,
"data": {
"text/plain": "(3, 4)"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "(x + z).shape",
"execution_count": 14,
"outputs": [
{
"execution_count": 14,
"data": {
"text/plain": "(3, 4)"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "x + z",
"execution_count": 15,
"outputs": [
{
"execution_count": 15,
"data": {
"text/plain": "array([[ 1., 2., 3., 4.],\n [ 1., 2., 3., 4.],\n [ 1., 2., 3., 4.]])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Broadcasting provides a convenient way of taking the outer product (or any other outer operation) of two arrays. \n(Broadcasting は、2つの配列の外積()を取る便利な方法を提供する)\n\nThe following example shows an outer addition operation of two 1-d arrays:"
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "a = np.array([0.0, 10.0, 20.0, 30.0])\na",
"execution_count": 16,
"outputs": [
{
"execution_count": 16,
"data": {
"text/plain": "array([ 0., 10., 20., 30.])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "b = np.array([1.0, 2.0, 3.0]) # × 3 \nb",
"execution_count": 17,
"outputs": [
{
"execution_count": 17,
"data": {
"text/plain": "array([ 1., 2., 3.])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": " a[:, np.newaxis] # 4 × 1 ",
"execution_count": 18,
"outputs": [
{
"execution_count": 18,
"data": {
"text/plain": "array([[ 0.],\n [ 10.],\n [ 20.],\n [ 30.]])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": false,
"trusted": false
},
"cell_type": "code",
"source": "a[:, np.newaxis] + b # 4 × 3になる",
"execution_count": 19,
"outputs": [
{
"execution_count": 19,
"data": {
"text/plain": "array([[ 1., 2., 3.],\n [ 11., 12., 13.],\n [ 21., 22., 23.],\n [ 31., 32., 33.]])"
},
"output_type": "execute_result",
"metadata": {}
}
]
},
{
"metadata": {
"collapsed": true
},
"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"
},
"toc": {
"toc_number_sections": true,
"toc_window_display": false,
"toc_threshold": "6",
"toc_cell": true
},
"gist": {
"id": "e9f50b0f0dbc28f40938b11088ac211d",
"data": {
"description": "NumPy Tutorial メモ4(Less Basic)",
"public": true
}
},
"hide_input": false,
"language_info": {
"pygments_lexer": "ipython3",
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"codemirror_mode": {
"version": 3,
"name": "ipython"
},
"version": "3.5.1",
"name": "python",
"file_extension": ".py"
},
"_draft": {
"nbviewer_url": "https://gist.github.com/e9f50b0f0dbc28f40938b11088ac211d"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment