end of Week 5

This commit is contained in:
James Turk 2024-10-30 21:39:04 -05:00
parent 732046428a
commit f3e3993893
4 changed files with 1474 additions and 286 deletions

View File

@ -131,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "1e9d6428-8f80-4707-b508-c15878459d1f",
"metadata": {},
"outputs": [],
@ -159,6 +159,7 @@
"def expensive_calculation(a, b, *, c=0):\n",
" print(f\"doing expensive calculation on {a} {b}...\")\n",
" return a ** b\n",
"#expensive_calculation = cache(expensive_calculation)\n",
"\n",
"@cache\n",
"def cheap_calculation(a, b):\n",
@ -230,53 +231,95 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"id": "b6da10be-562c-4968-86ed-c473db63eccf",
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "TypeError",
"evalue": "repeatN() missing 1 required positional argument: 'n'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[31], line 8\u001b[0m\n\u001b[1;32m 4\u001b[0m func(a, b, c)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m newfunc\n\u001b[1;32m 7\u001b[0m \u001b[38;5;129;43m@repeatN\u001b[39;49m\n\u001b[0;32m----> 8\u001b[0m \u001b[38;5;28;43;01mdef\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;21;43mprint_sum\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 9\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mprint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43msum\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: repeatN() missing 1 required positional argument: 'n'"
]
}
],
"source": [
"def repeat5(func): # the decorator \n",
" def newfunc(a, b, c): # the inner function\n",
" for i in range(5):\n",
" print(a, b, c)\n",
" return func(a, b, c, c, b, a)\n",
" return newfunc\n",
"\n",
"@repeat5 # the wrapped function\n",
"@repeatN\n",
"def print_sum(*args):\n",
" print(sum(args))"
" print(sum(args))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"id": "7f43e771-92d9-48d3-96ed-b95b1c94e942",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"6\n",
"6\n",
"6\n",
"6\n"
]
}
],
"source": [
"print_sum(1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "4327b95d-b9ab-49e3-a530-b34e0688cff6",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sdrawkcab\n",
"sdrawkcab\n",
"sdrawkcab\n",
"sdrawkcab\n",
"sdrawkcab\n",
"sdrawkcab\n",
"sdrawkcab\n",
"sdrawkcab\n",
"sdrawkcab\n",
"sdrawkcab\n"
]
}
],
"source": [
"# to make a decorator that takes additional arguments\n",
"# you need to write a decorator factory function that returns decorators\n",
"\n",
"def repeat(n): # factory: takes integer, returns decorator\n",
"total = 123\n",
"\n",
"def repeat(start, end): # factory: takes integer, returns decorator\n",
" def repeat_decorator(func): # decorator: takes function, returns function\n",
" def newfunc(*args, **kwargs): # inner function: takes ?, returns ?\n",
" for i in range(n):\n",
" for i in range(total):\n",
" func(*args, **kwargs)\n",
" return newfunc\n",
" return repeat_decorator\n",
"\n",
"@repeat(10)\n",
"def print_backwards(s):\n",
" print(\"backwards\")\n",
" print(s[::-1])\n",
"\n",
"print_backwards(\"backwards\")"
]
@ -306,7 +349,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"id": "fb9e74b4-a0a2-4463-add7-45b04430edcd",
"metadata": {},
"outputs": [],
@ -317,47 +360,89 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"id": "e64ec996-4085-4c03-a771-6c1073b04990",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, Scott, Paul, Lauren\n"
]
}
],
"source": [
"print_hello_names(\"Scott\", \"Paul\", \"Lauren\")"
"print_hello_names(\"Scott\", \"Paul\", \"Lauren\")\n",
"# same as print(\"Hello\", \"Scott\", \"Paul\", \"Lauren\", sep=\", \")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "eb0d1ad3-e636-4430-9139-265740adc8a1",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"('Hello',)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print_hello_names.args"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "9e67124b-9014-49d9-af86-54b8a617521f",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'sep': ', '}"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print_hello_names.keywords"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"id": "f4939684-6c92-4c07-8d88-f7c4349d0f02",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<function print>"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print_hello_names.func"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 36,
"id": "1ed27205-7691-46ee-bdc6-f75dae7d53da",
"metadata": {},
"outputs": [],
@ -367,28 +452,39 @@
" def newfunc(*args, **kwargs):\n",
" return func(*args, **kwargs)\n",
" # we can do whatever we like after defining newfunc, but before returning it\n",
" newfunc.is_wrapped = True\n",
" newfunc.xyz = \"hello\"*2\n",
" return newfunc"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"id": "8e02bb23-7278-4799-85d6-9004af65565b",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# property is assigned to all wrapped functions\n",
"@wrapper\n",
"def our_function():\n",
" print(\"inside our function\")\n",
"\n",
"our_function.is_wrapped"
"our_function.xyz"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 40,
"id": "93b1ad42-b4a2-4947-a4bb-19c818968e2c",
"metadata": {},
"outputs": [],
@ -406,41 +502,101 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 39,
"id": "092b1a1d-4300-4895-aa06-4d874bc61159",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Scott, Paul, Lauren, Hello!"
]
}
],
"source": [
"print_hello_names2 = our_partial(print, \"Hello\", sep=\", \")\n",
"print_hello_names2(\"Scott\", \"Paul\", \"Lauren\")"
"print_hello_names2(\"Scott\", \"Paul\", \"Lauren\", end=\"!\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"id": "a0bd355b-63d5-41e2-8238-2e3dff22cb45",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello?Scott?Paul?Lauren!"
]
}
],
"source": [
"#print_hello_names2 = our_partial(print, \"Hello\", sep=\", \")\n",
"print_hello_names2(\"Scott\", \"Paul\", \"Lauren\", end=\"!\", sep=\"?\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "c48a5dfb-0444-4f5e-a4bc-39f0770ff5a3",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"('Hello',)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print_hello_names2.args"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"id": "f5d68812-918b-42dd-b23f-925a692668aa",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'sep': ', '}"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print_hello_names2.keywords"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"id": "6b41b480-43e1-49b2-ade7-44d8aa484a09",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<function print>"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print_hello_names2.func"
]

