Skip to content

Instantly share code, notes, and snippets.

@nicktimko
Created March 24, 2015 16:25
Show Gist options
  • Save nicktimko/15511c07bf494b510205 to your computer and use it in GitHub Desktop.
Save nicktimko/15511c07bf494b510205 to your computer and use it in GitHub Desktop.
Brief Numpy intro
{
"metadata": {
"name": "",
"signature": "sha256:8a42cba00ac130c14461b6a63e14a5c1effb8f57ca3d24838d4bed68762294e9"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numpy - Numeric Python\n",
"\n",
"When dealing with large amounts of numbers, we can use specialized packages like Numpy to make writing and executing code much faster. Typically, if we have a bunch of numbers we will want to perform the same operation or mathematical function on large swaths of data. While we can do this in standard Python, of course, Numpy is able to ***vectorize*** the operations, running them hundreds or thousands of times faster.\n",
"\n",
"Much of Numpy is implemented to match MATLAB: if you know how use MATLAB, check out [Numpy for MATLAB users](http://wiki.scipy.org/NumPy_for_Matlab_Users) for how to translate your knowledge. `ones`, `size` and `linspace` are all here.\n",
"\n",
"## Easier to Write\n",
"In mathematics, equations are commonly written where it's implicit that they will be applied to all your data: there aren't `for` loops.\n",
"\n",
"Matrix operations (which operate on 'tables' of data and can perform unique calculations at every coordinate) are also commonly written as though they're just multiplication and addition. If I want to double the value of everything in a matrix $\\mathbf{A}$, I simply write $2 \\times \\mathbf{A}$ and that would do (as an example):\n",
"\n",
"$$2 \\times \\begin{bmatrix}1 & 2\\\\3 & 4\\end{bmatrix} = \\begin{bmatrix}2 & 4\\\\6 & 8\\end{bmatrix}$$\n",
"\n",
"## Faster to Run\n",
"\n",
"Modern microprocessors are highly tuned to repeatably perform the same mathematical operation on large amounts of data. Think of watching a HD YouTube video: your computer needs to expand a data stream on the order of megabits per second (Mbps) to a raw video 1000x larger: several gigabits per second.\n",
"\n",
"Ever since the Intel Pentium with [MMX](https://en.wikipedia.org/wiki/MMX_%28instruction_set%29), debuting in 1997, processors have had special instructions (methods of operating) that work on multiple data with a single command. These [single-instruction, multiple-data (SIMD)](https://en.wikipedia.org/wiki/SIMD) instructions have been continually expanded ever since to accomodate the types of math (frequently video processing or encryption) that we expect our devices to do.\n",
"\n",
"![](https://upload.wikimedia.org/wikipedia/en/d/d5/PentiumMMX-presslogo.jpg)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"odds = np.array([1, 3, 5, 7, 9, 11, 13, 15])\n",
"# evens = ?"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Standard Python\n",
"evens = []\n",
"for x in odds:\n",
" evens.append(x + 1)\n",
"\n",
"other_evens = [2 * x for x in odds]\n",
" \n",
"print(evens)\n",
"print(other_evens)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[2, 4, 6, 8, 10, 12, 14, 16]\n",
"[2, 6, 10, 14, 18, 22, 26, 30]\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Numpy\n",
"evens = odds + 1\n",
"other_evens = 2 * odds\n",
"\n",
"print(evens)\n",
"print(other_evens)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 2 4 6 8 10 12 14 16]\n",
"[ 2 6 10 14 18 22 26 30]\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment