2024-heraklion-data/exercises/numpy_vectorize/numpy_vectorize.ipynb

118 lines
2.1 KiB
Plaintext
Raw Normal View History

2024-08-27 14:27:53 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "84886528",
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "b8b62e7c",
"metadata": {},
"source": [
"Vectorize this code"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30cd956c",
"metadata": {},
"outputs": [],
"source": [
"size = 101\n",
"center = size // 2\n",
"radius = 20\n",
"\n",
"circle = np.zeros((size, size), dtype=float)\n",
"for i in range(size):\n",
" for j in range(size):\n",
" distance_from_center = np.sqrt((i - center)**2 + (j - center)**2)\n",
" if distance_from_center <= radius:\n",
" circle[i, j] = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "698050fb",
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(circle)"
]
},
{
"cell_type": "markdown",
"id": "a6ac0138",
"metadata": {},
"source": [
"# Solution"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d89a3e0d",
"metadata": {},
"outputs": [],
"source": [
"# type your solution here"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea311dfc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c62096b1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1469dfa",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}