Skip to content

Instantly share code, notes, and snippets.

@AllanLRH
Last active March 21, 2017 16:16
Show Gist options
  • Save AllanLRH/06c3d78ebd5448095dd65d763a517b02 to your computer and use it in GitHub Desktop.
Save AllanLRH/06c3d78ebd5448095dd65d763a517b02 to your computer and use it in GitHub Desktop.
Numpy broadcasting
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"from IPython.display import display as disp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Leg med broadcasting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Definer to arrays, `a` og `b`, begge 1d, deres 2d-kopier, `aa` og `bb` og 3d kopien af `a`, kaldet `aaa`."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3,)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"a = np.array([1, 2, 3])\n",
"disp(a, a.shape)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 4, 6])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3,)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"b = np.array([2, 4, 6])\n",
"disp(b, b.shape)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1, 3)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"aa = a[np.newaxis, :]\n",
"disp(aa, aa.shape)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[2],\n",
" [4],\n",
" [6]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3, 1)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"bb = b.reshape(-1, 1)\n",
"disp(bb, bb.shape)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[1]],\n",
"\n",
" [[2]],\n",
"\n",
" [[3]]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3, 1, 1)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"aaa = a.reshape(-1, 1, 1) \n",
"disp(aaa, aaa.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Se hvordan deres form påvirker vektoriserede operationer"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 8, 18])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3,)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"ab = a*b\n",
"disp(ab, ab.shape)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, 8, 18]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1, 3)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"aab = aa*b\n",
"disp(aab, aab.shape)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, 4, 6],\n",
" [ 4, 8, 12],\n",
" [ 6, 12, 18]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3, 3)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"abb = a*bb\n",
"disp(abb, abb.shape)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 2, 4, 6]],\n",
"\n",
" [[ 4, 8, 12]],\n",
"\n",
" [[ 6, 12, 18]]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3, 1, 3)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"aaab = aaa*b\n",
"disp(aaab, aaab.shape)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 2],\n",
" [ 4],\n",
" [ 6]],\n",
"\n",
" [[ 4],\n",
" [ 8],\n",
" [12]],\n",
"\n",
" [[ 6],\n",
" [12],\n",
" [18]]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3, 3, 1)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"aaabb = aaa*bb\n",
"disp(aaabb, aaabb.shape)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 2, 4, 6],\n",
" [ 4, 8, 12],\n",
" [ 6, 12, 18]]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(1, 3, 3)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"aaaTbb = aaa.T*bb\n",
"disp(aaaTbb, aaaTbb.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Der er forresten flere måder at ændre et arrays antal dimmensioner på…"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.expand_dims(a, axis=0) "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape(1, -1) # -1 betyder \"resten\", se evt. eksempel i bunden"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1],\n",
" [2],\n",
" [3]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape(-1, 1) "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(12,)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"foo = np.arange(12)\n",
"disp(foo, foo.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Reshape til 3 rækker, og brug resten af elementerne (-1) til at danne det nødvendige antal søjler."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [ 4, 5, 6, 7],\n",
" [ 8, 9, 10, 11]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(3, 4)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fooRshp = foo.reshape(3, -1)\n",
"disp(fooRshp, fooRshp.shape)"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda env:py35]",
"language": "python",
"name": "conda-env-py35-py"
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment