diff --git a/core/numpy/numpy-basics.ipynb b/core/numpy/numpy-basics.ipynb index 3281deb5b..db7f96cdf 100644 --- a/core/numpy/numpy-basics.ipynb +++ b/core/numpy/numpy-basics.ipynb @@ -981,6 +981,176 @@ "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\n", + " return None" + ] + }, + { + "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\n", + " return None" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# GIVEN CODE\n", + "import scipy.stats as sps\n", + "\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\n", + " return None" + ] + }, + { + "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": {},