Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions numpy-tutorial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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.
Binary file added numpy-tutorial/bad-gray.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added numpy-tutorial/blue.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions numpy-tutorial/curve_grades.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"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
}
102 changes: 102 additions & 0 deletions numpy-tutorial/durer.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"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
}
Binary file added numpy-tutorial/good-gray.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
247 changes: 247 additions & 0 deletions numpy-tutorial/image_mod.ipynb

Large diffs are not rendered by default.

Binary file added numpy-tutorial/kitty.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
131 changes: 131 additions & 0 deletions numpy-tutorial/maclaurin.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"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
}
Loading