{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Numpy tutorial: iterate numpy array using nditer

" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(12).reshape(3,4)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Using normal for loop iteration

" ] }, { "cell_type": "code", "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": [ "

For loop with flatten

" ] }, { "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": [ "

nditer

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

C style ordering

" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "scrolled": true }, "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 x in np.nditer(a, order='C'):\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Fortan style ordering

" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "4\n", "8\n", "1\n", "5\n", "9\n", "2\n", "6\n", "10\n", "3\n", "7\n", "11\n" ] } ], "source": [ "for x in np.nditer(a, order='F'):\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

external_loop

" ] }, { "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": [ "

Modify array values while iterating

" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for x in np.nditer(a, op_flags=['readwrite']):\n", " x[...] = x * x" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 4, 9],\n", " [ 16, 25, 36, 49],\n", " [ 64, 81, 100, 121]])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Iterate two broadcastable arrays concurrently

" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 3],\n", " [ 7],\n", " [11]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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": { "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }