Skip to content

Commit

Permalink
materials-generate-scripts update lectures and reference
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-morris committed Jun 13, 2024
1 parent f3c5145 commit 03b926b
Show file tree
Hide file tree
Showing 43 changed files with 29,657 additions and 195 deletions.
277 changes: 277 additions & 0 deletions lec/lec01.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-0",
"metadata": {},
"outputs": [],
"source": [
"# The pip install can take a minute\n",
"%pip install -q urllib3<2.0 datascience ipywidgets\n",
"import pyodide_http\n",
"pyodide_http.patch_all()\n",
"\n",
"from datascience import *\n",
"import numpy as np\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plots\n",
"plots.style.use('fivethirtyeight')\n",
"import warnings\n",
"warnings.simplefilter(action=\"ignore\", category=FutureWarning)\n",
"\n",
"from urllib.request import urlopen \n",
"import re\n",
"def read_url(url): \n",
" return re.sub('\\\\s+', ' ', urlopen(url).read().decode())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-1",
"metadata": {},
"outputs": [],
"source": [
"2+3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-2",
"metadata": {},
"outputs": [],
"source": [
"# Read two books, fast!\n",
"\n",
"huck_finn_url = 'https://www.inferentialthinking.com/data/huck_finn.txt'\n",
"huck_finn_text = read_url(huck_finn_url)\n",
"huck_finn_chapters = huck_finn_text.split('CHAPTER ')[44:]\n",
"\n",
"little_women_url = 'https://www.inferentialthinking.com/data/little_women.txt'\n",
"little_women_text = read_url(little_women_url)\n",
"little_women_chapters = little_women_text.split('CHAPTER ')[1:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-3",
"metadata": {},
"outputs": [],
"source": [
"huck_finn_chapters"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-4",
"metadata": {},
"outputs": [],
"source": [
"Table().with_column('Chapters', huck_finn_chapters)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-5",
"metadata": {},
"outputs": [],
"source": [
"np.char.count(huck_finn_chapters, 'Tom')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-6",
"metadata": {},
"outputs": [],
"source": [
"np.char.count(huck_finn_chapters, 'Jim')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-7",
"metadata": {},
"outputs": [],
"source": [
"counts = Table().with_columns([\n",
" 'Tom', np.char.count(huck_finn_chapters, 'Tom'),\n",
" 'Jim', np.char.count(huck_finn_chapters, 'Jim'),\n",
" 'Huck', np.char.count(huck_finn_chapters, 'Huck'),\n",
"])\n",
"counts"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-8",
"metadata": {},
"outputs": [],
"source": [
"# Plot the cumulative counts:\n",
"# how many times in Chapter 1, how many times in Chapters 1 and 2, and so on.\n",
"\n",
"cum_counts = counts.cumsum().with_column('Chapter', np.arange(1, 44, 1))\n",
"cum_counts.plot(column_for_xticks=3)\n",
"plots.title('Cumulative Number of Times Name Appears');"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-9",
"metadata": {},
"outputs": [],
"source": [
"# The chapters of Little Women\n",
"\n",
"Table().with_column('Chapters', little_women_chapters)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-10",
"metadata": {},
"outputs": [],
"source": [
"# Counts of names in the chapters of Little Women\n",
"\n",
"names = ['Amy', 'Beth', 'Jo', 'Laurie', 'Meg']\n",
"mentions = {name: np.char.count(little_women_chapters, name) for name in names}\n",
"\n",
"counts = Table().with_columns([\n",
" 'Amy', mentions['Amy'],\n",
" 'Beth', mentions['Beth'],\n",
" 'Jo', mentions['Jo'],\n",
" 'Laurie', mentions['Laurie'],\n",
" 'Meg', mentions['Meg']\n",
" ])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-11",
"metadata": {},
"outputs": [],
"source": [
"# Plot the cumulative counts\n",
"\n",
"cum_counts = counts.cumsum().with_column('Chapter', np.arange(1, 48, 1))\n",
"cum_counts.plot(column_for_xticks=5)\n",
"plots.title('Cumulative Number of Times Name Appears');"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-12",
"metadata": {},
"outputs": [],
"source": [
"len('Data 8')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-13",
"metadata": {},
"outputs": [],
"source": [
"len(read_url(huck_finn_url))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-14",
"metadata": {},
"outputs": [],
"source": [
"# In each chapter, count the number of all characters;\n",
"# call this the \"length\" of the chapter.\n",
"# Also count the number of periods.\n",
"\n",
"length_hf = Table().with_columns([\n",
" 'Length', [len(s) for s in huck_finn_chapters],\n",
" 'Periods', np.char.count(huck_finn_chapters, '.')\n",
" ])\n",
"length_lw = Table().with_columns([\n",
" 'Length', [len(s) for s in little_women_chapters],\n",
" 'Periods', np.char.count(little_women_chapters, '.')\n",
" ])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-15",
"metadata": {},
"outputs": [],
"source": [
"# The counts for Huckleberry Finn\n",
"\n",
"length_hf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-16",
"metadata": {},
"outputs": [],
"source": [
"# The counts for Little Women\n",
"\n",
"length_lw"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cell-lec01-17",
"metadata": {},
"outputs": [],
"source": [
"plots.figure(figsize=(10,10))\n",
"plots.scatter(length_hf[1], length_hf[0], color='darkblue')\n",
"plots.scatter(length_lw[1], length_lw[0], color='gold')\n",
"plots.xlabel('Number of periods in chapter')\n",
"plots.ylabel('Number of characters in chapter');"
]
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Raw Cell Format",
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 03b926b

Please sign in to comment.