Commit 528d73a1 authored by Raphael Bacher's avatar Raphael Bacher
Browse files

add broadcast

parent 97dd052b
......@@ -74,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
......@@ -1141,14 +1141,216 @@
"\n",
"For more info, see the related FAQ entry: https://www.scipy.org/scipylib/faq.html#why-both-numpy-linalg-and-scipy-linalg-what-s-the-difference."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Broadcasting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Broadcasting is the name given to the method that NumPy uses to allow array arithmetic between arrays with a different shape or size.\n",
"\n",
"Although the technique was developed for NumPy, it has also been adopted more broadly in other numerical computational libraries, such as Theano, TensorFlow, and Octave.\n",
"\n",
"Broadcasting solves the problem of arithmetic between arrays of differing shapes by in effect replicating the smaller array along the last mismatched dimension.\n",
"\n",
" The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes.\n",
"\n",
"— Broadcasting, SciPy.org\n",
"\n",
"See : https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"2\n",
"[3 4 5]\n"
]
}
],
"source": [
"# scalar and one-dimensional\n",
"a = np.array([1, 2, 3])\n",
"print(a)\n",
"b = 2\n",
"print(b)\n",
"c = a + b\n",
"print(c)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [1 2 3]]\n",
"2\n",
"[[3 4 5]\n",
" [3 4 5]]\n"
]
}
],
"source": [
"# scalar and two-dimensional\n",
"A = np.array([[1, 2, 3], [1, 2, 3]])\n",
"print(A)\n",
"b = 2\n",
"print(b)\n",
"C = A + b\n",
"print(C)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [1 2 3]]\n",
"[1 2 3]\n",
"[[2 4 6]\n",
" [2 4 6]]\n"
]
}
],
"source": [
"# one-dimensional and two-dimensional\n",
"A = np.array([[1, 2, 3], [1, 2, 3]])\n",
"print(A)\n",
"b = np.array([1, 2, 3])\n",
"print(b)\n",
"C = A + b\n",
"print(C)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Limitations of Broadcasting\n",
"\n",
"Broadcasting is a handy shortcut that proves very useful in practice when working with NumPy arrays.\n",
"\n",
"That being said, it does not work for all cases, and in fact imposes a strict rule that must be satisfied for broadcasting to be performed.\n",
"\n",
"Arithmetic, including broadcasting, can only be performed when the shape of each dimension in the arrays are equal or one has the dimension size of 1. The dimensions are considered in reverse order, starting with the trailing dimension; for example, looking at columns before rows in a two-dimensional case."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Vectorization\n",
"\n",
"- The vectorize function : https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html\n",
"- The art of rewriting problems"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 4, 1, 2])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Vetorize a function\n",
"def myfunc(a, b):\n",
" \"Return a-b if a>b, otherwise return a+b\"\n",
" if a > b:\n",
" return a - b\n",
" else:\n",
" return a + b\n",
"\n",
"vfunc = np.vectorize(myfunc)\n",
"vfunc([1, 2, 3, 4], 2)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 95 µs, sys: 0 ns, total: 95 µs\n",
"Wall time: 78.7 µs\n",
"CPU times: user 619 µs, sys: 0 ns, total: 619 µs\n",
"Wall time: 608 µs\n"
]
}
],
"source": [
"# Vectorize a problem\n",
"\n",
"def dist_loop(X,Y):\n",
" \"\"\"\n",
" Naive loop\n",
" \"\"\"\n",
" res=0\n",
" for x,y in zip(X,Y):\n",
" res += (x-y)**2\n",
" \n",
" return np.sqrt(res)\n",
"\n",
" \n",
"def dist(x,y): \n",
" return np.sqrt(np.sum((x-y)**2))\n",
"\n",
"a = np.random.random(1000)\n",
"b = np.random.random(1000)\n",
"%time dist_a_b = dist(a,b)\n",
"%time dist_a_b_loop = dist_loop(a,b)\n",
"assert np.isclose(dist_a_b, dist_a_b_loop)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Diaporama",
"kernelspec": {
"display_name": "Python 3",
"display_name": "jnb-rise",
"language": "python",
"name": "python3"
"name": "jnb-rise"
},
"language_info": {
"codemirror_mode": {
......@@ -1160,7 +1362,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.7.3"
}
},
"nbformat": 4,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment