Skip to content

Instantly share code, notes, and snippets.

@ajfriend
Created April 2, 2023 20:31
Show Gist options
  • Save ajfriend/6d8d1f112d99f1bd10753c3e5c408ff0 to your computer and use it in GitHub Desktop.
Save ajfriend/6d8d1f112d99f1bd10753c3e5c408ff0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "1334afcd-52fe-420d-8fc3-5e0d044881ab",
"metadata": {},
"source": [
"We might like to think that normalizing a vector by its sum would be idempotent.\n",
"But due to floating point arithmetic, this isn't necessarily true."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d5ec4353-7a66-436d-bc72-d66cdf865ec2",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 1., 1., 1., 1., 1.])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"x = np.ones(6)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7716236c-e857-411f-91b8-72a45fd0faa8",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.9999999999999999\n",
"1.0000000000000002\n",
"0.9999999999999999\n",
"1.0000000000000002\n",
"0.9999999999999999\n",
"1.0000000000000002\n",
"0.9999999999999999\n",
"1.0000000000000002\n",
"0.9999999999999999\n",
"1.0000000000000002\n"
]
}
],
"source": [
"for i in range(10):\n",
" x = x/np.sum(x)\n",
" print(sum(x))"
]
},
{
"cell_type": "markdown",
"id": "ad791620-09a1-4e0b-9680-262128656094",
"metadata": {},
"source": [
"You get different answers depending on how you sum up the elements of `x`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a3ca97bf-a98e-40dc-8077-3ec562ee0e2f",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"0.16666666666666669"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = x[0]\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6f52c735-5e4f-49c5-8672-5aab7588fc25",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1.0000000000000002"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a+a+a+a+a+a"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8a68507f-4875-4f36-8141-008096a02004",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a+a)+(a+a)+(a+a)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c8a1911e-2477-4817-b0e6-cecca8594523",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"0.16666666666666666"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = x/np.sum(x)\n",
"a = x[0]\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "a506821f-81e9-4048-9023-4fb91e893eee",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"0.9999999999999999"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a+a+a+a+a+a"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e6499b63-df55-45f9-8a5a-dcb64c610724",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a+a)+(a+a)+(a+a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f3c31423-ea7e-4c52-9a0f-98fe7ec7ac87",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment