Created
June 9, 2021 16:59
-
-
Save mkolod/1cda6d25cc7506933c66aedfa2077094 to your computer and use it in GitHub Desktop.
Parallel Reservoir Sampling Equivalent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Reservoir sampling is not normally parallel,\n", | |
"# but RNG generation, sorting and gather can be parallelized.\n", | |
"# This is for demonstrating the capability only\n", | |
"# See more here: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Sorting\n", | |
"def sim_parallel_shuffle(data):\n", | |
" rands = np.random.rand(data.size)\n", | |
" indices = np.argsort(rands)\n", | |
" return data[indices]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[0 1 2 3 4 5 6 7 8 9]\n", | |
"[5 7 4 1 6 9 8 3 2 0]\n" | |
] | |
} | |
], | |
"source": [ | |
"data = np.arange(10)\n", | |
"print(data)\n", | |
"shuffled = sim_parallel_shuffle(data)\n", | |
"print(shuffled)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"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.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment