未验证 提交 fafd56d4 编写于 作者: J jaschmitt 提交者: GitHub

Merge branch 'master' into python-dash

# Numpy Tutorial: First Steps in Data Science
This folder contains the sample code for the [NumPy Tutorial](https://realpython.com/numpy-tutorial/) by @rpalo.
## Installing
First, you will need to have the dependencies installed:
```shell
$ python -m pip install -r requirements.txt
```
## Usage
These examples all make use of the [Jupyter Notebook](https://jupyter-notebook.readthedocs.io/en/stable/). To run the examples, make sure the correct virtual environment is active (you may have to deactivate and reactivate after installing requirements), and run:
```shell
$ jupyter notebook
```
You should see listings for each of the example files. You can open them up and run them cell by cell to see how they perform. Feel free to make small changes to the code to see how those affect things.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Curving Grades\n",
"\n",
"You'll be playing the part of a teacher, curving test grades to account for an unfortunately difficult test. The curving should improve the student's grade relative to where they ended up with respect to the group's average. It should also *not* hurt the student by reducing their grade, and it shouldn't give anyone anything over 100%."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"CURVE_CENTER = 80"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def curve(grades):\n",
" \"\"\"Adjusts an array of grades so that the average is roughly shifted\n",
" to the specified curve center.\n",
"\n",
" This will never cause a student's grade to decrease, and it will\n",
" never cause the final grade to go over 100%.\n",
"\n",
" Parameters:\n",
" grades (np.ndarray): The individual student grades, between 0\n",
" and 100.\n",
"\n",
" Returns:\n",
" (np.ndarray): A new array of grades, adjusted upwards, but in\n",
" the same order.\n",
" \"\"\"\n",
" average = grades.mean()\n",
" change = CURVE_CENTER - average\n",
" new_grades = grades + change\n",
" return np.clip(new_grades, grades, 100)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 91.25, 54.25, 83.25, 100. , 70.25, 100. , 93.25, 31.25]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grades = np.array([[72, 35, 64, 88, 51, 90, 74, 12]])\n",
"curve(grades)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note how none of the grades are decreased or greater than 100%!"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Validating Dürer's Magic Square\n",
"\n",
"Albrecht Dürer was an artist and mathematician who created an engraving of a [magic square](https://mathworld.wolfram.com/DuerersMagicSquare.html), where lots of bits all add up to the same thing. Each row, column, quadrant, corners, etc. all add up to 34.\n",
"\n",
"At the bottom, you see 15 and 14, signifying the year it was engraved (1514) as well as 4 and 1, standing for D and A, the creator's initials.\n",
"\n",
"You're going to validate that everything does, in fact, add up."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[16, 3, 2, 13],\n",
" [ 5, 10, 11, 8],\n",
" [ 9, 6, 7, 12],\n",
" [ 4, 15, 14, 1]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"square = np.array([\n",
" [16, 3, 2, 13],\n",
" [5, 10, 11, 8],\n",
" [9, 6, 7, 12],\n",
" [4, 15, 14, 1],\n",
"])\n",
"square"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"All assertions passed!\n"
]
}
],
"source": [
"for i in range(4):\n",
" assert square[i, :].sum() == 34\n",
" assert square[:, i].sum() == 34\n",
" \n",
"assert square[:2, :2].sum() == 34\n",
"assert square[2:, :2].sum() == 34\n",
"assert square[:2, 2:].sum() == 34\n",
"assert square[2:, 2:].sum() == 34\n",
"\n",
"print(\"All assertions passed!\")"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
因为 它太大了无法显示 source diff 。你可以改为 查看blob
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using a Maclaurin Series to Estimate $e$\n",
"\n",
"A [Maclaurin series](https://mathworld.wolfram.com/MaclaurinSeries.html) is an infinite series of terms that can be used to approximate more complex functions quickly.\n",
"\n",
"You're going to approximate $e^x$ by using the first few terms of the series. The series equation for $e^x$ is this:\n",
"\n",
"$$\\sum_{n=0}^{\\infty} \\frac{x^n}{n!} = 1 + x + \\frac{x^2}{2} + \\frac{x^3}{6} \\ldots$$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from math import e, factorial"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"fac = np.vectorize(factorial)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def e_x(x, terms=10):\n",
" \"\"\"Approximates $e^x$ using a given number of terms of the Maclaurin series\"\"\"\n",
" n = np.arange(terms)\n",
" return np.sum((x ** n) / fac(n))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20.085536923187664"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Actual:\n",
"e ** 3"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"N (terms)\tMaclaurin\tError\n",
"1\t\t1.000\t\t19.086\n",
"2\t\t4.000\t\t16.086\n",
"3\t\t8.500\t\t11.586\n",
"4\t\t13.000\t\t7.086\n",
"5\t\t16.375\t\t3.711\n",
"6\t\t18.400\t\t1.686\n",
"7\t\t19.412\t\t0.673\n",
"8\t\t19.846\t\t0.239\n",
"9\t\t20.009\t\t0.076\n",
"10\t\t20.063\t\t0.022\n",
"11\t\t20.080\t\t0.006\n",
"12\t\t20.084\t\t0.001\n",
"13\t\t20.085\t\t0.000\n"
]
}
],
"source": [
"print(\"N (terms)\\tMaclaurin\\tError\")\n",
"\n",
"for n in range(1, 14):\n",
" maclaurin = e_x(3, terms=n)\n",
" print(f\"{n}\\t\\t{maclaurin:.03f}\\t\\t{e**3 - maclaurin:.03f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
\ No newline at end of file
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Validating the Normal Distribution\n",
"\n",
"The [Normal Distribution](https://en.wikipedia.org/wiki/Normal_distribution) or Gaussian Distribution is a probability distribution with a distinctive bell-curve shape. It has the property that 68.2% of all values are within one standard deviation of the mean, and 95.45% of all values are within two standard deviations of the mean.\n",
"\n",
"Here, you'll be verifying that ~95% of the values are indeed between -2 standard deviations and +2 standard deviations of the mean by sampling randomly."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from numpy.random import default_rng"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.20262383, -0.5297287 , -0.34621358, -0.08370099, -0.09591137])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rng = default_rng()\n",
"values = rng.standard_normal(10_000)\n",
"values[:5]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9550"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"std = values.std()\n",
"filtered = values[(values > -2 * std) & (values < 2 * std)]\n",
"\n",
"filtered.size"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10000"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"values.size"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.955"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filtered.size / values.size"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
appnope==0.1.0
argon2-cffi==20.1.0
async-generator==1.10
attrs==20.2.0
backcall==0.2.0
bleach==3.2.1
cffi==1.14.3
cycler==0.10.0
decorator==4.4.2
defusedxml==0.6.0
entrypoints==0.3
ipykernel==5.3.4
ipython==7.17.0
ipython-genutils==0.2.0
jedi==0.17.2
Jinja2==2.11.2
jsonschema==3.2.0
jupyter-client==6.1.7
jupyter-core==4.6.3
jupyterlab-pygments==0.1.2
kiwisolver==1.2.0
MarkupSafe==1.1.1
matplotlib==3.3.0
mistune==0.8.4
nbclient==0.5.1
nbconvert==6.0.7
nbformat==5.0.8
nest-asyncio==1.4.2
notebook==6.1.4
numpy==1.19.1
packaging==20.4
pandocfilters==1.4.3
parso==0.7.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==7.2.0
prometheus-client==0.8.0
prompt-toolkit==3.0.6
ptyprocess==0.6.0
pycparser==2.20
Pygments==2.6.1
pyparsing==2.4.7
pyrsistent==0.17.3
python-dateutil==2.8.1
pyzmq==19.0.2
Send2Trash==1.5.0
six==1.15.0
terminado==0.9.1
testpath==0.4.4
tornado==6.0.4
traitlets==4.3.3
wcwidth==0.2.5
webencodings==0.5.1
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册