{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Random Sampling" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-03-26T14:35:59.058093Z", "iopub.status.busy": "2024-03-26T14:35:59.058003Z", "iopub.status.idle": "2024-03-26T14:35:59.380841Z", "shell.execute_reply": "2024-03-26T14:35:59.380389Z" } }, "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-03-26T14:35:59.393131Z", "iopub.status.busy": "2024-03-26T14:35:59.393010Z", "iopub.status.idle": "2024-03-26T14:35:59.395079Z", "shell.execute_reply": "2024-03-26T14:35:59.394890Z" } }, "outputs": [], "source": [ "def data(n):\n", " np.random.seed(123)\n", " cov=[[1, -.8], \n", " [-.8, 1]]\n", " x, y = np.random.multivariate_normal(mean=[0,0], cov=cov, size=n).T\n", " return dict(x=x, y=y)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-03-26T14:35:59.396197Z", "iopub.status.busy": "2024-03-26T14:35:59.396033Z", "iopub.status.idle": "2024-03-26T14:35:59.760205Z", "shell.execute_reply": "2024-03-26T14:35:59.759693Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# N exceeds default point sampling threshold (50,000)\n", "N=75000\n", "ggplot(data(N), aes('x', 'y')) + geom_point()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-03-26T14:35:59.772080Z", "iopub.status.busy": "2024-03-26T14:35:59.771946Z", "iopub.status.idle": "2024-03-26T14:35:59.902374Z", "shell.execute_reply": "2024-03-26T14:35:59.902164Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Set 'random' sample size 500\n", "ggplot(data(N), aes('x', 'y')) + geom_point(sampling=sampling_random(500))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-03-26T14:35:59.903695Z", "iopub.status.busy": "2024-03-26T14:35:59.903584Z", "iopub.status.idle": "2024-03-26T14:36:00.238478Z", "shell.execute_reply": "2024-03-26T14:36:00.238249Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use wrong sampling ('group_random' will do nothing in this context)\n", "# It will override the default 'random' sampling and effectively disable proper sampling in this plot.\n", "# However, if N will grow above 'safety' threshold, 'safety' sampling will be applied (see next plot). \n", "ggplot(data(N), aes('x', 'y')) + geom_point(sampling=sampling_group_random(1))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-03-26T14:36:00.248311Z", "iopub.status.busy": "2024-03-26T14:36:00.248198Z", "iopub.status.idle": "2024-03-26T14:36:01.189365Z", "shell.execute_reply": "2024-03-26T14:36:01.189106Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# N exceeds 'safety' threshold (200,000) - 'safety' sampling kicks-in.\n", "ggplot(data(200001), aes('x', 'y')) + geom_point(sampling=sampling_group_random(1))" ] } ], "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 }