View File

@ -75,15 +75,676 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "4d5f6e18",
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on module statistics:\n",
"\n",
"NAME\n",
" statistics - Basic statistics module.\n",
"\n",
"MODULE REFERENCE\n",
" https://docs.python.org/3.10/library/statistics.html\n",
" \n",
" The following documentation is automatically generated from the Python\n",
" source files. It may be incomplete, incorrect or include features that\n",
" are considered implementation detail and may vary between Python\n",
" implementations. When in doubt, consult the module reference at the\n",
" location listed above.\n",
"\n",
"DESCRIPTION\n",
" This module provides functions for calculating statistics of data, including\n",
" averages, variance, and standard deviation.\n",
" \n",
" Calculating averages\n",
" --------------------\n",
" \n",
" ================== ==================================================\n",
" Function Description\n",
" ================== ==================================================\n",
" mean Arithmetic mean (average) of data.\n",
" fmean Fast, floating point arithmetic mean.\n",
" geometric_mean Geometric mean of data.\n",
" harmonic_mean Harmonic mean of data.\n",
" median Median (middle value) of data.\n",
" median_low Low median of data.\n",
" median_high High median of data.\n",
" median_grouped Median, or 50th percentile, of grouped data.\n",
" mode Mode (most common value) of data.\n",
" multimode List of modes (most common values of data).\n",
" quantiles Divide data into intervals with equal probability.\n",
" ================== ==================================================\n",
" \n",
" Calculate the arithmetic mean (\"the average\") of data:\n",
" \n",
" >>> mean([-1.0, 2.5, 3.25, 5.75])\n",
" 2.625\n",
" \n",
" \n",
" Calculate the standard median of discrete data:\n",
" \n",
" >>> median([2, 3, 4, 5])\n",
" 3.5\n",
" \n",
" \n",
" Calculate the median, or 50th percentile, of data grouped into class intervals\n",
" centred on the data values provided. E.g. if your data points are rounded to\n",
" the nearest whole number:\n",
" \n",
" >>> median_grouped([2, 2, 3, 3, 3, 4]) #doctest: +ELLIPSIS\n",
" 2.8333333333...\n",
" \n",
" This should be interpreted in this way: you have two data points in the class\n",
" interval 1.5-2.5, three data points in the class interval 2.5-3.5, and one in\n",
" the class interval 3.5-4.5. The median of these data points is 2.8333...\n",
" \n",
" \n",
" Calculating variability or spread\n",
" ---------------------------------\n",
" \n",
" ================== =============================================\n",
" Function Description\n",
" ================== =============================================\n",
" pvariance Population variance of data.\n",
" variance Sample variance of data.\n",
" pstdev Population standard deviation of data.\n",
" stdev Sample standard deviation of data.\n",
" ================== =============================================\n",
" \n",
" Calculate the standard deviation of sample data:\n",
" \n",
" >>> stdev([2.5, 3.25, 5.5, 11.25, 11.75]) #doctest: +ELLIPSIS\n",
" 4.38961843444...\n",
" \n",
" If you have previously calculated the mean, you can pass it as the optional\n",
" second argument to the four \"spread\" functions to avoid recalculating it:\n",
" \n",
" >>> data = [1, 2, 2, 4, 4, 4, 5, 6]\n",
" >>> mu = mean(data)\n",
" >>> pvariance(data, mu)\n",
" 2.5\n",
" \n",
" \n",
" Statistics for relations between two inputs\n",
" -------------------------------------------\n",
" \n",
" ================== ====================================================\n",
" Function Description\n",
" ================== ====================================================\n",
" covariance Sample covariance for two variables.\n",
" correlation Pearson's correlation coefficient for two variables.\n",
" linear_regression Intercept and slope for simple linear regression.\n",
" ================== ====================================================\n",
" \n",
" Calculate covariance, Pearson's correlation, and simple linear regression\n",
" for two inputs:\n",
" \n",
" >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
" >>> y = [1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
" >>> covariance(x, y)\n",
" 0.75\n",
" >>> correlation(x, y) #doctest: +ELLIPSIS\n",
" 0.31622776601...\n",
" >>> linear_regression(x, y) #doctest:\n",
" LinearRegression(slope=0.1, intercept=1.5)\n",
" \n",
" \n",
" Exceptions\n",
" ----------\n",
" \n",
" A single exception is defined: StatisticsError is a subclass of ValueError.\n",
"\n",
"CLASSES\n",
" builtins.ValueError(builtins.Exception)\n",
" StatisticsError\n",
" builtins.object\n",
" NormalDist\n",
" \n",
" class NormalDist(builtins.object)\n",
" | NormalDist(mu=0.0, sigma=1.0)\n",
" | \n",
" | Normal distribution of a random variable\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __add__(x1, x2)\n",
" | Add a constant or another NormalDist instance.\n",
" | \n",
" | If *other* is a constant, translate mu by the constant,\n",
" | leaving sigma unchanged.\n",
" | \n",
" | If *other* is a NormalDist, add both the means and the variances.\n",
" | Mathematically, this works only if the two distributions are\n",
" | independent or if they are jointly normally distributed.\n",
" | \n",
" | __eq__(x1, x2)\n",
" | Two NormalDist objects are equal if their mu and sigma are both equal.\n",
" | \n",
" | __getstate__(self)\n",
" | \n",
" | __hash__(self)\n",
" | NormalDist objects hash equal if their mu and sigma are both equal.\n",
" | \n",
" | __init__(self, mu=0.0, sigma=1.0)\n",
" | NormalDist where mu is the mean and sigma is the standard deviation.\n",
" | \n",
" | __mul__(x1, x2)\n",
" | Multiply both mu and sigma by a constant.\n",
" | \n",
" | Used for rescaling, perhaps to change measurement units.\n",
" | Sigma is scaled with the absolute value of the constant.\n",
" | \n",
" | __neg__(x1)\n",
" | Negates mu while keeping sigma the same.\n",
" | \n",
" | __pos__(x1)\n",
" | Return a copy of the instance.\n",
" | \n",
" | __radd__ = __add__(x1, x2)\n",
" | \n",
" | __repr__(self)\n",
" | Return repr(self).\n",
" | \n",
" | __rmul__ = __mul__(x1, x2)\n",
" | \n",
" | __rsub__(x1, x2)\n",
" | Subtract a NormalDist from a constant or another NormalDist.\n",
" | \n",
" | __setstate__(self, state)\n",
" | \n",
" | __sub__(x1, x2)\n",
" | Subtract a constant or another NormalDist instance.\n",
" | \n",
" | If *other* is a constant, translate by the constant mu,\n",
" | leaving sigma unchanged.\n",
" | \n",
" | If *other* is a NormalDist, subtract the means and add the variances.\n",
" | Mathematically, this works only if the two distributions are\n",
" | independent or if they are jointly normally distributed.\n",
" | \n",
" | __truediv__(x1, x2)\n",
" | Divide both mu and sigma by a constant.\n",
" | \n",
" | Used for rescaling, perhaps to change measurement units.\n",
" | Sigma is scaled with the absolute value of the constant.\n",
" | \n",
" | cdf(self, x)\n",
" | Cumulative distribution function. P(X <= x)\n",
" | \n",
" | inv_cdf(self, p)\n",
" | Inverse cumulative distribution function. x : P(X <= x) = p\n",
" | \n",
" | Finds the value of the random variable such that the probability of\n",
" | the variable being less than or equal to that value equals the given\n",
" | probability.\n",
" | \n",
" | This function is also called the percent point function or quantile\n",
" | function.\n",
" | \n",
" | overlap(self, other)\n",
" | Compute the overlapping coefficient (OVL) between two normal distributions.\n",
" | \n",
" | Measures the agreement between two normal probability distributions.\n",
" | Returns a value between 0.0 and 1.0 giving the overlapping area in\n",
" | the two underlying probability density functions.\n",
" | \n",
" | >>> N1 = NormalDist(2.4, 1.6)\n",
" | >>> N2 = NormalDist(3.2, 2.0)\n",
" | >>> N1.overlap(N2)\n",
" | 0.8035050657330205\n",
" | \n",
" | pdf(self, x)\n",
" | Probability density function. P(x <= X < x+dx) / dx\n",
" | \n",
" | quantiles(self, n=4)\n",
" | Divide into *n* continuous intervals with equal probability.\n",
" | \n",
" | Returns a list of (n - 1) cut points separating the intervals.\n",
" | \n",
" | Set *n* to 4 for quartiles (the default). Set *n* to 10 for deciles.\n",
" | Set *n* to 100 for percentiles which gives the 99 cuts points that\n",
" | separate the normal distribution in to 100 equal sized groups.\n",
" | \n",
" | samples(self, n, *, seed=None)\n",
" | Generate *n* samples for a given mean and standard deviation.\n",
" | \n",
" | zscore(self, x)\n",
" | Compute the Standard Score. (x - mean) / stdev\n",
" | \n",
" | Describes *x* in terms of the number of standard deviations\n",
" | above or below the mean of the normal distribution.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Class methods defined here:\n",
" | \n",
" | from_samples(data) from builtins.type\n",
" | Make a normal distribution instance from sample data.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Readonly properties defined here:\n",
" | \n",
" | mean\n",
" | Arithmetic mean of the normal distribution.\n",
" | \n",
" | median\n",
" | Return the median of the normal distribution\n",
" | \n",
" | mode\n",
" | Return the mode of the normal distribution\n",
" | \n",
" | The mode is the value x where which the probability density\n",
" | function (pdf) takes its maximum value.\n",
" | \n",
" | stdev\n",
" | Standard deviation of the normal distribution.\n",
" | \n",
" | variance\n",
" | Square of the standard deviation.\n",
" \n",
" class StatisticsError(builtins.ValueError)\n",
" | Method resolution order:\n",
" | StatisticsError\n",
" | builtins.ValueError\n",
" | builtins.Exception\n",
" | builtins.BaseException\n",
" | builtins.object\n",
" | \n",
" | Data descriptors defined here:\n",
" | \n",
" | __weakref__\n",
" | list of weak references to the object (if defined)\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Methods inherited from builtins.ValueError:\n",
" | \n",
" | __init__(self, /, *args, **kwargs)\n",
" | Initialize self. See help(type(self)) for accurate signature.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Static methods inherited from builtins.ValueError:\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Methods inherited from builtins.BaseException:\n",
" | \n",
" | __delattr__(self, name, /)\n",
" | Implement delattr(self, name).\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __reduce__(...)\n",
" | Helper for pickle.\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
" | \n",
" | __setattr__(self, name, value, /)\n",
" | Implement setattr(self, name, value).\n",
" | \n",
" | __setstate__(...)\n",
" | \n",
" | __str__(self, /)\n",
" | Return str(self).\n",
" | \n",
" | with_traceback(...)\n",
" | Exception.with_traceback(tb) --\n",
" | set self.__traceback__ to tb and return self.\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Data descriptors inherited from builtins.BaseException:\n",
" | \n",
" | __cause__\n",
" | exception cause\n",
" | \n",
" | __context__\n",
" | exception context\n",
" | \n",
" | __dict__\n",
" | \n",
" | __suppress_context__\n",
" | \n",
" | __traceback__\n",
" | \n",
" | args\n",
"\n",
"FUNCTIONS\n",
" correlation(x, y, /)\n",
" Pearson's correlation coefficient\n",
" \n",
" Return the Pearson's correlation coefficient for two inputs. Pearson's\n",
" correlation coefficient *r* takes values between -1 and +1. It measures the\n",
" strength and direction of the linear relationship, where +1 means very\n",
" strong, positive linear relationship, -1 very strong, negative linear\n",
" relationship, and 0 no linear relationship.\n",
" \n",
" >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
" >>> y = [9, 8, 7, 6, 5, 4, 3, 2, 1]\n",
" >>> correlation(x, x)\n",
" 1.0\n",
" >>> correlation(x, y)\n",
" -1.0\n",
" \n",
" covariance(x, y, /)\n",
" Covariance\n",
" \n",
" Return the sample covariance of two inputs *x* and *y*. Covariance\n",
" is a measure of the joint variability of two inputs.\n",
" \n",
" >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
" >>> y = [1, 2, 3, 1, 2, 3, 1, 2, 3]\n",
" >>> covariance(x, y)\n",
" 0.75\n",
" >>> z = [9, 8, 7, 6, 5, 4, 3, 2, 1]\n",
" >>> covariance(x, z)\n",
" -7.5\n",
" >>> covariance(z, x)\n",
" -7.5\n",
" \n",
" fmean(data)\n",
" Convert data to floats and compute the arithmetic mean.\n",
" \n",
" This runs faster than the mean() function and it always returns a float.\n",
" If the input dataset is empty, it raises a StatisticsError.\n",
" \n",
" >>> fmean([3.5, 4.0, 5.25])\n",
" 4.25\n",
" \n",
" geometric_mean(data)\n",
" Convert data to floats and compute the geometric mean.\n",
" \n",
" Raises a StatisticsError if the input dataset is empty,\n",
" if it contains a zero, or if it contains a negative value.\n",
" \n",
" No special efforts are made to achieve exact results.\n",
" (However, this may change in the future.)\n",
" \n",
" >>> round(geometric_mean([54, 24, 36]), 9)\n",
" 36.0\n",
" \n",
" harmonic_mean(data, weights=None)\n",
" Return the harmonic mean of data.\n",
" \n",
" The harmonic mean is the reciprocal of the arithmetic mean of the\n",
" reciprocals of the data. It can be used for averaging ratios or\n",
" rates, for example speeds.\n",
" \n",
" Suppose a car travels 40 km/hr for 5 km and then speeds-up to\n",
" 60 km/hr for another 5 km. What is the average speed?\n",
" \n",
" >>> harmonic_mean([40, 60])\n",
" 48.0\n",
" \n",
" Suppose a car travels 40 km/hr for 5 km, and when traffic clears,\n",
" speeds-up to 60 km/hr for the remaining 30 km of the journey. What\n",
" is the average speed?\n",
" \n",
" >>> harmonic_mean([40, 60], weights=[5, 30])\n",
" 56.0\n",
" \n",
" If ``data`` is empty, or any element is less than zero,\n",
" ``harmonic_mean`` will raise ``StatisticsError``.\n",
" \n",
" linear_regression(x, y, /)\n",
" Slope and intercept for simple linear regression.\n",
" \n",
" Return the slope and intercept of simple linear regression\n",
" parameters estimated using ordinary least squares. Simple linear\n",
" regression describes relationship between an independent variable\n",
" *x* and a dependent variable *y* in terms of linear function:\n",
" \n",
" y = slope * x + intercept + noise\n",
" \n",
" where *slope* and *intercept* are the regression parameters that are\n",
" estimated, and noise represents the variability of the data that was\n",
" not explained by the linear regression (it is equal to the\n",
" difference between predicted and actual values of the dependent\n",
" variable).\n",
" \n",
" The parameters are returned as a named tuple.\n",
" \n",
" >>> x = [1, 2, 3, 4, 5]\n",
" >>> noise = NormalDist().samples(5, seed=42)\n",
" >>> y = [3 * x[i] + 2 + noise[i] for i in range(5)]\n",
" >>> linear_regression(x, y) #doctest: +ELLIPSIS\n",
" LinearRegression(slope=3.09078914170..., intercept=1.75684970486...)\n",
" \n",
" mean(data)\n",
" Return the sample arithmetic mean of data.\n",
" \n",
" >>> mean([1, 2, 3, 4, 4])\n",
" 2.8\n",
" \n",
" >>> from fractions import Fraction as F\n",
" >>> mean([F(3, 7), F(1, 21), F(5, 3), F(1, 3)])\n",
" Fraction(13, 21)\n",
" \n",
" >>> from decimal import Decimal as D\n",
" >>> mean([D(\"0.5\"), D(\"0.75\"), D(\"0.625\"), D(\"0.375\")])\n",
" Decimal('0.5625')\n",
" \n",
" If ``data`` is empty, StatisticsError will be raised.\n",
" \n",
" median(data)\n",
" Return the median (middle value) of numeric data.\n",
" \n",
" When the number of data points is odd, return the middle data point.\n",
" When the number of data points is even, the median is interpolated by\n",
" taking the average of the two middle values:\n",
" \n",
" >>> median([1, 3, 5])\n",
" 3\n",
" >>> median([1, 3, 5, 7])\n",
" 4.0\n",
" \n",
" median_grouped(data, interval=1)\n",
" Return the 50th percentile (median) of grouped continuous data.\n",
" \n",
" >>> median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])\n",
" 3.7\n",
" >>> median_grouped([52, 52, 53, 54])\n",
" 52.5\n",
" \n",
" This calculates the median as the 50th percentile, and should be\n",
" used when your data is continuous and grouped. In the above example,\n",
" the values 1, 2, 3, etc. actually represent the midpoint of classes\n",
" 0.5-1.5, 1.5-2.5, 2.5-3.5, etc. The middle value falls somewhere in\n",
" class 3.5-4.5, and interpolation is used to estimate it.\n",
" \n",
" Optional argument ``interval`` represents the class interval, and\n",
" defaults to 1. Changing the class interval naturally will change the\n",
" interpolated 50th percentile value:\n",
" \n",
" >>> median_grouped([1, 3, 3, 5, 7], interval=1)\n",
" 3.25\n",
" >>> median_grouped([1, 3, 3, 5, 7], interval=2)\n",
" 3.5\n",
" \n",
" This function does not check whether the data points are at least\n",
" ``interval`` apart.\n",
" \n",
" median_high(data)\n",
" Return the high median of data.\n",
" \n",
" When the number of data points is odd, the middle value is returned.\n",
" When it is even, the larger of the two middle values is returned.\n",
" \n",
" >>> median_high([1, 3, 5])\n",
" 3\n",
" >>> median_high([1, 3, 5, 7])\n",
" 5\n",
" \n",
" median_low(data)\n",
" Return the low median of numeric data.\n",
" \n",
" When the number of data points is odd, the middle value is returned.\n",
" When it is even, the smaller of the two middle values is returned.\n",
" \n",
" >>> median_low([1, 3, 5])\n",
" 3\n",
" >>> median_low([1, 3, 5, 7])\n",
" 3\n",
" \n",
" mode(data)\n",
" Return the most common data point from discrete or nominal data.\n",
" \n",
" ``mode`` assumes discrete data, and returns a single value. This is the\n",
" standard treatment of the mode as commonly taught in schools:\n",
" \n",
" >>> mode([1, 1, 2, 3, 3, 3, 3, 4])\n",
" 3\n",
" \n",
" This also works with nominal (non-numeric) data:\n",
" \n",
" >>> mode([\"red\", \"blue\", \"blue\", \"red\", \"green\", \"red\", \"red\"])\n",
" 'red'\n",
" \n",
" If there are multiple modes with same frequency, return the first one\n",
" encountered:\n",
" \n",
" >>> mode(['red', 'red', 'green', 'blue', 'blue'])\n",
" 'red'\n",
" \n",
" If *data* is empty, ``mode``, raises StatisticsError.\n",
" \n",
" multimode(data)\n",
" Return a list of the most frequently occurring values.\n",
" \n",
" Will return more than one result if there are multiple modes\n",
" or an empty list if *data* is empty.\n",
" \n",
" >>> multimode('aabbbbbbbbcc')\n",
" ['b']\n",
" >>> multimode('aabbbbccddddeeffffgg')\n",
" ['b', 'd', 'f']\n",
" >>> multimode('')\n",
" []\n",
" \n",
" pstdev(data, mu=None)\n",
" Return the square root of the population variance.\n",
" \n",
" See ``pvariance`` for arguments and other details.\n",
" \n",
" >>> pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])\n",
" 0.986893273527251\n",
" \n",
" pvariance(data, mu=None)\n",
" Return the population variance of ``data``.\n",
" \n",
" data should be a sequence or iterable of Real-valued numbers, with at least one\n",
" value. The optional argument mu, if given, should be the mean of\n",
" the data. If it is missing or None, the mean is automatically calculated.\n",
" \n",
" Use this function to calculate the variance from the entire population.\n",
" To estimate the variance from a sample, the ``variance`` function is\n",
" usually a better choice.\n",
" \n",
" Examples:\n",
" \n",
" >>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]\n",
" >>> pvariance(data)\n",
" 1.25\n",
" \n",
" If you have already calculated the mean of the data, you can pass it as\n",
" the optional second argument to avoid recalculating it:\n",
" \n",
" >>> mu = mean(data)\n",
" >>> pvariance(data, mu)\n",
" 1.25\n",
" \n",
" Decimals and Fractions are supported:\n",
" \n",
" >>> from decimal import Decimal as D\n",
" >>> pvariance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\n",
" Decimal('24.815')\n",
" \n",
" >>> from fractions import Fraction as F\n",
" >>> pvariance([F(1, 4), F(5, 4), F(1, 2)])\n",
" Fraction(13, 72)\n",
" \n",
" quantiles(data, *, n=4, method='exclusive')\n",
" Divide *data* into *n* continuous intervals with equal probability.\n",
" \n",
" Returns a list of (n - 1) cut points separating the intervals.\n",
" \n",
" Set *n* to 4 for quartiles (the default). Set *n* to 10 for deciles.\n",
" Set *n* to 100 for percentiles which gives the 99 cuts points that\n",
" separate *data* in to 100 equal sized groups.\n",
" \n",
" The *data* can be any iterable containing sample.\n",
" The cut points are linearly interpolated between data points.\n",
" \n",
" If *method* is set to *inclusive*, *data* is treated as population\n",
" data. The minimum value is treated as the 0th percentile and the\n",
" maximum value is treated as the 100th percentile.\n",
" \n",
" stdev(data, xbar=None)\n",
" Return the square root of the sample variance.\n",
" \n",
" See ``variance`` for arguments and other details.\n",
" \n",
" >>> stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])\n",
" 1.0810874155219827\n",
" \n",
" variance(data, xbar=None)\n",
" Return the sample variance of data.\n",
" \n",
" data should be an iterable of Real-valued numbers, with at least two\n",
" values. The optional argument xbar, if given, should be the mean of\n",
" the data. If it is missing or None, the mean is automatically calculated.\n",
" \n",
" Use this function when your data is a sample from a population. To\n",
" calculate the variance from the entire population, see ``pvariance``.\n",
" \n",
" Examples:\n",
" \n",
" >>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]\n",
" >>> variance(data)\n",
" 1.3720238095238095\n",
" \n",
" If you have already calculated the mean of your data, you can pass it as\n",
" the optional second argument ``xbar`` to avoid recalculating it:\n",
" \n",
" >>> m = mean(data)\n",
" >>> variance(data, m)\n",
" 1.3720238095238095\n",
" \n",
" This function does not check that ``xbar`` is actually the mean of\n",
" ``data``. Giving arbitrary values for ``xbar`` may lead to invalid or\n",
" impossible results.\n",
" \n",
" Decimals and Fractions are supported:\n",
" \n",
" >>> from decimal import Decimal as D\n",
" >>> variance([D(\"27.5\"), D(\"30.25\"), D(\"30.25\"), D(\"34.5\"), D(\"41.75\")])\n",
" Decimal('31.01875')\n",
" \n",
" >>> from fractions import Fraction as F\n",
" >>> variance([F(1, 6), F(1, 2), F(5, 3)])\n",
" Fraction(67, 108)\n",
"\n",
"DATA\n",
" __all__ = ['NormalDist', 'StatisticsError', 'correlation', 'covariance...\n",
"\n",
"FILE\n",
" /Users/jamesturk/.local/share/uv/python/cpython-3.10.15-macos-aarch64-none/lib/python3.10/statistics.py\n",
"\n",
"\n"
]
}
],
"source": [
"import statistics\n",
"#help(statistics)"
"help(statistics)"
]
},
{
@ -96,19 +757,110 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 3,
"id": "5db28bf9-2ff6-4847-bd18-ae46ea99b57c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Counter',\n",
" 'Decimal',\n",
" 'Fraction',\n",
" 'LinearRegression',\n",
" 'NormalDist',\n",
" 'StatisticsError',\n",
" '__all__',\n",
" '__builtins__',\n",
" '__cached__',\n",
" '__doc__',\n",
" '__file__',\n",
" '__loader__',\n",
" '__name__',\n",
" '__package__',\n",
" '__spec__',\n",
" '_coerce',\n",
" '_convert',\n",
" '_exact_ratio',\n",
" '_fail_neg',\n",
" '_find_lteq',\n",
" '_find_rteq',\n",
" '_isfinite',\n",
" '_normal_dist_inv_cdf',\n",
" '_ss',\n",
" '_sum',\n",
" 'bisect_left',\n",
" 'bisect_right',\n",
" 'correlation',\n",
" 'covariance',\n",
" 'erf',\n",
" 'exp',\n",
" 'fabs',\n",
" 'fmean',\n",
" 'fsum',\n",
" 'geometric_mean',\n",
" 'groupby',\n",
" 'harmonic_mean',\n",
" 'hypot',\n",
" 'itemgetter',\n",
" 'linear_regression',\n",
" 'log',\n",
" 'math',\n",
" 'mean',\n",
" 'median',\n",
" 'median_grouped',\n",
" 'median_high',\n",
" 'median_low',\n",
" 'mode',\n",
" 'multimode',\n",
" 'namedtuple',\n",
" 'numbers',\n",
" 'pstdev',\n",
" 'pvariance',\n",
" 'quantiles',\n",
" 'random',\n",
" 'repeat',\n",
" 'sqrt',\n",
" 'stdev',\n",
" 'tau',\n",
" 'variance']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dir(statistics)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "09c27388",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on built-in function cos in module math:\n",
"\n",
"cos(x, /)\n",
" Return the cosine of x (measured in radians).\n",
"\n"
]
},
{
"data": {
"text/plain": [
"-1.0"
]
},
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@ -116,9 +868,60 @@
"source": [
"import math\n",
"\n",
"help(math.cos)\n",
"math.cos(math.pi)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "4b76400a-82af-4ce6-91a9-2b4c4e125e5b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on class repeat in module itertools:\n",
"\n",
"class repeat(builtins.object)\n",
" | repeat(object [,times]) -> create an iterator which returns the object\n",
" | for the specified number of times. If not specified, returns the object\n",
" | endlessly.\n",
" | \n",
" | Methods defined here:\n",
" | \n",
" | __getattribute__(self, name, /)\n",
" | Return getattr(self, name).\n",
" | \n",
" | __iter__(self, /)\n",
" | Implement iter(self).\n",
" | \n",
" | __length_hint__(...)\n",
" | Private method returning an estimate of len(list(it)).\n",
" | \n",
" | __next__(self, /)\n",
" | Implement next(self).\n",
" | \n",
" | __reduce__(...)\n",
" | Return state information for pickling.\n",
" | \n",
" | __repr__(self, /)\n",
" | Return repr(self).\n",
" | \n",
" | ----------------------------------------------------------------------\n",
" | Static methods defined here:\n",
" | \n",
" | __new__(*args, **kwargs) from builtins.type\n",
" | Create and return a new object. See help(type) for accurate signature.\n",
"\n"
]
}
],
"source": [
"help(statistics.repeat)"
]
},
{
"cell_type": "markdown",
"id": "9d5f6a9f",
@ -202,9 +1005,41 @@
"execution_count": 6,
"id": "1604f2d7",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function mode in module statistics:\n",
"\n",
"mode(data)\n",
" Return the most common data point from discrete or nominal data.\n",
" \n",
" ``mode`` assumes discrete data, and returns a single value. This is the\n",
" standard treatment of the mode as commonly taught in schools:\n",
" \n",
" >>> mode([1, 1, 2, 3, 3, 3, 3, 4])\n",
" 3\n",
" \n",
" This also works with nominal (non-numeric) data:\n",
" \n",
" >>> mode([\"red\", \"blue\", \"blue\", \"red\", \"green\", \"red\", \"red\"])\n",
" 'red'\n",
" \n",
" If there are multiple modes with same frequency, return the first one\n",
" encountered:\n",
" \n",
" >>> mode(['red', 'red', 'green', 'blue', 'blue'])\n",
" 'red'\n",
" \n",
" If *data* is empty, ``mode``, raises StatisticsError.\n",
"\n"
]
}
],
"source": [
"#help(statistics.mode)"
"import statistics\n",
"help(statistics.mode)"
]
},
{
@ -245,11 +1080,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "57d95a79",
"metadata": {},
"outputs": [],
"source": []
"source": [
"#from .. import symbol"
]
},
{
"cell_type": "code",

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "c69f70a0-f070-4d3c-adaa-a4520cd73af5",
"metadata": {
"tags": []
@ -28,6 +28,27 @@
" return a * c"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6eabe23e-d4dc-442c-b405-4adc9fae7417",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.3333333333333333"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_func(4, 3)"
]
},
{
"cell_type": "code",
"execution_count": 2,
@ -53,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "1ef88eb5-0852-4d97-bd25-340b83730442",
"metadata": {
"tags": []
@ -66,8 +87,8 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[1], line 4\u001b[0m, in \u001b[0;36mmy_func\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\" what can go wrong with this function? \"\"\"\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a \u001b[38;5;241m>\u001b[39m b:\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a \u001b[38;5;241m*\u001b[39m c\n",
"Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmy_func\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[2], line 4\u001b[0m, in \u001b[0;36mmy_func\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\" what can go wrong with this function? \"\"\"\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a \u001b[38;5;241m>\u001b[39m b:\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43ma\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m a \u001b[38;5;241m*\u001b[39m c\n",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
@ -214,17 +235,18 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 10,
"id": "5022b7c8-a19f-45cb-9d40-58abc48de961",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"can't divide by zero, set c = 0\n"
"ename": "SyntaxError",
"evalue": "expected 'except' or 'finally' block (572013031.py, line 6)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[10], line 6\u001b[0;36m\u001b[0m\n\u001b[0;31m print(\"hello\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m expected 'except' or 'finally' block\n"
]
}
],
@ -233,9 +255,11 @@
" a = 3\n",
" b = 0\n",
" c = a / b\n",
" print(\"last line of try\")\n",
"except ZeroDivisionError:\n",
" c = 0\n",
" print(f\"can't divide by zero, set c = {c}\")"
" print(f\"can't divide by zero, set c = {c}\")\n",
"print(\"and then this happens\")"
]
},
{