From 3ea5c232ff12f5704e54056131c2a6cb9b4632a9 Mon Sep 17 00:00:00 2001 From: Mirko Birbaumer <mirko.birbaumer@hslu.ch> Date: Fri, 16 Sep 2022 12:48:30 +0000 Subject: [PATCH] added element-wise operations --- .../Preliminaries_Numpy_Pandas.ipynb | 300 +++++++++++++++++- 1 file changed, 299 insertions(+), 1 deletion(-) diff --git a/notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb b/notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb index eb29636..a3a7307 100644 --- a/notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb +++ b/notebooks/Block_0/Examples script/Preliminaries_Numpy_Pandas.ipynb @@ -752,9 +752,307 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 3.4 Element-wise Operations" + "### Changing Shapes\n", + "\n", + "Sometimes you will need to change the shape of your data without actually changing \n", + "its contents. For example, you may have a vector, which is one-dimensional, but need \n", + "a matrix, which is two-dimensional. There are two ways you can do that.\n", + "\n", + "Let's say you have the following vector:" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4,)" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "v = np.array([1,2,3,4])\n", + "v.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Calling `v.shape` would return `(4,)`. But what if you want a $1\\times 4$ matrix? \n", + "You can accomplish that with the `reshape` function, like so:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2, 3, 4]])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = v.reshape(1,4)\n", + "x" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Calling `x.shape` would return `(1,4)`. If you wanted a $4\\times 1$ matrix, you \n", + "could do this:" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1],\n", + " [2],\n", + " [3],\n", + " [4]])" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = v.reshape(4,1)\n", + "x" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The `reshape` function works for more than just adding a dimension of size $1$. Check out its \n", + "documentation for more examples.\n", + "\n", + "One more thing about reshaping NumPy arrays: if you see code from experienced NumPy users, you \n", + "will often see them use a special slicing syntax instead of calling `reshape`. Using this \n", + "syntax, the previous two examples would look like this:" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2, 3, 4]])" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = v[None, :]\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 1)" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1],\n", + " [2],\n", + " [3],\n", + " [4]])" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = v[:, None]\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4, 1)" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x.shape" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Those lines create a slice that looks at all of the items of `v` but asks NumPy to add a new dimension \n", + "of size $1$ for the associated axis. It may look strange to you now, but it's a common technique so \n", + "it's good to be aware of it. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Element-wise Operations\n", + "\n", + "#### The Python Way\n", + "\n", + "Suppose you had a list of numbers, and you wanted to add $5$ to every item in the list. \n", + "Without NumPy, you might do something like this:" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[6, 7, 8, 9, 10]" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "values = [1,2,3,4,5]\n", + "for i in range(len(values)):\n", + " values[i] += 5\n", + " \n", + "values" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "That makes sense, but it's a lot of code to write and it runs slowly because \n", + "it's pure Python.\n", + "\n", + "__Note:__ Just in case you aren't used to using operators like `+=`, that just \n", + "means _add these two items and then store the result in the left item._ It is a more \n", + "succinct way of writing `values[i] = values[i] + 5`. The code you see in these examples \n", + "makes use of such operators whenever possible." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### The NumPy Way\n", + "\n", + "In NumPy, we could do the following:\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [], + "source": [ + "values = [1,2,3,4,5]\n", + "values = np.array(values) + 5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Creating that array may seem odd, but normally you'll be storing your data in `ndarrays` \n", + "anyway. So if you already had an `ndarray` named `values`, you could have just done:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": 4, -- GitLab