Skip to content

Commit

Permalink
update nditer tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
learnp committed Dec 2, 2017
1 parent 41acf45 commit 312309e
Showing 1 changed file with 172 additions and 22 deletions.
194 changes: 172 additions & 22 deletions numpy/nditer.ipynb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"color:green\" align='center'>Numpy tutorial: iterate numpy array using nditer</h1>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 27,
"metadata": {
"collapsed": true
},
Expand All @@ -13,8 +20,10 @@
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"execution_count": 28,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
Expand All @@ -24,7 +33,7 @@
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 26,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -38,12 +47,94 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 style=\"color:purple\">C style ordering</h2>"
"<h3 style=\"color:purple\">Using normal for loop iteration</h3>"
]
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 29,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n"
]
}
],
"source": [
"for row in a:\n",
" for cell in row:\n",
" print(cell)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 style=\"color:purple\">For loop with flatten</h3>"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n"
]
}
],
"source": [
"for cell in a.flatten():\n",
" print(cell)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 style=\"color:blue\" align=\"center\">nditer</h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 style=\"color:purple\">C style ordering</h3>"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"scrolled": true
},
Expand Down Expand Up @@ -81,9 +172,9 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 32,
"metadata": {
"scrolled": true
"scrolled": false
},
"outputs": [
{
Expand Down Expand Up @@ -114,37 +205,67 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 style=\"color:purple\">Modify array values while iterating</h2>"
"<h3 style=\"color:purple\">external_loop</h3>"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 4 8]\n",
"[1 5 9]\n",
"[ 2 6 10]\n",
"[ 3 7 11]\n"
]
}
],
"source": [
"for x in np.nditer(a, flags=['external_loop'],order='F'):\n",
" print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 style=\"color:purple\">Modify array values while iterating</h2>"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"for x in np.nditer(a, op_flags=['readwrite']):\n",
" x[...] = x + 1"
" x[...] = x * x"
]
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 35,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8],\n",
" [ 9, 10, 11, 12]])"
"array([[ 0, 1, 4, 9],\n",
" [ 16, 25, 36, 49],\n",
" [ 64, 81, 100, 121]])"
]
},
"execution_count": 34,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -162,26 +283,55 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
"array([[ 3],\n",
" [ 7],\n",
" [11]])"
]
},
"execution_count": 35,
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.arange(12).reshape(3,4)\n",
"b = np.arange(3, 15, 4).reshape(3,1)\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 3\n",
"1 3\n",
"4 3\n",
"9 3\n",
"16 7\n",
"25 7\n",
"36 7\n",
"49 7\n",
"64 11\n",
"81 11\n",
"100 11\n",
"121 11\n"
]
}
],
"source": [
"for x, y in np.nditer([a,b]):\n",
" print (x,y)"
]
}
],
"metadata": {
Expand All @@ -200,7 +350,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
"version": "3.6.3"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 312309e

Please sign in to comment.