Skip to content

Instantly share code, notes, and snippets.

@hitvoice
Last active January 9, 2023 08:49
Show Gist options
  • Save hitvoice/22fbc6ebb80a8a14a51d51ce54265531 to your computer and use it in GitHub Desktop.
Save hitvoice/22fbc6ebb80a8a14a51d51ce54265531 to your computer and use it in GitHub Desktop.
Compare sample efficiency
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "b2f95d28",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "60e5cda8",
"metadata": {},
"outputs": [],
"source": [
"s = pd.Series(np.random.randn(100000))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b5d1ab2e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.53 ms ± 26.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"np.random.choice(s, 1000, replace=False)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "f9683c24",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.54 ms ± 20.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"s.sample(1000).values"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "7fd08ce7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.46 µs ± 115 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"np.random.choice(s)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "73df188a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.52 ms ± 13.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"s.sample(1).item"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50577784",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment