Skip to content

Instantly share code, notes, and snippets.

@EthanRosenthal
Last active March 27, 2019 00:41
Show Gist options
  • Save EthanRosenthal/7956c25cddb9482327bddbe091d4fdb6 to your computer and use it in GitHub Desktop.
Save EthanRosenthal/7956c25cddb9482327bddbe091d4fdb6 to your computer and use it in GitHub Desktop.
TIL: ellipsis indexing
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TIL: [ellipsis indexing](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x.shape = (2, 3, 3)\n"
]
}
],
"source": [
"x = np.array([\n",
" [[ 0., 1., 2.],\n",
" [ 3., 4., 5.],\n",
" [ 6., 7., 8.]],\n",
" [[ 0., 2., 4.],\n",
" [ 6., 8., 10.],\n",
" [12., 14., 16.]]\n",
"])\n",
"print(f'x.shape = {x.shape}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## These are equivalent"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 1., 2.],\n",
" [3., 4., 5.],\n",
" [6., 7., 8.]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[0]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 1., 2.],\n",
" [3., 4., 5.],\n",
" [6., 7., 8.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[0, :, :]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 1., 2.],\n",
" [3., 4., 5.],\n",
" [6., 7., 8.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[0, ...]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Broadcasting changes things after the first index"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 1., 2.],\n",
" [0., 2., 4.]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:, 0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 3., 6.],\n",
" [ 0., 6., 12.]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[..., 0]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 3., 6.],\n",
" [ 0., 6., 12.]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:, :, 0]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 1., 2.],\n",
" [0., 2., 4.]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:, 0, :]"
]
}
],
"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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment