From bffe82cb2a65da6dd8c7981850719880b21cb268 Mon Sep 17 00:00:00 2001 From: Sergei Nabatov Date: Sat, 20 Apr 2024 16:35:11 +0200 Subject: [PATCH 1/3] numpy tasks --- core/numpy/numpy-basics.ipynb | 166 ++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) diff --git a/core/numpy/numpy-basics.ipynb b/core/numpy/numpy-basics.ipynb index 3281deb5b..493eb29ae 100644 --- a/core/numpy/numpy-basics.ipynb +++ b/core/numpy/numpy-basics.ipynb @@ -981,6 +981,172 @@ "to investigate every preceding dimension along our the last entry of our last axis, the same as `c[:, :, -1]`." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Numpy exercises\n", + "This block exists to add more practical exercises for the students. The exercises are not required but they can be very helpful for undestanding the subject. \n", + "\n", + "### Q1\n", + "Write a function that finds the sum of even diagonal elements of a square matrix. If there are no such elements, then print 0." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def np_diag_2k(a):\n", + " # YOUR CODE" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# GIVEN CODE\n", + "a = np.random.randint(1, 10, size=(10, 10))\n", + "a" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "np_diag_2k(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + " Answer\n", + "\n", + "```python\n", + "def np_diag_2k(a):\n", + " diag = a.diagonal()\n", + " return np.sum(diag[diag % 2 == 0])\n", + "```\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Q2\n", + "\n", + "Write a function that, using a given sequence $\\{A_i\\}_{i=1}^n$, builds a sequence $S_n$, where $S_k = \\frac{A_1+ ... + A_k}{k}$." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# ANSWER\n", + "def np_sec_av(A):\n", + " # YOUR CODE" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# GIVEN CODE\n", + "import scipy.stats as sps\n", + "A = sps.uniform.rvs(size=10**3)\n", + "\n", + "np_sec_av(A)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + " Answer\n", + "\n", + "```python\n", + "# ANSWER\n", + "def np_sec_av(A):\n", + " return sum(A)/len(A)\n", + "```\n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Q3\n", + "\n", + "A two-dimensional array $X$ is specified. For each row of the array X, the following transformation must be performed.\n", + "\n", + "Let the line x be given. It is necessary to build a new array, where all elements with odd indexes must be replaced with the number a (default value a=1). All elements with even indexes must be cubed. Then write down the elements in reverse order relative to their positions. At the end, you need to merge the array x with the transformed x and output it.\n", + "\n", + "Write a function that performs this transformation for each row of a two-dimensional array X. Array X should remain unchanged at the same time.\n", + "\n", + "Use the numpy library.\n", + "\n", + "Example:\n", + "$X = [[100,200,300,400,500]]$ -> $[[100, a,300,a,500]]$ -> $[[500^3, a,300^3,a,100^3]]$ -> glue -> $[[100,200,300,400,500,500^3,a,300^3,a,100^3]]$" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# ANSWER\n", + "from copy import copy\n", + "\n", + "def transform(X, a=1):\n", + " # YOUR CODE" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# GIVEN CODE\n", + "X = np.array([[100,200,300,400,500, 600], [200, 300, 500, 22, 11, 17]])\n", + "\n", + "S2 = transform(X)\n", + "print(S2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + " Answer\n", + "\n", + "```python\n", + "# ANSWER\n", + "def transform(X, a=1):\n", + " Y = np.copy(X)\n", + " Y[:,1::2] = a\n", + " Y[:,0::2] **= 3\n", + " return np.hstack((X, Y[:,::-1]))\n", + "```\n", + "
" + ] + }, { "cell_type": "markdown", "metadata": {}, From 1b1e62b59d94901c4a06b24a3b9ba186719da1f6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Apr 2024 14:35:39 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- core/numpy/numpy-basics.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/numpy/numpy-basics.ipynb b/core/numpy/numpy-basics.ipynb index 493eb29ae..5908e4f32 100644 --- a/core/numpy/numpy-basics.ipynb +++ b/core/numpy/numpy-basics.ipynb @@ -1065,6 +1065,7 @@ "source": [ "# GIVEN CODE\n", "import scipy.stats as sps\n", + "\n", "A = sps.uniform.rvs(size=10**3)\n", "\n", "np_sec_av(A)" @@ -1123,7 +1124,7 @@ "outputs": [], "source": [ "# GIVEN CODE\n", - "X = np.array([[100,200,300,400,500, 600], [200, 300, 500, 22, 11, 17]])\n", + "X = np.array([[100, 200, 300, 400, 500, 600], [200, 300, 500, 22, 11, 17]])\n", "\n", "S2 = transform(X)\n", "print(S2)" From 551dd930db4622f2d050bd48d4b050e2159d76b1 Mon Sep 17 00:00:00 2001 From: Sergei Nabatov Date: Sat, 20 Apr 2024 16:44:01 +0200 Subject: [PATCH 3/3] fixed for the pipeline to pass --- core/numpy/numpy-basics.ipynb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/numpy/numpy-basics.ipynb b/core/numpy/numpy-basics.ipynb index 493eb29ae..49ef32f3f 100644 --- a/core/numpy/numpy-basics.ipynb +++ b/core/numpy/numpy-basics.ipynb @@ -999,7 +999,8 @@ "outputs": [], "source": [ "def np_diag_2k(a):\n", - " # YOUR CODE" + " # YOUR CODE\n", + " return None" ] }, { @@ -1054,7 +1055,8 @@ "source": [ "# ANSWER\n", "def np_sec_av(A):\n", - " # YOUR CODE" + " # YOUR CODE\n", + " return None" ] }, { @@ -1113,7 +1115,8 @@ "from copy import copy\n", "\n", "def transform(X, a=1):\n", - " # YOUR CODE" + " # YOUR CODE\n", + " return None" ] }, {