{ "metadata": { "name": "", "signature": "sha256:92fd351c440d231ede1e12fd30aef73b1ff27a92476bf44cd933a7f12d1a30dd" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Indexing And Slicing Numpy Arrays\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Slicing Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Explanation Of Broadcasting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unlike many other data types, slicing an array into a new variable means that any changes to that new variable are broadcasted to the original variable. Put other way, a slice is a hotlink to the original array variable, not a seperate and independent copy of it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import Modules" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array of battle casualties from the first to the last battle" ] }, { "cell_type": "code", "collapsed": false, "input": [ "battleDeaths = np.array([1245, 2732, 3853, 4824, 5292, 6184, 7282, 81393, 932, 10834])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Divide the array of battle deaths into start, middle, and end of the war" ] }, { "cell_type": "code", "collapsed": false, "input": [ "warStart = battleDeaths[0:3]; print('Death from battles at the start of war:', warStart)\n", "warMiddle = battleDeaths[3:7]; print('Death from battles at the middle of war:', warMiddle)\n", "warEnd = battleDeaths[7:10]; print('Death from battles at the end of war:', warEnd)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Death from battles at the start of war: [1245 2732 3853]\n", "Death from battles at the middle of war: [4824 5292 6184 7282]\n", "Death from battles at the end of war: [81393 932 10834]\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Change the battle death numbers from the first battle" ] }, { "cell_type": "code", "collapsed": false, "input": [ "warStart[0] = 11101" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View that change reflected in the warStart slice of the battleDeaths array" ] }, { "cell_type": "code", "collapsed": false, "input": [ "warStart" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 21, "text": [ "array([11101, 2732, 3853])" ] } ], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View that change reflected in (i.e. \"broadcasted to) the original battleDeaths array" ] }, { "cell_type": "code", "collapsed": false, "input": [ "battleDeaths" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "array([11101, 2732, 3853, 4824, 5292, 6184, 7282, 81393, 932, 10834])" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indexing Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create an array of regiment information\n", "\n", "Note: This multidimensional array behaves like a dataframe or matrix (i.e. columns and rows)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "regimentNames = ['Nighthawks', 'Sky Warriors', 'Rough Riders', 'New Birds']\n", "regimentNumber = [1, 2, 3, 4]\n", "regimentSize = [1092, 2039, 3011, 4099]\n", "regimentCommander = ['Mitchell', 'Blackthorn', 'Baker', 'Miller']\n", "\n", "regiments = np.array([regimentNames, regimentNumber, regimentSize, regimentCommander])\n", "regiments" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 67, "text": [ "array([['Nighthawks', 'Sky Warriors', 'Rough Riders', 'New Birds'],\n", " ['1', '2', '3', '4'],\n", " ['1092', '2039', '3011', '4099'],\n", " ['Mitchell', 'Blackthorn', 'Baker', 'Miller']], \n", " dtype='