{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Systematic Sampling" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:07.360138Z", "iopub.status.busy": "2024-04-17T07:35:07.360020Z", "iopub.status.idle": "2024-04-17T07:35:07.677935Z", "shell.execute_reply": "2024-04-17T07:35:07.677643Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:07.691885Z", "iopub.status.busy": "2024-04-17T07:35:07.691717Z", "iopub.status.idle": "2024-04-17T07:35:07.693874Z", "shell.execute_reply": "2024-04-17T07:35:07.693679Z" } }, "outputs": [], "source": [ "N = 10000\n", "X = np.arange(N)\n", "np.random.seed(1)\n", "Y = np.random.normal(0, 1, N)\n", "p = ggplot(dict(x=X, y=Y), aes('x', 'y'))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:07.694874Z", "iopub.status.busy": "2024-04-17T07:35:07.694798Z", "iopub.status.idle": "2024-04-17T07:35:07.760137Z", "shell.execute_reply": "2024-04-17T07:35:07.759926Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Linear regression is computed using the entire data \n", "# therefore it is same on all plots regardless the sampling applied to 'lines' layer.\n", "p + geom_line() + geom_smooth()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:07.762083Z", "iopub.status.busy": "2024-04-17T07:35:07.762005Z", "iopub.status.idle": "2024-04-17T07:35:07.779723Z", "shell.execute_reply": "2024-04-17T07:35:07.779539Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Systematic sampling often looks better than 'random' sampling\n", "p + geom_line(sampling=sampling_systematic(50)) + geom_smooth()\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:35:07.780910Z", "iopub.status.busy": "2024-04-17T07:35:07.780788Z", "iopub.status.idle": "2024-04-17T07:35:07.798339Z", "shell.execute_reply": "2024-04-17T07:35:07.798140Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_line(sampling=sampling_random(50, seed=33)) + geom_smooth()" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 1 }