From 54163b4a58e462bdfdc986644643b690cd7ae2e6 Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Thu, 10 Mar 2022 01:13:40 -0800 Subject: [PATCH 1/2] solution to FibonacciIterator with maximum parameter n --- 00_python/iterators.ipynb | 56 ++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/00_python/iterators.ipynb b/00_python/iterators.ipynb index d3c32ce..d4de17a 100644 --- a/00_python/iterators.ipynb +++ b/00_python/iterators.ipynb @@ -469,10 +469,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "1\n", + "2\n", + "3\n", + "5\n", + "8\n", + "13\n", + "21\n", + "34\n", + "55\n", + "89\n", + "144\n", + "233\n", + "377\n", + "610\n", + "987\n" + ] + } + ], + "source": [ + "\n", + "class FibonacciIterator_n(object):\n", + " def __init__(self, n):\n", + " self.a = 0 # current number\n", + " self.b = 1 # next number\n", + " self.n = n # the maximum parameter \n", + " \n", + " def __iter__(self):\n", + " return self\n", + " \n", + " def __next__(self):\n", + " if self.a >= self.n:\n", + " raise StopIteration # sigals that we went over the maximum parameter \n", + " ret = self.a\n", + " self.a, self.b = self.b, self.a + self.b # advance the iteration\n", + " return ret\n", + " \n", + "for i in FibonacciIterator_n(1000):\n", + " print(i)" + ] }, { "cell_type": "markdown", @@ -1282,9 +1326,9 @@ "metadata": { "celltoolbar": "Tags", "kernelspec": { - "display_name": "Python (pycourse)", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "pycourse" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -1296,7 +1340,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.1" } }, "nbformat": 4, From 8bc3843ddba361854c54a998adbf7210ff3148bc Mon Sep 17 00:00:00 2001 From: Yilin Sun Date: Thu, 10 Mar 2022 02:19:29 -0800 Subject: [PATCH 2/2] potential solution to the exercise on pdist --- 02_linear_algebra/blas_lapack.ipynb | 109 ++++++++++------- 02_linear_algebra/convolutions.ipynb | 6 +- 02_linear_algebra/linearoperators.ipynb | 6 +- 02_linear_algebra/pytorch.ipynb | 23 ++-- 04_functions/ode_initial.ipynb | 13 +- 05_graphs/graphs.ipynb | 6 +- 08_geometry/distances.ipynb | 152 ++++++++++++++++++++++-- 7 files changed, 240 insertions(+), 75 deletions(-) diff --git a/02_linear_algebra/blas_lapack.ipynb b/02_linear_algebra/blas_lapack.ipynb index 5ebc227..016a8dc 100644 --- a/02_linear_algebra/blas_lapack.ipynb +++ b/02_linear_algebra/blas_lapack.ipynb @@ -45,27 +45,36 @@ "name": "stdout", "output_type": "stream", "text": [ + "%pylab is deprecated, use %matplotlib inline and import the required libraries.\n", "Populating the interactive namespace from numpy and matplotlib\n", - "blas_mkl_info:\n", - " libraries = ['mkl_rt', 'pthread']\n", - " library_dirs = ['/home/brad/miniconda3/envs/pycourse/lib']\n", - " define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n", - " include_dirs = ['/home/brad/miniconda3/envs/pycourse/include']\n", - "blas_opt_info:\n", - " libraries = ['mkl_rt', 'pthread']\n", - " library_dirs = ['/home/brad/miniconda3/envs/pycourse/lib']\n", - " define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n", - " include_dirs = ['/home/brad/miniconda3/envs/pycourse/include']\n", - "lapack_mkl_info:\n", - " libraries = ['mkl_rt', 'pthread']\n", - " library_dirs = ['/home/brad/miniconda3/envs/pycourse/lib']\n", - " define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n", - " include_dirs = ['/home/brad/miniconda3/envs/pycourse/include']\n", - "lapack_opt_info:\n", - " libraries = ['mkl_rt', 'pthread']\n", - " library_dirs = ['/home/brad/miniconda3/envs/pycourse/lib']\n", - " define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n", - " include_dirs = ['/home/brad/miniconda3/envs/pycourse/include']\n" + "openblas64__info:\n", + " libraries = ['openblas64_', 'openblas64_']\n", + " library_dirs = ['/usr/local/lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]\n", + " runtime_library_dirs = ['/usr/local/lib']\n", + "blas_ilp64_opt_info:\n", + " libraries = ['openblas64_', 'openblas64_']\n", + " library_dirs = ['/usr/local/lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]\n", + " runtime_library_dirs = ['/usr/local/lib']\n", + "openblas64__lapack_info:\n", + " libraries = ['openblas64_', 'openblas64_']\n", + " library_dirs = ['/usr/local/lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]\n", + " runtime_library_dirs = ['/usr/local/lib']\n", + "lapack_ilp64_opt_info:\n", + " libraries = ['openblas64_', 'openblas64_']\n", + " library_dirs = ['/usr/local/lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]\n", + " runtime_library_dirs = ['/usr/local/lib']\n", + "Supported SIMD extensions in this NumPy install:\n", + " baseline = SSE,SSE2,SSE3\n", + " found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2\n", + " not found = AVX512F,AVX512CD,AVX512_KNL,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL\n" ] } ], @@ -101,25 +110,35 @@ "output_type": "stream", "text": [ "lapack_mkl_info:\n", - " libraries = ['mkl_rt', 'pthread']\n", - " library_dirs = ['/home/brad/miniconda3/envs/pycourse/lib']\n", - " define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n", - " include_dirs = ['/home/brad/miniconda3/envs/pycourse/include']\n", + " NOT AVAILABLE\n", + "openblas_lapack_info:\n", + " libraries = ['openblas', 'openblas']\n", + " library_dirs = ['/opt/arm64-builds/lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None)]\n", + " runtime_library_dirs = ['/opt/arm64-builds/lib']\n", "lapack_opt_info:\n", - " libraries = ['mkl_rt', 'pthread']\n", - " library_dirs = ['/home/brad/miniconda3/envs/pycourse/lib']\n", - " define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n", - " include_dirs = ['/home/brad/miniconda3/envs/pycourse/include']\n", + " libraries = ['openblas', 'openblas']\n", + " library_dirs = ['/opt/arm64-builds/lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None)]\n", + " runtime_library_dirs = ['/opt/arm64-builds/lib']\n", "blas_mkl_info:\n", - " libraries = ['mkl_rt', 'pthread']\n", - " library_dirs = ['/home/brad/miniconda3/envs/pycourse/lib']\n", - " define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n", - " include_dirs = ['/home/brad/miniconda3/envs/pycourse/include']\n", + " NOT AVAILABLE\n", + "blis_info:\n", + " NOT AVAILABLE\n", + "openblas_info:\n", + " libraries = ['openblas', 'openblas']\n", + " library_dirs = ['/opt/arm64-builds/lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None)]\n", + " runtime_library_dirs = ['/opt/arm64-builds/lib']\n", "blas_opt_info:\n", - " libraries = ['mkl_rt', 'pthread']\n", - " library_dirs = ['/home/brad/miniconda3/envs/pycourse/lib']\n", - " define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n", - " include_dirs = ['/home/brad/miniconda3/envs/pycourse/include']\n" + " libraries = ['openblas', 'openblas']\n", + " library_dirs = ['/opt/arm64-builds/lib']\n", + " language = c\n", + " define_macros = [('HAVE_CBLAS', None)]\n", + " runtime_library_dirs = ['/opt/arm64-builds/lib']\n" ] } ], @@ -148,7 +167,7 @@ { "data": { "text/plain": [ - "6.53530223265452e-12" + "0.0" ] }, "execution_count": 3, @@ -178,7 +197,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "1.87 s ± 90.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" + "2.3 s ± 567 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], @@ -195,7 +214,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "1.62 s ± 74.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" + "2.01 s ± 245 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], @@ -450,7 +469,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -655,19 +674,19 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ - "## Your code here\n" + "## Your code here" ] } ], "metadata": { "kernelspec": { - "display_name": "Python (pycourse)", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "pycourse" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -679,7 +698,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/02_linear_algebra/convolutions.ipynb b/02_linear_algebra/convolutions.ipynb index 9bf7ab1..aadf009 100644 --- a/02_linear_algebra/convolutions.ipynb +++ b/02_linear_algebra/convolutions.ipynb @@ -899,9 +899,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python (pycourse)", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "pycourse" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -913,7 +913,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/02_linear_algebra/linearoperators.ipynb b/02_linear_algebra/linearoperators.ipynb index bce1dd3..c64b308 100644 --- a/02_linear_algebra/linearoperators.ipynb +++ b/02_linear_algebra/linearoperators.ipynb @@ -657,9 +657,9 @@ "metadata": { "celltoolbar": "Tags", "kernelspec": { - "display_name": "Python (pycourse)", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "pycourse" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -671,7 +671,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/02_linear_algebra/pytorch.ipynb b/02_linear_algebra/pytorch.ipynb index 0be3ba3..45cf354 100644 --- a/02_linear_algebra/pytorch.ipynb +++ b/02_linear_algebra/pytorch.ipynb @@ -25,14 +25,15 @@ "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'1.7.0'" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" + "ename": "ModuleNotFoundError", + "evalue": "No module named 'torch'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", + "Input \u001b[0;32mIn [1]\u001b[0m, in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mnp\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtorch\u001b[39;00m\n\u001b[1;32m 4\u001b[0m torch\u001b[38;5;241m.\u001b[39m__version__\n", + "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'torch'" + ] } ], "source": [ @@ -472,9 +473,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python (pycourse)", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "pycourse" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -486,7 +487,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/04_functions/ode_initial.ipynb b/04_functions/ode_initial.ipynb index 2cc8fc7..4b64a3b 100644 --- a/04_functions/ode_initial.ipynb +++ b/04_functions/ode_initial.ipynb @@ -295,6 +295,13 @@ "## Your code here\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -928,9 +935,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python (pycourse)", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "pycourse" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -942,7 +949,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/05_graphs/graphs.ipynb b/05_graphs/graphs.ipynb index 39d20da..418c8df 100644 --- a/05_graphs/graphs.ipynb +++ b/05_graphs/graphs.ipynb @@ -331,9 +331,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python (pycourse)", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "pycourse" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -345,7 +345,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.1" } }, "nbformat": 4, diff --git a/08_geometry/distances.ipynb b/08_geometry/distances.ipynb index ea3db5f..60014fe 100644 --- a/08_geometry/distances.ipynb +++ b/08_geometry/distances.ipynb @@ -15,7 +15,15 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + ":241: RuntimeWarning: scipy._lib.messagestream.MessageStream size changed, may indicate binary incompatibility. Expected 56 from C header, got 64 from PyObject\n" + ] + } + ], "source": [ "import numpy as np\n", "import scipy.linalg as la\n", @@ -1010,19 +1018,149 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "205 µs ± 11.6 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n" + ] + } + ], "source": [ - "## Your code here\n" + "## Your code here\n", + "\n", + "X = np.random.randn(100,50)\n", + "%timeit D = distance.pdist(X, 'braycurtis')" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "272 µs ± 11.9 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n" + ] + } + ], + "source": [ + "%timeit D = distance.pdist(X, 'canberra')" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "129 µs ± 7.54 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n" + ] + } + ], + "source": [ + "%timeit D = distance.pdist(X, 'chebyshev')" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "154 µs ± 3.83 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n" + ] + } + ], + "source": [ + "%timeit D = distance.pdist(X, 'cityblock')" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "237 µs ± 15.4 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n" + ] + } + ], + "source": [ + "%timeit D = distance.pdist(X, 'correlation')" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "227 µs ± 14.5 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)\n" + ] + } + ], + "source": [ + "%timeit D = distance.pdist(X, 'cosine')" ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "116 µs ± 11.7 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n" + ] + } + ], + "source": [ + "%timeit D = distance.pdist(X, 'euclidean')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Quick observation: \n", + "\n", + "After running pdist on a random 100 * 50 matrix for all the different metrics, it seems that `euclidean`, `cityblock`, \n", + "and `chebyshev` took shorter time to compute. The metric `canberra` took the longest time. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python (pycourse)", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "pycourse" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -1034,7 +1172,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.10.1" } }, "nbformat": 4,