Skip to content

Instantly share code, notes, and snippets.

@KodairaTomonori
Created June 19, 2017 10:36
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 KodairaTomonori/f15979c9cec4648643b73f5051143a71 to your computer and use it in GitHub Desktop.
Save KodairaTomonori/f15979c9cec4648643b73f5051143a71 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## numpy チュートリアル\n",
"\n",
"Tomonori Kodaira"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# numpy\n",
"## 行列とか扱いやすくするやつ"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ndarray"
]
},
{
"cell_type": "code",
"execution_count": 136,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0 1 2 3]\n",
" [9 8 7 6]]\n"
]
},
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 136,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = numpy.array([[0,1,2,3], [9,8,7,6]]); print(x)\n",
"x.__class__"
]
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"shape, size: (2, 4) 8\n",
"any, all: True False\n",
"sum: 36\n",
"flatten: [0 1 2 3 9 8 7 6]\n",
"Transpose: \n",
"[[0 9]\n",
" [1 8]\n",
" [2 7]\n",
" [3 6]]\n",
"fill: \n",
"[[10 10 10 10]\n",
" [10 10 10 10]]\n"
]
}
],
"source": [
"print(\"shape, size: \", x.shape, x.size)\n",
"print(\"any, all: \", x.any(), x.all())\n",
"print(\"sum: \", x.sum())\n",
"print(\"flatten: \", x.flatten())\n",
"print(\"Transpose: \", x.T, sep='\\n')\n",
"x.fill(10)\n",
"print(\"fill: \", x, sep='\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# よく使うコマンド"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# zeros, arange, ones, random.*, concatenate, stack etc.."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## zeros, ones"
]
},
{
"cell_type": "code",
"execution_count": 232,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
]
},
"execution_count": 232,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.zeros((1, 10))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.ones(10)"
]
},
{
"cell_type": "code",
"execution_count": 233,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0., 0., 0.],\n",
" [ 0., 0., 0.],\n",
" [ 0., 0., 0.],\n",
" [ 0., 0., 0.]],\n",
"\n",
" [[ 0., 0., 0.],\n",
" [ 0., 0., 0.],\n",
" [ 0., 0., 0.],\n",
" [ 0., 0., 0.]]])"
]
},
"execution_count": 233,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.zeros((2, 4, 3))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## random, arange"
]
},
{
"cell_type": "code",
"execution_count": 239,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.21240213, 0.51893698, 0.37301441, 0.52153013, 0.4189147 ,\n",
" 0.50376525, 0.58299756, 0.37359483, 0.88470023, 0.57319329],\n",
" [ 0.22352932, 0.6454468 , 0.04963214, 0.57786814, 0.20154706,\n",
" 0.8007831 , 0.49362569, 0.33227308, 0.32134634, 0.43553474]])"
]
},
"execution_count": 239,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.random.random((2, 10))\n",
"# numpy.random.randint(0, 10, (2, 4))"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 177,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.arange(10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# concat"
]
},
{
"cell_type": "code",
"execution_count": 245,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3 4 5 6 7 8 9]\n",
" [10 11 12 13 14 15 16 17 18 19]]\n"
]
}
],
"source": [
"x = numpy.arange(20).reshape(2, 10); print(x)"
]
},
{
"cell_type": "code",
"execution_count": 222,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3 4 5 6 7 8 9]\n",
" [10 11 12 13 14 15 16 17 18 19]\n",
" [ 0 1 2 3 4 5 6 7 8 9]\n",
" [10 11 12 13 14 15 16 17 18 19]] (4, 10)\n",
"\n",
"[[ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]\n",
" [10 11 12 13 14 15 16 17 18 19 10 11 12 13 14 15 16 17 18 19]] (2, 20)\n"
]
}
],
"source": [
"cx = numpy.concatenate((x, x)); print(cx, cx.shape, end=\"\\n\\n\")\n",
"cx = numpy.concatenate((x, x), axis=1); print(cx, cx.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# stack"
]
},
{
"cell_type": "code",
"execution_count": 252,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0 2 2 3 100 5 6 7 8 9]\n",
" [ 10 11 12 13 14 15 16 17 18 19]]\n",
"\n",
" [[ 0 2 2 3 100 5 6 7 8 9]\n",
" [ 10 11 12 13 14 15 16 17 18 19]]] (2, 2, 10)\n"
]
}
],
"source": [
"sx = numpy.stack((x, x))\n",
"print(sx, sx.shape)"
]
},
{
"cell_type": "code",
"execution_count": 225,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0 1 2 3 4 5 6 7 8 9]\n",
" [ 0 1 2 3 4 5 6 7 8 9]]\n",
"\n",
" [[10 11 12 13 14 15 16 17 18 19]\n",
" [10 11 12 13 14 15 16 17 18 19]]] (2, 2, 10)\n"
]
}
],
"source": [
"sx = numpy.stack((x, x), axis=1); print(sx, sx.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# index\n",
"- カンマ区切りで要素にアクセスできる\n",
"- リストが入れられる."
]
},
{
"cell_type": "code",
"execution_count": 258,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"ename": "ValueError",
"evalue": "cannot reshape array of size 40 into shape (2,2,5)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-258-afe23daab318>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m40\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 40 into shape (2,2,5)"
]
}
],
"source": [
"import numpy \n",
"x = numpy.arange(40).reshape(2, 2, 5)\n",
"print(x[:, 1, [1, 3, 5]])\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# where"
]
},
{
"cell_type": "code",
"execution_count": 266,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x =\n",
" [[[ 0 1 2 3 4 5 6 7 8 9]\n",
" [10 11 12 13 14 15 16 17 18 19]]\n",
"\n",
" [[20 21 22 23 24 25 26 27 28 29]\n",
" [30 31 32 33 34 35 36 37 38 39]]]\n",
"[[[2 2 2 2 2 2 2 2 2 2]\n",
" [2 2 2 2 2 2 2 2 2 2]]\n",
"\n",
" [[1 1 1 1 1 1 1 1 1 1]\n",
" [1 1 1 1 1 1 1 1 1 1]]] (2, 2, 10)\n"
]
},
{
"data": {
"text/plain": [
"array([[[2, 0, 2, 0, 2, 0, 2, 0, 2, 0],\n",
" [2, 0, 2, 0, 2, 0, 2, 0, 2, 0]],\n",
"\n",
" [[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],\n",
" [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]])"
]
},
"execution_count": 266,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"x =\\n\", x)\n",
"# print(\"\\nx % 2 == 0,if True 1 else 0\\n\", numpy.where(x % 2 == 0, 1, 0))\n",
"two = numpy.where(x % 2 == 0)\n",
"# print(\"\\nwhere\", *two, sep='\\n')\n",
"ones = numpy.ones_like(x);ones[0] += 1; zeros = numpy.zeros_like(x)\n",
"print(ones, zeros.shape)\n",
"numpy.where(x % 2 == 0, ones, zeros)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# 線形代数的な"
]
},
{
"cell_type": "code",
"execution_count": 269,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5]\n",
"[0 1 0 0 1]\n"
]
},
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 269,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"xx = numpy.arange(5).reshape(5) + 1; print(xx)\n",
"w = numpy.random.randint(0, 3, (5, )); print(w.flatten())\n",
"w.dot(xx)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 加減乗除\n",
"###### 基本的にどこかの次元と形が合っていれば勝手にやってくれる"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 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]]])"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sample = numpy.arange(24).reshape(2, 3, 4) # shape(2, 3, 4)\n",
"sample"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 3, 6, 9],\n",
" [12, 15, 18, 21],\n",
" [24, 27, 30, 33]],\n",
"\n",
" [[36, 39, 42, 45],\n",
" [48, 51, 54, 57],\n",
" [60, 63, 66, 69]]])"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sample * 3 # shape(, )"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[ 0 4 12 24]\n",
" [ 8 20 36 56]\n",
" [ 16 36 60 88]]\n",
"\n",
" [[ 24 52 84 120]\n",
" [ 32 68 108 152]\n",
" [ 40 84 132 184]]]\n",
"\n",
"[[[ 0 3 6 9]\n",
" [ 8 10 12 14]\n",
" [ 8 9 10 11]]\n",
"\n",
" [[36 39 42 45]\n",
" [32 34 36 38]\n",
" [20 21 22 23]]]\n"
]
}
],
"source": [
"print(sample * [2, 4, 6, 8], end='\\n\\n') # shape (4, )\n",
"print(sample * [[3], [2], [1]]) # shape(3, 1)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 3, 12, 27],\n",
" [ 4, 20, 42, 70],\n",
" [ 16, 45, 80, 121]],\n",
"\n",
" [[ 0, 39, 84, 135],\n",
" [ 16, 68, 126, 190],\n",
" [ 40, 105, 176, 253]]])"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sample * [[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]] # shape (3, 4)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (2,3,4) (2,3) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-92-0539d9cc838c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# sample.shape = 2, 3, 4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0msample\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m7\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# shape (2, 3)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,3,4) (2,3) "
]
}
],
"source": [
"# sample.shape = 2, 3, 4\n",
"sample * numpy.array([[0, 3, 6], [1, 4, 7]]).reshape((2,3)) # shape (2, 3)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# broadcast_to\n",
"### 次元の拡張を行う."
]
},
{
"cell_type": "code",
"execution_count": 273,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 3, 6, 9],\n",
" [ 1, 4, 7, 10],\n",
" [ 2, 5, 8, 11]],\n",
"\n",
" [[ 0, 3, 6, 9],\n",
" [ 1, 4, 7, 10],\n",
" [ 2, 5, 8, 11]]])"
]
},
"execution_count": 273,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp = numpy.array([[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]]) # shape (3, 4)\n",
"numpy.broadcast_to(temp, (2, 3, 4)) \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## broadcast_to関数が扱えるようなshapeに直せばいい"
]
},
{
"cell_type": "code",
"execution_count": 275,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[0]\n",
" [3]\n",
" [6]]\n",
"\n",
" [[1]\n",
" [4]\n",
" [7]]]\n"
]
},
{
"data": {
"text/plain": [
"array([[[ 0, 0, 0, 0],\n",
" [ 12, 15, 18, 21],\n",
" [ 48, 54, 60, 66]],\n",
"\n",
" [[ 12, 13, 14, 15],\n",
" [ 64, 68, 72, 76],\n",
" [140, 147, 154, 161]]])"
]
},
"execution_count": 275,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp = numpy.array([[0, 3, 6], [1, 4, 7]]).reshape((2,3)) # shape (2, 3)\n",
"temp = temp.reshape(2, 3, 1); print(temp)\n",
"temp * sample\n",
"# numpy.broadcast_to(temp, (2, 3, 4))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 各要素に関数を当てる"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {
"collapsed": false,
"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"
]
},
{
"data": {
"text/plain": [
"array([[ 6, 22, 38],\n",
" [54, 70, 86]])"
]
},
"execution_count": 140,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(sample)\n",
"numpy.apply_along_axis(numpy.sum, axis=2, arr=sample)"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 6, 22, 38],\n",
" [54, 70, 86]])"
]
},
"execution_count": 142,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numpy.sum(sample, axis=2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# axis\n"
]
},
{
"cell_type": "code",
"execution_count": 284,
"metadata": {
"collapsed": 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]]] (2, 3, 4)\n",
"-----------------------\n",
"Default 276 ()\n",
"axis=1:\n",
" [[12 15 18 21]\n",
" [48 51 54 57]] (2, 4)\n",
"axis=2:\n",
" [[ 6 22 38]\n",
" [54 70 86]] (2, 3)\n"
]
}
],
"source": [
"print(sample, sample.shape)\n",
"print(\"-----------------------\")\n",
"sums = sample.sum(axis=None); print(\"Default\", sums, sums.shape)\n",
"# sums = sample.sum(axis=0); print(\"axis=0:\\n\", sums, sums.shape)\n",
"sums = sample.sum(axis=1); print(\"axis=1:\\n\", sums, sums.shape)\n",
"sums = sample.sum(axis=2); print(\"axis=2:\\n\", sums, sums.shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"scrolled": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment