Skip to content

Instantly share code, notes, and snippets.

@cgranade
Created June 17, 2016 01:08
Show Gist options
  • Save cgranade/550b3d77f9544cfde321f93580e6148e to your computer and use it in GitHub Desktop.
Save cgranade/550b3d77f9544cfde321f93580e6148e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from __future__ import print_function, division\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's make an example array to work with."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.arange(12).reshape((3, 4))\n",
"arr"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3, 4)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Slicing the array using ``:`` and an array on the second axis picks out the elements of the second axis listed in that array."
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 3],\n",
" [ 4, 7],\n",
" [ 8, 11]])"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sl_arr = arr[:, [0, 3]]\n",
"sl_arr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This changes the shape as well; since we picked two elements out from the second axis, the shape goes "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(3, 2)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sl_arr.shape"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 11])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fancy_sl_arr = arr[[0, 2], [1, 3]]\n",
"fancy_sl_arr"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(2,)"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fancy_sl_arr.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When indexing along multiple axes by arrays, those axes are removed and are replaced by the axes of the arrays used to index. Put differently, the example above is equivalent to (but much faster than) the following for-loop:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"arr[(0, 1)] → fancy_sl_arr[0]\n",
"arr[(2, 3)] → fancy_sl_arr[1]\n"
]
}
],
"source": [
"indexes = ([0, 2], [1, 3])\n",
"fancy_sl_arr = np.empty(np.shape(indexes[0]))\n",
"for new_index, old_index in enumerate(zip(*indexes)):\n",
" print(\"arr[{}] → fancy_sl_arr[{}]\".format(old_index, new_index))\n",
" fancy_sl_arr[new_index] = arr[old_index]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1., 11.])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fancy_sl_arr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment