Skip to content

Commit

Permalink
post lesson slides upload
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoGiampieri committed Mar 15, 2019
1 parent 90ded5f commit f215e60
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 36 deletions.
230 changes: 219 additions & 11 deletions Lesson_06_Vectorization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 73,
"metadata": {
"nbpresent": {
"id": "7238ab2b-ae7b-4dea-bb79-df6b613f330d"
Expand All @@ -970,7 +970,7 @@
"[0 1 2 3 4 5 6 7 8 9]\n",
"[1 3 5 7 9]\n",
"[ 0. 2.5 5. 7.5 10. ]\n",
"[ 1. 3.16227766 10. 31.6227766 100. ]\n",
"[ 1. 3.46410162 12. 41.56921938 144. ]\n",
"[ 1. 3.16227766 10. 31.6227766 100. ]\n"
]
}
Expand All @@ -982,7 +982,7 @@
"print(a)\n",
"a = np.linspace(0, 10, 5) # (initial value, final value, number of steps)\n",
"print(a)\n",
"a = np.logspace(0, 2, 5) # (initial value (in log), final value (in log), number of steps)\n",
"a = np.logspace(0, 2, 5, base=12) # (initial value (in log), final value (in log), number of steps)\n",
"print(a)\n",
"a = np.geomspace(1, 100, 5) # (initial value, final value, number of steps)\n",
"print(a)"
Expand Down Expand Up @@ -1119,7 +1119,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 76,
"metadata": {
"nbpresent": {
"id": "183db7a5-f898-4085-a130-2ac7465a1bf0"
Expand All @@ -1141,6 +1141,16 @@
"[12 13 14 15]\n",
"[16 17 18 19]\n"
]
},
{
"data": {
"text/plain": [
"array([1., 1.])"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
Expand All @@ -1153,7 +1163,10 @@
"print(a[8:12])\n",
"print(a[12:-4])\n",
"print(a[-4:])\n",
"assert( np.all(a[-15:-10] == a[5:10])) "
"assert( np.all(a[-15:-10] == a[5:10]))\n",
"\n",
"a = np.ones((4, 4))\n",
"a[0, :2]"
]
},
{
Expand Down Expand Up @@ -1261,6 +1274,7 @@
"source": [
"arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n",
"print (\" a = \", arr3d)\n",
"print (\"first element : \", arr3d[0, 0, 0])\n",
"print (\"first element : \", arr3d[0][0][0])\n",
"print (\"first row : \", arr3d[0][0])\n",
"print (\"first 'matrix' : \", arr3d[0])\n",
Expand Down Expand Up @@ -1532,6 +1546,132 @@
"print(a[a>0]) # CONDITIONAL SELECTION"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Broadcasting\n",
"\n",
"Numpy can combine arrays of different sizes in a smart way, as long as they have a similar number of dimensions (or this can be padded out with sizes 1)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4,)\n",
"(4, 1)\n"
]
}
],
"source": [
"a = np.arange(4)\n",
"print(a.shape)\n",
"a = a.reshape(4, 1)\n",
"print(a.shape)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 2, 4, 6])"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# I can normally sum these two arrays\n",
"a = np.arange(4)\n",
"b = np.arange(4)\n",
"\n",
"a+b"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[2, 3, 4, 5],\n",
" [3, 4, 5, 6],\n",
" [4, 5, 6, 7],\n",
" [5, 6, 7, 8]])"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# If I reshape them with compatible shapes, I can still add them, but it broadcast over the two dimensions\n",
"a1 = a.reshape(4, 1)\n",
"b1 = b.reshape(1, 4)\n",
"\n",
"a1+b1"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (2,2) (1,4) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-85-9f255dee9d27>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mb1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0ma1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mb1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,2) (1,4) "
]
}
],
"source": [
"# if the shapes are not compatible then it raises error\n",
"a1 = a.reshape(2, 2)\n",
"b1 = b.reshape(1, 4)\n",
"\n",
"a1+b1"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -1611,13 +1751,81 @@
"print(matrix)\n",
"print('-'*30)\n",
"print(\"normalized matrix (by column):\")\n",
"matrix/=matrix.sum(axis=0)\n",
"matrix = matrix / matrix.sum(axis=0)\n",
"print(matrix)\n",
"print('-'*30)\n",
"print(\"sum over the columns:\")\n",
"print(matrix.sum(axis=0))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Normally these operation reduce the number of dimensions of the array.\n",
"\n",
"One can also apply one of these reduction operations while keeping the same number of dimensions as the previous array, using the `keepdims` option"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4,)\n",
"(1, 4)\n"
]
}
],
"source": [
"a = np.random.rand(4, 4)\n",
"print(a.sum(axis=0).shape)\n",
"print(a.sum(axis=0, keepdims=True).shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Memory recycling\n",
"\n",
"one option to optimize performance of memory use is to use an array memory space to store the result of an operation, without requiring the creation of partial results."
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0. 2. 4. 6.]\n"
]
}
],
"source": [
"a = np.arange(4)\n",
"b = np.arange(4)\n",
"c = np.empty(4)\n",
"\n",
"np.add(a, b, out=c)\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand All @@ -1644,7 +1852,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 77,
"metadata": {
"slideshow": {
"slide_type": "slide"
Expand All @@ -1664,7 +1872,7 @@
"(496, 595, 3)"
]
},
"execution_count": 31,
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1682,7 +1890,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 80,
"metadata": {
"slideshow": {
"slide_type": "slide"
Expand All @@ -1692,10 +1900,10 @@
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x7f8b43fd30b8>"
"<matplotlib.image.AxesImage at 0x7f8b43bc3b70>"
]
},
"execution_count": 41,
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
},
Expand Down
Loading

0 comments on commit f215e60

Please sign in to comment.