Add gitignore, remove files that were not ignored
This commit is contained in:
parent
7d6a8df094
commit
658eca8116
166
.gitignore
vendored
Normal file
166
.gitignore
vendored
Normal file
|
@ -0,0 +1,166 @@
|
|||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
||||
.pdm.toml
|
||||
.pdm-python
|
||||
.pdm-build/
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
.idea/
|
||||
|
||||
|
||||
# OSX
|
||||
.DS_Store
|
|
@ -1,386 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8685ea3a",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import timeit\n",
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "048881d0",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"# Example: Find common words"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "2464a282",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"Problem: given two lists of words, extract all the words that are in common"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "71740eab",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"# Implementation with 2x for-loops"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f175c775",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%timeit\n",
|
||||
"\n",
|
||||
"scaling_factor = 1 #10, 100\n",
|
||||
"\n",
|
||||
"words1 = ['apple', 'orange', 'banana', 'melon', 'peach'] * scaling_factor\n",
|
||||
"words2 = ['orange', 'kiwi', 'avocado', 'apple', 'banana'] * scaling_factor\n",
|
||||
"\n",
|
||||
"common_for = []\n",
|
||||
"for w in words1:\n",
|
||||
" if w in words2:\n",
|
||||
" common_for.append(w) # 612 ns, 12.3 us, 928 us "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "affab857",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"input_size = [1, 10, 100]\n",
|
||||
"results_for_loop = [(612/10**9)/(612/10**9), (12.4 /10**6)/(612/10**9), (928/10**6)/(612/10**9)] # in seconds\n",
|
||||
"\n",
|
||||
"x = np.linspace(0,100,100)\n",
|
||||
"fit1 = np.polyfit(input_size,results_for_loop,2)\n",
|
||||
"eval1 = np.polyval(fit1, x)\n",
|
||||
"\n",
|
||||
"plt.plot(x,eval1,c = 'orange')\n",
|
||||
"plt.scatter(input_size, results_for_loop, c = 'orange', s = 100, label = '2 for loops')\n",
|
||||
"\n",
|
||||
"plt.xlabel('input size')\n",
|
||||
"plt.ylabel('processing time')\n",
|
||||
"plt.yticks(results_for_loop, ['T', str(int((12.4 /10**6)/(513/10**9)))+ 'x T', str(int((928/10**6)/(513/10**9))) + 'x T'])\n",
|
||||
"plt.legend()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "2a61bf38",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "skip"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print('Data increase 1x, 10x, 100x')\n",
|
||||
"print('Time increase 513 ns, 12.4 µs, 928 µs')\n",
|
||||
"print('time1, ~ 24x time1, ~ 1800x time1')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "38e47397",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"What is the big-O complexity of this implementation? "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4118b38d",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "skip"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"n * n ~ O(n<sup>2</sup>)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "31cd0e74",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"# Implementation with sorted lists"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c13a24f4",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%timeit\n",
|
||||
"scaling_factor = 100 #10, 100\n",
|
||||
"words1 = ['apple', 'orange', 'banana', 'melon', 'peach'] * scaling_factor\n",
|
||||
"words2 = ['orange', 'kiwi', 'avocado', 'apple', 'banana'] *scaling_factor\n",
|
||||
"words1 = sorted(words1)\n",
|
||||
"words2 = sorted(words2)\n",
|
||||
"\n",
|
||||
"common_sort_list = []\n",
|
||||
"idx2 = 0\n",
|
||||
"for w in words1:\n",
|
||||
" while idx2 < len(words2) and words2[idx2] < w:\n",
|
||||
" idx2 += 1\n",
|
||||
" if idx2 >= len(words2):\n",
|
||||
" break\n",
|
||||
" if words2[idx2] == w:\n",
|
||||
" common_sort_list.append(w) #1.94 ns, 17.3 us, 204 us"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f1e8fed2",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 1.9 * 10**6\n",
|
||||
"# 17.9 * 10**6\n",
|
||||
"# 205 * 10**6"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8ce798ab",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"input_size = [1, 10, 100]\n",
|
||||
"results_sorted_lists = [(1.9 * 10**6)/(1.9 * 10**6), (17.9 * 10**6)/(1.9 * 10**6), (205 * 10**6)/(1.9 * 10**6)]\n",
|
||||
"fit2 = np.polyfit(input_size, results_sorted_lists, 2)\n",
|
||||
"eval2 = np.polyval(fit2, x)\n",
|
||||
"plt.plot(x,eval1,c = 'orange')\n",
|
||||
"plt.plot(x,eval2,c = 'pink')\n",
|
||||
"plt.scatter(input_size, results_for_loop, c = 'orange', s = 100, label = '2 for loops')\n",
|
||||
"plt.scatter(input_size, results_sorted_lists, c = 'pink', s = 100, label = 'sorted lists')\n",
|
||||
"plt.xlabel('input size')\n",
|
||||
"plt.ylabel('processing time')\n",
|
||||
"plt.yticks(results_for_loop + results_sorted_lists[1:], ['T', str(int((12.4 /10**6)/(513/10**9)))+ 'x T', str(int((928/10**6)/(513/10**9))) + 'x T',\n",
|
||||
" str(int((17.9 * 10**6)/(1.9 * 10**6)))+ 'x T', str(int((205 * 10**6)/(1.9 * 10**6))) + 'x T',])\n",
|
||||
"plt.legend()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1da4c22f",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"What is the big-O complexity of this implementation? "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4b068a1b",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"2 * sorting + traversing two lists = 2*n log<sub>2</sub> + 2*n ~ O(n * log<sub>n</sub>)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "13c96239",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "slide"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"# Implementation with sets"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "61edb9f3",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "fragment"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%timeit\n",
|
||||
"\n",
|
||||
"scaling_factor = 1\n",
|
||||
"\n",
|
||||
"words1 = ['apple', 'orange', 'banana', 'melon', 'peach'] * scaling_factor\n",
|
||||
"words2 = ['orange', 'kiwi', 'avocado', 'apple', 'banana'] *scaling_factor\n",
|
||||
"\n",
|
||||
"words2 = set(words2)\n",
|
||||
"\n",
|
||||
"common_sets = []\n",
|
||||
"for w in words1:\n",
|
||||
" if w in words2:\n",
|
||||
" common_sets.append(w) # 630 ns, 3.13 us, 28.6 us"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c90d8e68",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "notes"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 630 * 10**9\n",
|
||||
"# 3.13 * 10**6\n",
|
||||
"# 28.6 * 10**6"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "236c132d",
|
||||
"metadata": {
|
||||
"scrolled": true,
|
||||
"slideshow": {
|
||||
"slide_type": "subslide"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"results_sets = [(630 * 10**9)/(630 * 10**9), (3.13 * 10**6)/(630 * 10**9), (28.6 * 10**6)/(630 * 10**9)]\n",
|
||||
"fit3 = np.polyfit(input_size, results_sets, 2)\n",
|
||||
"eval3 = np.polyval(fit3, x)\n",
|
||||
"plt.plot(x,eval1,c = 'orange')\n",
|
||||
"plt.plot(x,eval2,c = 'pink')\n",
|
||||
"plt.plot(x, eval3, c = 'blue')\n",
|
||||
"plt.scatter(input_size, results_for_loop, c = 'orange', s = 100, label = '2 for loops')\n",
|
||||
"plt.scatter(input_size, results_sorted_lists, c = 'pink', s = 100, label = 'sorted lists')\n",
|
||||
"plt.scatter(input_size, results_sets, c = 'blue', s = 100, label = 'sets')\n",
|
||||
"plt.xlabel('input size')\n",
|
||||
"plt.ylabel('processing time')\n",
|
||||
"plt.yticks(results_for_loop + results_sorted_lists[1:], ['T', str(int((12.4 /10**6)/(513/10**9)))+ 'x T', str(int((928/10**6)/(513/10**9))) + 'x T', str(int((17.9 * 10**6)/(1.9 * 10**6)))+ 'x T', str(int((205 * 10**6)/(1.9 * 10**6))) + 'x T'])\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c9780532",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"What is the big-O complexity of this implementation? "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "297bcd7d",
|
||||
"metadata": {
|
||||
"slideshow": {
|
||||
"slide_type": "-"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"transforming one list to set + 1 for loop = 2 * n ~ O(n)\n",
|
||||
"\n",
|
||||
"It’s the exact same code as for lists, but now looking up an element in sets \u000b",
|
||||
"(if w in words2) takes constant time!\n",
|
||||
"How could you have known that set lookup is fast? Learning about data structures!"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"celltoolbar": "Slideshow",
|
||||
"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.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
File diff suppressed because it is too large
Load diff
|
@ -1,693 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "20df51b1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# NumPy views and copies\n",
|
||||
"\n",
|
||||
"- Operations that only require changing the metadata always do so, and return a **view**\n",
|
||||
"- Operations that cannot be executed by changing the metadata create a new memory block, and return a **copy**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "4ed67e38",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def print_info(a):\n",
|
||||
" \"\"\" Print the content of an array, and its metadata. \"\"\"\n",
|
||||
" \n",
|
||||
" txt = f\"\"\"\n",
|
||||
"dtype\\t{a.dtype}\n",
|
||||
"ndim\\t{a.ndim}\n",
|
||||
"shape\\t{a.shape}\n",
|
||||
"strides\\t{a.strides}\n",
|
||||
" \"\"\"\n",
|
||||
"\n",
|
||||
" print(a)\n",
|
||||
" print(txt)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"id": "53bd92f9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 0 1 2 3]\n",
|
||||
" [ 4 5 6 7]\n",
|
||||
" [ 8 9 10 11]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(3, 4)\n",
|
||||
"strides\t(32, 8)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = np.arange(12).reshape(3, 4).copy()\n",
|
||||
"print_info(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d2ee43d7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Views"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f4838e77",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Operations that only require changing the metadata always do so, and return a **view**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"id": "f1b82845",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 1 3]\n",
|
||||
" [ 9 11]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(2, 2)\n",
|
||||
"strides\t(64, 16)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y = x[0::2, 1::2]\n",
|
||||
"print_info(y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3199b45b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"A view shares the same memory block as the original array. \n",
|
||||
"\n",
|
||||
"CAREFUL: Modifying the view changes the original array and all an other views of that array as well!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"id": "28ea1c71",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 0 1 2 3 4 5 6 7 8 9 10 11]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(1, 12)\n",
|
||||
"strides\t(96, 8)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"z = x.reshape(1, 12)\n",
|
||||
"print_info(z)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"id": "46822b5a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[101 103]\n",
|
||||
" [109 111]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(2, 2)\n",
|
||||
"strides\t(64, 16)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y += 100\n",
|
||||
"print_info(y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"id": "ad9a7950",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 0 101 2 103]\n",
|
||||
" [ 4 5 6 7]\n",
|
||||
" [ 8 109 10 111]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(3, 4)\n",
|
||||
"strides\t(32, 8)\n",
|
||||
" \n",
|
||||
"[[ 0 101 2 103 4 5 6 7 8 109 10 111]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(1, 12)\n",
|
||||
"strides\t(96, 8)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print_info(x)\n",
|
||||
"print_info(z)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4fc789c1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Functions that take an array as an input should avoid modifying it in place! \n",
|
||||
"\n",
|
||||
"Always make a copy or be super extra clear in the docstring."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"id": "aa25ac4b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def robust_log(a, cte=1e-10):\n",
|
||||
" \"\"\" Returns the log of an array, avoiding troubles when a value is 0.\n",
|
||||
" \n",
|
||||
" Add a tiny constant to the values of `a` so that they are not 0. \n",
|
||||
" `a` is expected to have non-negative values.\n",
|
||||
" \"\"\"\n",
|
||||
" a[a == 0] += cte\n",
|
||||
" return np.log(a)\n",
|
||||
" \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"id": "471d9d6b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/tmp/ipykernel_48764/1018405258.py:2: RuntimeWarning: divide by zero encountered in log\n",
|
||||
" np.log(a)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[-1.2039728 , -4.60517019],\n",
|
||||
" [ -inf, 0. ]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 57,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a = np.array([[0.3, 0.01], [0, 1]])\n",
|
||||
"np.log(a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 58,
|
||||
"id": "6c05d356",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[0. 1.]\n",
|
||||
"\n",
|
||||
"dtype\tfloat64\n",
|
||||
"ndim\t1\n",
|
||||
"shape\t(2,)\n",
|
||||
"strides\t(8,)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# This is a view of `a`\n",
|
||||
"b = a[1, :]\n",
|
||||
"print_info(b)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 59,
|
||||
"id": "9d96fb61",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[ -1.2039728 , -4.60517019],\n",
|
||||
" [-23.02585093, 0. ]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 59,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"robust_log(a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 60,
|
||||
"id": "35d0327d",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[3.e-01, 1.e-02],\n",
|
||||
" [1.e-10, 1.e+00]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 60,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 61,
|
||||
"id": "4a2b95c5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([1.e-10, 1.e+00])"
|
||||
]
|
||||
},
|
||||
"execution_count": 61,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"b"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "fa8cf77a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Better to make a copy!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 62,
|
||||
"id": "c5359eac",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def robust_log(a, cte=1e-10):\n",
|
||||
" \"\"\" Returns the log of an array, avoiding troubles when a value is 0.\n",
|
||||
" \n",
|
||||
" Add a tiny constant to the values of `a` so that they are not 0. \n",
|
||||
" `a` is expected to have non-negative values.\n",
|
||||
" \"\"\"\n",
|
||||
" a = a.copy()\n",
|
||||
" a[a == 0] += cte\n",
|
||||
" return np.log(a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 66,
|
||||
"id": "0bf9b2d5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[ -1.2039728 , -4.60517019],\n",
|
||||
" [-23.02585093, 0. ]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 66,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a = np.array([[0.3, 0.01], [0, 1]])\n",
|
||||
"b = a[1, :]\n",
|
||||
"\n",
|
||||
"robust_log(a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 67,
|
||||
"id": "895209ce",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([[0.3 , 0.01],\n",
|
||||
" [0. , 1. ]])"
|
||||
]
|
||||
},
|
||||
"execution_count": 67,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"a"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 68,
|
||||
"id": "18004050",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([0., 1.])"
|
||||
]
|
||||
},
|
||||
"execution_count": 68,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"b"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d664b462",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Copies\n",
|
||||
"\n",
|
||||
"Operations that cannot be executed by changing the metadata create a new memory block, and return a **copy**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 72,
|
||||
"id": "8c8f77e1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 0 1 2 3]\n",
|
||||
" [ 4 5 6 7]\n",
|
||||
" [ 8 9 10 11]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(3, 4)\n",
|
||||
"strides\t(32, 8)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"x = np.arange(12).reshape(3, 4).copy()\n",
|
||||
"print_info(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "716aec53",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Choosing row, columns, or individual elements of an array by giving explicitly their indices (a.k.a \"fancy indexing\") it's an operation that in general cannot be executed by changing the metadata alone.\n",
|
||||
"\n",
|
||||
"Therefore, **fancy indexing always returns a copy**."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 77,
|
||||
"id": "40fb1777",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[0 1]\n",
|
||||
" [4 5]\n",
|
||||
" [8 9]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(3, 2)\n",
|
||||
"strides\t(8, 24)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Get the first and second column\n",
|
||||
"y = x[:, [0, 1]]\n",
|
||||
"print_info(y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 79,
|
||||
"id": "b8ed81d5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[2000 2001]\n",
|
||||
" [2004 2005]\n",
|
||||
" [2008 2009]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(3, 2)\n",
|
||||
"strides\t(8, 24)\n",
|
||||
" \n",
|
||||
"[[ 0 1 2 3]\n",
|
||||
" [ 4 5 6 7]\n",
|
||||
" [ 8 9 10 11]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(3, 4)\n",
|
||||
"strides\t(32, 8)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y += 1000\n",
|
||||
"print_info(y)\n",
|
||||
"# the original array is unchanged => not a view!\n",
|
||||
"print_info(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 80,
|
||||
"id": "6c50e46e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[ 1 0 11]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t1\n",
|
||||
"shape\t(3,)\n",
|
||||
"strides\t(8,)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y = x[[0, 0, 2], [1, 0, 3]]\n",
|
||||
"print_info(y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 81,
|
||||
"id": "9d65a5c3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1001 1000 1011]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t1\n",
|
||||
"shape\t(3,)\n",
|
||||
"strides\t(8,)\n",
|
||||
" \n",
|
||||
"[[ 0 1 2 3]\n",
|
||||
" [ 4 5 6 7]\n",
|
||||
" [ 8 9 10 11]]\n",
|
||||
"\n",
|
||||
"dtype\tint64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(3, 4)\n",
|
||||
"strides\t(32, 8)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y += 1000\n",
|
||||
"print_info(y)\n",
|
||||
"# the original array is unchanged => not a view!\n",
|
||||
"print_info(x)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5e76ea7a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Any operation that computes new values also returns a copy."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 82,
|
||||
"id": "b8a3d44c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[[ 0. 7.1 14.2 21.3]\n",
|
||||
" [28.4 35.5 42.6 49.7]\n",
|
||||
" [56.8 63.9 71. 78.1]]\n",
|
||||
"\n",
|
||||
"dtype\tfloat64\n",
|
||||
"ndim\t2\n",
|
||||
"shape\t(3, 4)\n",
|
||||
"strides\t(32, 8)\n",
|
||||
" \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"y = x * 7.1\n",
|
||||
"print_info(y)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9e50edfd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "022e7b98",
|
||||
"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.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -1,103 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "3ae332a0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "aa7bbab6",
|
||||
"metadata": {
|
||||
"scrolled": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sound_data = np.random.rand(100)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "626eafc7",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([0.66709183, 0.55973494, 0.95416669, 0.60810949, 0.05188879,\n",
|
||||
" 0.58619063, 0.25555136, 0.72451477, 0.2646681 , 0.08694215,\n",
|
||||
" 0.75592186, 0.67261696, 0.62847452, 0.06232598, 0.20549438,\n",
|
||||
" 0.11718457, 0.25184725, 0.48625729, 0.8103058 , 0.18100915,\n",
|
||||
" 0.81113341, 0.62055231, 0.9046905 , 0.56664205, 0.73235338,\n",
|
||||
" 0.74382869, 0.64856368, 0.80644398, 0.46199345, 0.78516632,\n",
|
||||
" 0.91298397, 0.48290914, 0.20847714, 0.99162659, 0.26374781,\n",
|
||||
" 0.3602381 , 0.07173351, 0.8584085 , 0.32248766, 0.39167573,\n",
|
||||
" 0.67944923, 0.00930429, 0.21714217, 0.58810089, 0.17668711,\n",
|
||||
" 0.57444803, 0.25760187, 0.43785728, 0.39119371, 0.68268063,\n",
|
||||
" 0.95954499, 0.45934239, 0.03616905, 0.23896063, 0.61872801,\n",
|
||||
" 0.76332531, 0.96272817, 0.57169277, 0.50225193, 0.01361629,\n",
|
||||
" 0.15357459, 0.8057233 , 0.0642748 , 0.95013941, 0.38712684,\n",
|
||||
" 0.97231498, 0.20261775, 0.74184693, 0.26629893, 0.84672705,\n",
|
||||
" 0.67662718, 0.96055977, 0.64942314, 0.66487937, 0.86867536,\n",
|
||||
" 0.40815661, 0.1139344 , 0.95638066, 0.87436447, 0.18407227,\n",
|
||||
" 0.64457074, 0.19233097, 0.24012179, 0.90399279, 0.39093908,\n",
|
||||
" 0.26389161, 0.97537645, 0.14209784, 0.75261696, 0.10078122,\n",
|
||||
" 0.87468408, 0.77990102, 0.92983283, 0.45841805, 0.61470669,\n",
|
||||
" 0.87939755, 0.09266009, 0.41177209, 0.46973971, 0.43152144])"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sound_data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "ef55bee9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"synonyms = {\n",
|
||||
" 'hot': ['blazing', 'boiling', 'heated'],\n",
|
||||
" 'airplane': ['aircraft', 'airliner', \n",
|
||||
" 'cab', 'jet', 'plane'],\n",
|
||||
" 'beach': ['coast', 'shore', 'waterfront'],\n",
|
||||
" # ...\n",
|
||||
"}"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue