Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy tasks #462

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions core/numpy/numpy-basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"<details>\n",
" <summary>Answer</summary>\n",
"\n",
"```python\n",
"def np_diag_2k(a):\n",
" diag = a.diagonal()\n",
" return np.sum(diag[diag % 2 == 0])\n",
"```\n",
"</details>"
]
},
{
"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": [
"<details>\n",
" <summary>Answer</summary>\n",
"\n",
"```python\n",
"# ANSWER\n",
"def np_sec_av(A):\n",
" return sum(A)/len(A)\n",
"```\n",
"</details>"
]
},
{
"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": [
"<details>\n",
" <summary>Answer</summary>\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",
"</details>"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading