Simple modifications to power function #1
1 changed files with 81 additions and 50 deletions
131
exercise.ipynb
131
exercise.ipynb
|
@ -2,15 +2,8 @@
|
||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"execution": {
|
|
||||||
"iopub.execute_input": "2024-03-04T09:40:28.904Z",
|
|
||||||
"iopub.status.busy": "2024-03-04T09:40:28.896Z",
|
|
||||||
"iopub.status.idle": "2024-03-04T09:40:28.978Z",
|
|
||||||
"shell.execute_reply": "2024-03-04T09:40:28.967Z"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import numpy as np"
|
"import numpy as np"
|
||||||
|
@ -18,15 +11,8 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"execution": {
|
|
||||||
"iopub.execute_input": "2024-03-04T10:02:39.062Z",
|
|
||||||
"iopub.status.busy": "2024-03-04T10:02:39.057Z",
|
|
||||||
"iopub.status.idle": "2024-03-04T10:02:39.068Z",
|
|
||||||
"shell.execute_reply": "2024-03-04T10:02:39.071Z"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"n_series = 32\n",
|
"n_series = 32\n",
|
||||||
|
@ -37,17 +23,20 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"execution": {
|
"outputs": [
|
||||||
"iopub.execute_input": "2024-03-04T10:02:41.027Z",
|
{
|
||||||
"iopub.status.busy": "2024-03-04T10:02:41.020Z",
|
"name": "stdout",
|
||||||
"iopub.status.idle": "2024-03-04T10:02:41.036Z",
|
"output_type": "stream",
|
||||||
"shell.execute_reply": "2024-03-04T10:02:41.040Z"
|
"text": [
|
||||||
},
|
"Size of one time series: 40 M\n",
|
||||||
"scrolled": true
|
"Size of collection: 1280 M\n",
|
||||||
},
|
"Gap size: 128 K\n",
|
||||||
"outputs": [],
|
"Gapped series size: 2 K\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"print(f'Size of one time series: {int(time_series[0].nbytes/2**20)} M')\n",
|
"print(f'Size of one time series: {int(time_series[0].nbytes/2**20)} M')\n",
|
||||||
"print(f'Size of collection: {int(time_series.nbytes/2**20)} M')\n",
|
"print(f'Size of collection: {int(time_series.nbytes/2**20)} M')\n",
|
||||||
|
@ -80,15 +69,8 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"execution": {
|
|
||||||
"iopub.execute_input": "2024-03-04T10:06:08.461Z",
|
|
||||||
"iopub.status.busy": "2024-03-04T10:06:08.459Z",
|
|
||||||
"iopub.status.idle": "2024-03-04T10:06:08.466Z",
|
|
||||||
"shell.execute_reply": "2024-03-04T10:06:08.468Z"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# compute an approximation of a power series for a collection of gapped timeseries\n",
|
"# compute an approximation of a power series for a collection of gapped timeseries\n",
|
||||||
|
@ -100,6 +82,32 @@
|
||||||
" "
|
" "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"P = np.zeros_like(time_series)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"5.53 s ± 1.3 s per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%timeit power(time_series, P, gap)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -132,7 +140,20 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 12,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def power_improved(time_series, P, gap):\n",
|
||||||
|
" for pwr in range(30):\n",
|
||||||
|
" for row_idx, row_values in enumerate(time_series):\n",
|
||||||
|
" P[row_idx] += (row_values[::gap]**pwr).sum()\n",
|
||||||
|
" return P"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 13,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
|
@ -163,21 +184,31 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 14,
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"execution": {
|
"outputs": [
|
||||||
"iopub.execute_input": "2024-03-04T10:06:20.056Z",
|
{
|
||||||
"iopub.status.busy": "2024-03-04T10:06:20.053Z",
|
"name": "stdout",
|
||||||
"iopub.status.idle": "2024-03-04T10:06:21.695Z",
|
"output_type": "stream",
|
||||||
"shell.execute_reply": "2024-03-04T10:06:21.700Z"
|
"text": [
|
||||||
|
"17.8 ms ± 1.56 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
],
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"P = np.zeros(n_series, dtype='float64')\n",
|
"P = np.zeros(n_series, dtype='float64')\n",
|
||||||
"%timeit power_improved(time_series, P, gap)"
|
"%timeit power_improved(time_series, P, gap)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Learnings:\n",
|
||||||
|
"- just changing the order of the loops: 5.5 s -> 24.2 ms\n",
|
||||||
|
"- also enumerating and accessing the rows: 17.8 ms"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
@ -202,12 +233,12 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.13.5"
|
"version": "3.13.6"
|
||||||
},
|
},
|
||||||
"nteract": {
|
"nteract": {
|
||||||
"version": "0.28.0"
|
"version": "0.28.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 2
|
"nbformat_minor": 4
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue