diff --git a/10_Random_sampling_Solutions.ipynb b/10_Random_sampling_Solutions.ipynb
index 1cc4b87..b04efd6 100644
--- a/10_Random_sampling_Solutions.ipynb
+++ b/10_Random_sampling_Solutions.ipynb
@@ -1,337 +1 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Random Sampling"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1.11.2'"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.__version__"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "__author__ = 'kyubyong. longinglove@nate.com'"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Simple random data"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q1. Create an array of shape (3, 2) and populate it with random samples from a uniform distribution over [0, 1)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 0.13879034, 0.71300174],\n",
- " [ 0.08121322, 0.00393554],\n",
- " [ 0.02349471, 0.56677474]])"
- ]
- },
- "execution_count": 49,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.random.rand(3, 2) \n",
- "# Or np.random.random((3,2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q2. Create an array of shape (1000, 1000) and populate it with random samples from a standard normal distribution. And verify that the mean and standard deviation is close enough to 0 and 1 repectively."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "-0.00110028519551\n",
- "0.999683483393\n"
- ]
- }
- ],
- "source": [
- "out1 = np.random.randn(1000, 1000)\n",
- "out2 = np.random.standard_normal((1000, 1000))\n",
- "out3 = np.random.normal(loc=0.0, scale=1.0, size=(1000, 1000))\n",
- "assert np.allclose(np.mean(out1), np.mean(out2), atol=0.1)\n",
- "assert np.allclose(np.mean(out1), np.mean(out3), atol=0.1)\n",
- "assert np.allclose(np.std(out1), np.std(out2), atol=0.1)\n",
- "assert np.allclose(np.std(out1), np.std(out3), atol=0.1)\n",
- "print np.mean(out3)\n",
- "print np.std(out1)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q3. Create an array of shape (3, 2) and populate it with random integers ranging from 0 to 3 (inclusive) from a discrete uniform distribution."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[1, 3],\n",
- " [3, 0],\n",
- " [0, 0]])"
- ]
- },
- "execution_count": 44,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.random.randint(0, 4, (3, 2))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q4. Extract 1 elements from x randomly such that each of them would be associated with probabilities .3, .5, .2. Then print the result 10 times."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 58,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 60,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2 out of 10\n",
- "5 out of 10\n",
- "3 out of 10\n",
- "5 out of 10\n",
- "5 out of 10\n",
- "5 out of 10\n",
- "2 out of 10\n",
- "2 out of 10\n",
- "5 out of 10\n",
- "5 out of 10\n"
- ]
- }
- ],
- "source": [
- "for _ in range(10):\n",
- " print np.random.choice(x, p=[.3, .5, .2])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q5. Extract 3 different integers from 0 to 9 randomly with the same probabilities."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 66,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([5, 4, 0])"
- ]
- },
- "execution_count": 66,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.random.choice(10, 3, replace=False)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Permutations"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q6. Shuffle numbers between 0 and 9 (inclusive)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 86,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2 3 8 4 5 1 0 6 9 7]\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(10)\n",
- "np.random.shuffle(x)\n",
- "print x"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 88,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[5 2 7 4 1 0 6 8 9 3]\n"
- ]
- }
- ],
- "source": [
- "# Or\n",
- "print np.random.permutation(10)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Random generator"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q7. Assign number 10 to the seed of the random generator so that you can get the same value next time."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 91,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "np.random.seed(10)"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.10"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
+{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Random Sampling"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":"import numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"'1.16.5'"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"np.__version__"},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[],"source":"__author__ = 'kyubyong. longinglove@nate.com'"},{"cell_type":"markdown","metadata":{},"source":["## Simple random data"]},{"cell_type":"markdown","metadata":{},"source":["Q1. Create an array of shape (3, 2) and populate it with random samples from a uniform distribution over [0, 1)."]},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"data":{"text/plain":"array([[0.91619879, 0.84402355],\n [0.96187101, 0.63032521],\n [0.87581479, 0.49324904]])"},"execution_count":4,"metadata":{},"output_type":"execute_result"}],"source":"np.random.rand(3, 2) \n# Or np.random.random((3,2))"},{"cell_type":"markdown","metadata":{},"source":["Q2. Create an array of shape (1000, 1000) and populate it with random samples from a standard normal distribution. And verify that the mean and standard deviation is close enough to 0 and 1 repectively."]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"-0.0007331580720093552\n1.0006913189352846\n"}],"source":"out1 = np.random.randn(1000, 1000)\nout2 = np.random.standard_normal((1000, 1000))\nout3 = np.random.normal(loc=0.0, scale=1.0, size=(1000, 1000))\nassert np.allclose(np.mean(out1), np.mean(out2), atol=0.1)\nassert np.allclose(np.mean(out1), np.mean(out3), atol=0.1)\nassert np.allclose(np.std(out1), np.std(out2), atol=0.1)\nassert np.allclose(np.std(out1), np.std(out3), atol=0.1)\nprint (np.mean(out3))\nprint (np.std(out1))\n"},{"cell_type":"markdown","metadata":{},"source":["Q3. Create an array of shape (3, 2) and populate it with random integers ranging from 0 to 3 (inclusive) from a discrete uniform distribution."]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"data":{"text/plain":"array([[2, 0],\n [3, 1],\n [2, 0]])"},"execution_count":6,"metadata":{},"output_type":"execute_result"}],"source":"np.random.randint(0, 4, (3, 2))"},{"cell_type":"markdown","metadata":{},"source":["Q4. Extract 1 elements from x randomly such that each of them would be associated with probabilities .3, .5, .2. Then print the result 10 times."]},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[],"source":"x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']"},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"b'3 out of 10'\nb'5 out of 10'\nb'3 out of 10'\nb'5 out of 10'\nb'2 out of 10'\nb'5 out of 10'\nb'2 out of 10'\nb'5 out of 10'\nb'5 out of 10'\nb'5 out of 10'\n"}],"source":"for _ in range(10):\n print (np.random.choice(x, p=[.3, .5, .2]))"},{"cell_type":"markdown","metadata":{},"source":["Q5. Extract 3 different integers from 0 to 9 randomly with the same probabilities."]},{"cell_type":"code","execution_count":10,"metadata":{},"outputs":[{"data":{"text/plain":"array([5, 8, 3])"},"execution_count":10,"metadata":{},"output_type":"execute_result"}],"source":"np.random.choice(10, 3, replace=False)"},{"cell_type":"markdown","metadata":{},"source":["## Permutations"]},{"cell_type":"markdown","metadata":{},"source":["Q6. Shuffle numbers between 0 and 9 (inclusive)."]},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0 8 9 5 7 4 2 6 1 3]\n"}],"source":"x = np.arange(10)\nnp.random.shuffle(x)\nprint (x)"},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[9 6 3 1 5 2 0 8 7 4]\n"}],"source":"# Or\nprint (np.random.permutation(10))"},{"cell_type":"markdown","metadata":{},"source":["## Random generator"]},{"cell_type":"markdown","metadata":{},"source":["Q7. Assign number 10 to the seed of the random generator so that you can get the same value next time."]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[],"source":"np.random.seed(10)"},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":""}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
\ No newline at end of file
diff --git a/11_Set_routines_Solutions.ipynb b/11_Set_routines_Solutions.ipynb
index 6565478..6976a3f 100644
--- a/11_Set_routines_Solutions.ipynb
+++ b/11_Set_routines_Solutions.ipynb
@@ -1,277 +1 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Set routines"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1.11.2'"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.__version__"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "author = 'kyubyong. longinglove@nate.com'"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Making proper sets"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q1. Get unique elements and reconstruction indices from x. And reconstruct x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "unique elements = [1 2 3 4 6]\n",
- "reconstruction indices = [0 1 4 3 1 2 1]\n",
- "reconstructed = [1 2 6 4 2 3 2]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, 2, 6, 4, 2, 3, 2])\n",
- "out, indices = np.unique(x, return_inverse=True)\n",
- "print \"unique elements =\", out\n",
- "print \"reconstruction indices =\", indices\n",
- "print \"reconstructed =\", out[indices]\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Boolean operations"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q2. Create a boolean array of the same shape as x. If each element of x is present in y, the result will be True, otherwise False."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ True True False False True]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([0, 1, 2, 5, 0])\n",
- "y = np.array([0, 1])\n",
- "print np.in1d(x, y)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q3. Find the unique intersection of x and y."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0 1]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([0, 1, 2, 5, 0])\n",
- "y = np.array([0, 1, 4])\n",
- "print np.intersect1d(x, y)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q4. Find the unique elements of x that are not present in y."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2 5]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([0, 1, 2, 5, 0])\n",
- "y = np.array([0, 1, 4])\n",
- "print np.setdiff1d(x, y)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q5. Find the xor elements of x and y."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2 4 5]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([0, 1, 2, 5, 0])\n",
- "y = np.array([0, 1, 4])\n",
- "out1 = np.setxor1d(x, y)\n",
- "out2 = np.sort(np.concatenate((np.setdiff1d(x, y), np.setdiff1d(y, x))))\n",
- "assert np.allclose(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q6. Find the union of x and y."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0 1 2 4 5]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([0, 1, 2, 5, 0])\n",
- "y = np.array([0, 1, 4])\n",
- "out1 = np.union1d(x, y)\n",
- "out2 = np.sort(np.unique(np.concatenate((x, y))))\n",
- "assert np.allclose(out1, out2)\n",
- "print np.union1d(x, y)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.10"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
+{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Set routines"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":"import numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"'1.16.5'"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"np.__version__"},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[],"source":"author = 'kyubyong. longinglove@nate.com'"},{"cell_type":"markdown","metadata":{},"source":["## Making proper sets"]},{"cell_type":"markdown","metadata":{},"source":["Q1. Get unique elements and reconstruction indices from x. And reconstruct x."]},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"unique elements = [1 2 3 4 6]\nreconstruction indices = [0 1 4 3 1 2 1]\nreconstructed = [1 2 6 4 2 3 2]\n"}],"source":"x = np.array([1, 2, 6, 4, 2, 3, 2])\nout, indices = np.unique(x, return_inverse=True)\nprint (\"unique elements =\", out)\nprint (\"reconstruction indices =\", indices)\nprint (\"reconstructed =\", out[indices])\n"},{"cell_type":"markdown","metadata":{},"source":["## Boolean operations"]},{"cell_type":"markdown","metadata":{},"source":["Q2. Create a boolean array of the same shape as x. If each element of x is present in y, the result will be True, otherwise False."]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ True True False False True]\n"}],"source":"x = np.array([0, 1, 2, 5, 0])\ny = np.array([0, 1])\nprint (np.in1d(x, y))"},{"cell_type":"markdown","metadata":{},"source":["Q3. Find the unique intersection of x and y."]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0 1]\n"}],"source":"x = np.array([0, 1, 2, 5, 0])\ny = np.array([0, 1, 4])\nprint (np.intersect1d(x, y))"},{"cell_type":"markdown","metadata":{},"source":["Q4. Find the unique elements of x that are not present in y."]},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[2 5]\n"}],"source":"x = np.array([0, 1, 2, 5, 0])\ny = np.array([0, 1, 4])\nprint (np.setdiff1d(x, y))"},{"cell_type":"markdown","metadata":{},"source":["Q5. Find the xor elements of x and y."]},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[2 4 5]\n"}],"source":"x = np.array([0, 1, 2, 5, 0])\ny = np.array([0, 1, 4])\nout1 = np.setxor1d(x, y)\nout2 = np.sort(np.concatenate((np.setdiff1d(x, y), np.setdiff1d(y, x))))\nassert np.allclose(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q6. Find the union of x and y."]},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0 1 2 4 5]\n"}],"source":"x = np.array([0, 1, 2, 5, 0])\ny = np.array([0, 1, 4])\nout1 = np.union1d(x, y)\nout2 = np.sort(np.unique(np.concatenate((x, y))))\nassert np.allclose(out1, out2)\nprint (np.union1d(x, y))"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":""}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
\ No newline at end of file
diff --git a/12_Sorting_searching_and_counting_Solutions.ipynb b/12_Sorting_searching_and_counting_Solutions.ipynb
index b26aab0..9238561 100644
--- a/12_Sorting_searching_and_counting_Solutions.ipynb
+++ b/12_Sorting_searching_and_counting_Solutions.ipynb
@@ -1,453 +1 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Soring, searching, and counting"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1.11.2'"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.__version__"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "author = 'kyubyong. longinglove@nate.com'"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Sorting"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q1. Sort x along the second axis."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[1 4]\n",
- " [1 3]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1,4],[3,1]])\n",
- "out = np.sort(x, axis=1)\n",
- "x.sort(axis=1)\n",
- "assert np.array_equal(out, x)\n",
- "print out"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q2. Sort pairs of surnames and first names and return their indices. (first by surname, then by name)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1 2 0]\n"
- ]
- }
- ],
- "source": [
- "surnames = ('Hertz', 'Galilei', 'Hertz')\n",
- "first_names = ('Heinrich', 'Galileo', 'Gustav')\n",
- "print np.lexsort((first_names, surnames))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q3. Get the indices that would sort x along the second axis."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[0 1]\n",
- " [1 0]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1,4],[3,1]])\n",
- "out = np.argsort(x, axis=1)\n",
- "print out"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q4. Create an array such that its fifth element would be the same as the element of sorted x, and it divide other elements by their value."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "x = [5 1 6 3 9 8 2 7 4 0]\n",
- "\n",
- "Check the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\n",
- "[2 0 4 3 1 5 8 7 6 9]\n"
- ]
- }
- ],
- "source": [
- "x = np.random.permutation(10)\n",
- "print \"x =\", x\n",
- "print \"\\nCheck the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\\n\", \n",
- "out = np.partition(x, 5)\n",
- "x.partition(5) # in-place equivalent\n",
- "assert np.array_equal(x, out)\n",
- "print out\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q5. Create the indices of an array such that its third element would be the same as the element of sorted x, and it divide other elements by their value."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 56,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "x = [2 8 3 7 5 6 4 0 9 1]\n",
- "partitioned = [0 1 2 3 4 5 8 6 9 7]\n",
- "indices = [0 1 2 3 4 5 8 6 9 7]\n"
- ]
- }
- ],
- "source": [
- "x = np.random.permutation(10)\n",
- "print \"x =\", x\n",
- "partitioned = np.partition(x, 3)\n",
- "indices = np.argpartition(x, 3)\n",
- "print \"partitioned =\", partitioned\n",
- "print \"indices =\", partitioned\n",
- "assert np.array_equiv(x[indices], partitioned)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Searching"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q6. Get the maximum and minimum values and their indices of x along the second axis."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 78,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "x = [[0 5 9 8 2]\n",
- " [3 7 4 1 6]]\n",
- "maximum values = [9 7]\n",
- "max indices = [2 1]\n",
- "minimum values = [0 1]\n",
- "min indices = [0 3]\n"
- ]
- }
- ],
- "source": [
- "x = np.random.permutation(10).reshape(2, 5)\n",
- "print \"x =\", x\n",
- "print \"maximum values =\", np.max(x, 1)\n",
- "print \"max indices =\", np.argmax(x, 1)\n",
- "print \"minimum values =\", np.min(x, 1)\n",
- "print \"min indices =\", np.argmin(x, 1)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q7. Get the maximum and minimum values and their indices of x along the second axis, ignoring NaNs."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 79,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "maximum values ignoring NaNs = [ 4. 3.]\n",
- "max indices = [1 0]\n",
- "minimum values ignoring NaNs = [ 4. 2.]\n",
- "min indices = [1 1]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[np.nan, 4], [3, 2]])\n",
- "print \"maximum values ignoring NaNs =\", np.nanmax(x, 1)\n",
- "print \"max indices =\", np.nanargmax(x, 1)\n",
- "print \"minimum values ignoring NaNs =\", np.nanmin(x, 1)\n",
- "print \"min indices =\", np.nanargmin(x, 1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q8. Get the values and indices of the elements that are bigger than 2 in x.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 113,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Values bigger than 2 = [3 3 5]\n",
- "Their indices are (array([0, 1, 1], dtype=int64), array([2, 1, 2], dtype=int64))\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 2, 3], [1, 3, 5]])\n",
- "print \"Values bigger than 2 =\", x[x>2]\n",
- "print \"Their indices are \", np.nonzero(x > 2)\n",
- "assert np.array_equiv(x[x>2], x[np.nonzero(x > 2)])\n",
- "assert np.array_equiv(x[x>2], np.extract(x > 2, x))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q9. Get the indices of the elements that are bigger than 2 in the flattend x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "scrolled": true
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[2 4 5]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 2, 3], [1, 3, 5]])\n",
- "print np.flatnonzero(x>2)\n",
- "assert np.array_equiv(np.flatnonzero(x), x.ravel().nonzero())"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q10. Check the elements of x and return 0 if it is less than 0, otherwise the element itself."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 105,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[0 0 0]\n",
- " [0 0 0]\n",
- " [1 2 3]]\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(-5, 4).reshape(3, 3)\n",
- "print np.where(x <0, 0, x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q11. Get the indices where elements of y should be inserted to x to maintain order."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 109,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([0, 2, 1, 3], dtype=int64)"
- ]
- },
- "execution_count": 109,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x = [1, 3, 5, 7, 9]\n",
- "y = [0, 4, 2, 6]\n",
- "np.searchsorted(x, y)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Counting"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q12. Get the number of nonzero elements in x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 120,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "5\n"
- ]
- }
- ],
- "source": [
- "x = [[0,1,7,0,0],[3,0,0,2,19]]\n",
- "print np.count_nonzero(x)\n",
- "assert np.count_nonzero(x) == len(x[x!=0])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "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.5.2"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
-}
+{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Soring, searching, and counting"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":"import numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"'1.16.5'"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"np.__version__"},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[],"source":"author = 'kyubyong. longinglove@nate.com'"},{"cell_type":"markdown","metadata":{},"source":["## Sorting"]},{"cell_type":"markdown","metadata":{},"source":["Q1. Sort x along the second axis."]},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[1 4]\n [1 3]]\n"}],"source":"x = np.array([[1,4],[3,1]])\nout = np.sort(x, axis=1)\nx.sort(axis=1)\nassert np.array_equal(out, x)\nprint (out)"},{"cell_type":"markdown","metadata":{},"source":["Q2. Sort pairs of surnames and first names and return their indices. (first by surname, then by name)."]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1 2 0]\n"}],"source":"surnames = ('Hertz', 'Galilei', 'Hertz')\nfirst_names = ('Heinrich', 'Galileo', 'Gustav')\nprint (np.lexsort((first_names, surnames)))"},{"cell_type":"markdown","metadata":{},"source":["Q3. Get the indices that would sort x along the second axis."]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[0 1]\n [1 0]]\n"}],"source":"x = np.array([[1,4],[3,1]])\nout = np.argsort(x, axis=1)\nprint (out)"},{"cell_type":"markdown","metadata":{},"source":["Q4. Create an array such that its fifth element would be the same as the element of sorted x, and it divide other elements by their value."]},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"x = [1 9 7 3 0 4 5 6 2 8]\n\nCheck the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\n\n[0 1 3 2 4 5 6 7 9 8]\n"}],"source":"x = np.random.permutation(10)\nprint (\"x =\", x)\nprint (\"\\nCheck the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\\n\", )\nout = np.partition(x, 5)\nx.partition(5) # in-place equivalent\nassert np.array_equal(x, out)\nprint (out)\n"},{"cell_type":"markdown","metadata":{},"source":["Q5. Create the indices of an array such that its third element would be the same as the element of sorted x, and it divide other elements by their value."]},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"x = [9 5 1 4 6 3 0 8 2 7]\npartitioned = [1 0 2 3 4 5 6 7 8 9]\nindices = [2 6 8 5 3 1 4 9 7 0]\n"}],"source":"x = np.random.permutation(10)\nprint (\"x =\", x)\npartitioned = np.partition(x, 3)\nindices = np.argpartition(x, 3)\nprint (\"partitioned =\", partitioned)\nprint (\"indices =\", indices)\nassert np.array_equiv(x[indices], partitioned)"},{"cell_type":"markdown","metadata":{},"source":["## Searching"]},{"cell_type":"markdown","metadata":{},"source":["Q6. Get the maximum and minimum values and their indices of x along the second axis."]},{"cell_type":"code","execution_count":78,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["x = [[0 5 9 8 2]\n"," [3 7 4 1 6]]\n","maximum values = [9 7]\n","max indices = [2 1]\n","minimum values = [0 1]\n","min indices = [0 3]\n"]}],"source":"x = np.random.permutation(10).reshape(2, 5)\nprint (\"x =\", x)\nprint (\"maximum values =\", np.max(x, 1))\nprint (\"max indices =\", np.argmax(x, 1))\nprint (\"minimum values =\", np.min(x, 1))\nprint (\"min indices =\", np.argmin(x, 1))\n"},{"cell_type":"markdown","metadata":{},"source":["Q7. Get the maximum and minimum values and their indices of x along the second axis, ignoring NaNs."]},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"maximum values ignoring NaNs = [4. 3.]\nmax indices = [1 0]\nminimum values ignoring NaNs = [4. 2.]\nmin indices = [1 1]\n"}],"source":"x = np.array([[np.nan, 4], [3, 2]])\nprint (\"maximum values ignoring NaNs =\", np.nanmax(x, 1))\nprint (\"max indices =\", np.nanargmax(x, 1))\nprint (\"minimum values ignoring NaNs =\", np.nanmin(x, 1))\nprint (\"min indices =\", np.nanargmin(x, 1))"},{"cell_type":"markdown","metadata":{},"source":["Q8. Get the values and indices of the elements that are bigger than 2 in x.\n"]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"Values bigger than 2 = [3 3 5]\nTheir indices are (array([0, 1, 1], dtype=int64), array([2, 1, 2], dtype=int64))\n"}],"source":"x = np.array([[1, 2, 3], [1, 3, 5]])\nprint (\"Values bigger than 2 =\", x[x>2])\nprint (\"Their indices are \", np.nonzero(x > 2))\nassert np.array_equiv(x[x>2], x[np.nonzero(x > 2)])\nassert np.array_equiv(x[x>2], np.extract(x > 2, x))"},{"cell_type":"markdown","metadata":{},"source":["Q9. Get the indices of the elements that are bigger than 2 in the flattend x."]},{"cell_type":"code","execution_count":14,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[2 4 5]\n"}],"source":"x = np.array([[1, 2, 3], [1, 3, 5]])\nprint (np.flatnonzero(x>2))\nassert np.array_equiv(np.flatnonzero(x), x.ravel().nonzero())"},{"cell_type":"markdown","metadata":{},"source":["Q10. Check the elements of x and return 0 if it is less than 0, otherwise the element itself."]},{"cell_type":"code","execution_count":15,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[0 0 0]\n [0 0 0]\n [1 2 3]]\n"}],"source":"x = np.arange(-5, 4).reshape(3, 3)\nprint (np.where(x <0, 0, x))"},{"cell_type":"markdown","metadata":{},"source":["Q11. Get the indices where elements of y should be inserted to x to maintain order."]},{"cell_type":"code","execution_count":16,"metadata":{},"outputs":[{"data":{"text/plain":"array([0, 2, 1, 3], dtype=int64)"},"execution_count":16,"metadata":{},"output_type":"execute_result"}],"source":"x = [1, 3, 5, 7, 9]\ny = [0, 4, 2, 6]\nnp.searchsorted(x, y)"},{"cell_type":"markdown","metadata":{},"source":["## Counting"]},{"cell_type":"markdown","metadata":{},"source":["Q12. Get the number of nonzero elements in x."]},{"cell_type":"code","execution_count":17,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"5\n"}],"source":"x = [[0,1,7,0,0],[3,0,0,2,19]]\nprint (np.count_nonzero(x))\nassert np.count_nonzero(x) == len(x[x!=0])"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":""}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
\ No newline at end of file
diff --git a/1_Array_creation_routines_Solution.ipynb b/1_Array_creation_routines_Solution.ipynb
index fb1ecfd..05c7852 100644
--- a/1_Array_creation_routines_Solution.ipynb
+++ b/1_Array_creation_routines_Solution.ipynb
@@ -1,949 +1 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Array creation routines"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Ones and zeros"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create a new array of 2*2 integers, without initializing entries."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[0, 0],\n",
- " [0, 0]])"
- ]
- },
- "execution_count": 27,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.empty([2,2], int)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let X = np.array([1,2,3], [4,5,6], np.int32). \n",
- "Create a new array with the same shape and type as X."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[1, 2, 3],\n",
- " [4, 5, 6]])"
- ]
- },
- "execution_count": 32,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "X = np.array([[1,2,3], [4,5,6]], np.int32)\n",
- "np.empty_like(X)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create a 3-D array with ones on the diagonal and zeros elsewhere."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 1., 0., 0.],\n",
- " [ 0., 1., 0.],\n",
- " [ 0., 0., 1.]])"
- ]
- },
- "execution_count": 33,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.eye(3)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 1., 0., 0.],\n",
- " [ 0., 1., 0.],\n",
- " [ 0., 0., 1.]])"
- ]
- },
- "execution_count": 35,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.identity(3)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create a new array of 3*2 float numbers, filled with ones."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 1., 1.],\n",
- " [ 1., 1.],\n",
- " [ 1., 1.]])"
- ]
- },
- "execution_count": 36,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.ones([3,2], float)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let x = np.arange(4, dtype=np.int64). Create an array of ones with the same shape and type as X."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 59,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([1, 1, 1, 1], dtype=int64)"
- ]
- },
- "execution_count": 59,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x = np.arange(4, dtype=np.int64)\n",
- "np.ones_like(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create a new array of 3*2 float numbers, filled with zeros."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 45,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 0., 0.],\n",
- " [ 0., 0.],\n",
- " [ 0., 0.]])"
- ]
- },
- "execution_count": 45,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.zeros((3,2), float)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let x = np.arange(4, dtype=np.int64). Create an array of zeros with the same shape and type as X."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 58,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([0, 0, 0, 0], dtype=int64)"
- ]
- },
- "execution_count": 58,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x = np.arange(4, dtype=np.int64)\n",
- "np.zeros_like(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create a new array of 2*5 uints, filled with 6."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[6, 6, 6, 6, 6],\n",
- " [6, 6, 6, 6, 6]], dtype=uint32)"
- ]
- },
- "execution_count": 49,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.full((2, 5), 6, dtype=np.uint)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[6, 6, 6, 6, 6],\n",
- " [6, 6, 6, 6, 6]], dtype=uint32)"
- ]
- },
- "execution_count": 50,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.ones([2, 5], dtype=np.uint) * 6"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let x = np.arange(4, dtype=np.int64). Create an array of 6's with the same shape and type as X."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 79,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([6, 6, 6, 6], dtype=int64)"
- ]
- },
- "execution_count": 79,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x = np.arange(4, dtype=np.int64)\n",
- "np.full_like(x, 6)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 81,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([6, 6, 6, 6], dtype=int64)"
- ]
- },
- "execution_count": 81,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.ones_like(x) * 6"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## From existing data"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create an array of [1, 2, 3]."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 53,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([1, 2, 3])"
- ]
- },
- "execution_count": 53,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.array([1, 2, 3])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let x = [1, 2]. Convert it into an array."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 60,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([1, 2])"
- ]
- },
- "execution_count": 60,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x = [1,2]\n",
- "np.asarray(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 62,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "matrix([[1, 2],\n",
- " [3, 4]])"
- ]
- },
- "execution_count": 62,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "X = np.array([[1, 2], [3, 4]])\n",
- "np.asmatrix(X)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let x = [1, 2]. Conver it into an array of `float`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 63,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([ 1., 2.])"
- ]
- },
- "execution_count": 63,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x = [1, 2]\n",
- "np.asfarray(x)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 64,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([ 1., 2.])"
- ]
- },
- "execution_count": 64,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.asarray(x, float)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 67,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "30"
- ]
- },
- "execution_count": 67,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x = np.array([30])\n",
- "np.asscalar(x)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 68,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "30"
- ]
- },
- "execution_count": 68,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x[0]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let x = np.array([1, 2, 3]). Create a array copy of x, which has a different id from x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 76,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "70140352 [1 2 3]\n",
- "70140752 [1 2 3]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, 2, 3])\n",
- "y = np.copy(x)\n",
- "print id(x), x\n",
- "print id(y), y"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Numerical ranges"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create an array of 2, 4, 6, 8, ..., 100."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 85,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,\n",
- " 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52,\n",
- " 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78,\n",
- " 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])"
- ]
- },
- "execution_count": 85,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.arange(2, 101, 2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 86,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([ 3. , 3.14285714, 3.28571429, 3.42857143,\n",
- " 3.57142857, 3.71428571, 3.85714286, 4. ,\n",
- " 4.14285714, 4.28571429, 4.42857143, 4.57142857,\n",
- " 4.71428571, 4.85714286, 5. , 5.14285714,\n",
- " 5.28571429, 5.42857143, 5.57142857, 5.71428571,\n",
- " 5.85714286, 6. , 6.14285714, 6.28571429,\n",
- " 6.42857143, 6.57142857, 6.71428571, 6.85714286,\n",
- " 7. , 7.14285714, 7.28571429, 7.42857143,\n",
- " 7.57142857, 7.71428571, 7.85714286, 8. ,\n",
- " 8.14285714, 8.28571429, 8.42857143, 8.57142857,\n",
- " 8.71428571, 8.85714286, 9. , 9.14285714,\n",
- " 9.28571429, 9.42857143, 9.57142857, 9.71428571,\n",
- " 9.85714286, 10. ])"
- ]
- },
- "execution_count": 86,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.linspace(3., 10, 50)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 88,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([ 1.00000000e+03, 1.38038426e+03, 1.90546072e+03,\n",
- " 2.63026799e+03, 3.63078055e+03, 5.01187234e+03,\n",
- " 6.91830971e+03, 9.54992586e+03, 1.31825674e+04,\n",
- " 1.81970086e+04, 2.51188643e+04, 3.46736850e+04,\n",
- " 4.78630092e+04, 6.60693448e+04, 9.12010839e+04,\n",
- " 1.25892541e+05, 1.73780083e+05, 2.39883292e+05,\n",
- " 3.31131121e+05, 4.57088190e+05, 6.30957344e+05,\n",
- " 8.70963590e+05, 1.20226443e+06, 1.65958691e+06,\n",
- " 2.29086765e+06, 3.16227766e+06, 4.36515832e+06,\n",
- " 6.02559586e+06, 8.31763771e+06, 1.14815362e+07,\n",
- " 1.58489319e+07, 2.18776162e+07, 3.01995172e+07,\n",
- " 4.16869383e+07, 5.75439937e+07, 7.94328235e+07,\n",
- " 1.09647820e+08, 1.51356125e+08, 2.08929613e+08,\n",
- " 2.88403150e+08, 3.98107171e+08, 5.49540874e+08,\n",
- " 7.58577575e+08, 1.04712855e+09, 1.44543977e+09,\n",
- " 1.99526231e+09, 2.75422870e+09, 3.80189396e+09,\n",
- " 5.24807460e+09, 7.24435960e+09])"
- ]
- },
- "execution_count": 88,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.logspace(3., 10., 50, endpoint=False)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Building matrices"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Let X = np.array([[ 0, 1, 2, 3],\n",
- " [ 4, 5, 6, 7],\n",
- " [ 8, 9, 10, 11]]).\n",
- " Get the diagonal of X, that is, [0, 5, 10]."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 93,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([ 0, 5, 10])"
- ]
- },
- "execution_count": 93,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])\n",
- "np.diag(X)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 94,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([ 0, 5, 10])"
- ]
- },
- "execution_count": 94,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "X.diagonal()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 95,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[1, 0, 0, 0],\n",
- " [0, 2, 0, 0],\n",
- " [0, 0, 3, 0],\n",
- " [0, 0, 0, 4]])"
- ]
- },
- "execution_count": 95,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.diagflat([1, 2, 3, 4])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create an array which looks like below.\n",
- "array([[ 0., 0., 0., 0., 0.],\n",
- " [ 1., 0., 0., 0., 0.],\n",
- " [ 1., 1., 0., 0., 0.]])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 97,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 0., 0., 0., 0., 0.],\n",
- " [ 1., 0., 0., 0., 0.],\n",
- " [ 1., 1., 0., 0., 0.]])"
- ]
- },
- "execution_count": 97,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.tri(3, 5, -1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create an array which looks like below.\n",
- "array([[ 0, 0, 0],\n",
- " [ 4, 0, 0],\n",
- " [ 7, 8, 0],\n",
- " [10, 11, 12]])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 101,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 0, 0, 0],\n",
- " [ 4, 0, 0],\n",
- " [ 7, 8, 0],\n",
- " [10, 11, 12]])"
- ]
- },
- "execution_count": 101,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.tril(np.arange(1, 13).reshape(4, 3), -1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Create an array which looks like below. array([[ 1, 2, 3],\n",
- " [ 4, 5, 6],\n",
- " [ 0, 8, 9],\n",
- " [ 0, 0, 12]])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 102,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[ 1, 2, 3],\n",
- " [ 4, 5, 6],\n",
- " [ 0, 8, 9],\n",
- " [ 0, 0, 12]])"
- ]
- },
- "execution_count": 102,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.triu(np.arange(1, 13).reshape(4, 3), -1)"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.6"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
+{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Array creation routines"]},{"cell_type":"markdown","metadata":{},"source":["## Ones and zeros"]},{"cell_type":"code","execution_count":1,"metadata":{"collapsed":true},"outputs":[],"source":"import numpy as np"},{"cell_type":"markdown","metadata":{},"source":["Create a new array of 2*2 integers, without initializing entries."]},{"cell_type":"code","execution_count":27,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[0, 0],\n"," [0, 0]])"]},"execution_count":27,"metadata":{},"output_type":"execute_result"}],"source":"np.empty([2,2], int)"},{"cell_type":"markdown","metadata":{},"source":["Let X = np.array([1,2,3], [4,5,6], np.int32). \n","Create a new array with the same shape and type as X."]},{"cell_type":"code","execution_count":32,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[1, 2, 3],\n"," [4, 5, 6]])"]},"execution_count":32,"metadata":{},"output_type":"execute_result"}],"source":"X = np.array([[1,2,3], [4,5,6]], np.int32)\nnp.empty_like(X)"},{"cell_type":"markdown","metadata":{},"source":["Create a 3-D array with ones on the diagonal and zeros elsewhere."]},{"cell_type":"code","execution_count":33,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[ 1., 0., 0.],\n"," [ 0., 1., 0.],\n"," [ 0., 0., 1.]])"]},"execution_count":33,"metadata":{},"output_type":"execute_result"}],"source":"np.eye(3)"},{"cell_type":"code","execution_count":35,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[ 1., 0., 0.],\n"," [ 0., 1., 0.],\n"," [ 0., 0., 1.]])"]},"execution_count":35,"metadata":{},"output_type":"execute_result"}],"source":"np.identity(3)"},{"cell_type":"markdown","metadata":{},"source":["Create a new array of 3*2 float numbers, filled with ones."]},{"cell_type":"code","execution_count":36,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[ 1., 1.],\n"," [ 1., 1.],\n"," [ 1., 1.]])"]},"execution_count":36,"metadata":{},"output_type":"execute_result"}],"source":"np.ones([3,2], float)"},{"cell_type":"markdown","metadata":{},"source":["Let x = np.arange(4, dtype=np.int64). Create an array of ones with the same shape and type as X."]},{"cell_type":"code","execution_count":59,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([1, 1, 1, 1], dtype=int64)"]},"execution_count":59,"metadata":{},"output_type":"execute_result"}],"source":"x = np.arange(4, dtype=np.int64)\nnp.ones_like(x)"},{"cell_type":"markdown","metadata":{},"source":["Create a new array of 3*2 float numbers, filled with zeros."]},{"cell_type":"code","execution_count":45,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[ 0., 0.],\n"," [ 0., 0.],\n"," [ 0., 0.]])"]},"execution_count":45,"metadata":{},"output_type":"execute_result"}],"source":"np.zeros((3,2), float)"},{"cell_type":"markdown","metadata":{},"source":["Let x = np.arange(4, dtype=np.int64). Create an array of zeros with the same shape and type as X."]},{"cell_type":"code","execution_count":58,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([0, 0, 0, 0], dtype=int64)"]},"execution_count":58,"metadata":{},"output_type":"execute_result"}],"source":"x = np.arange(4, dtype=np.int64)\nnp.zeros_like(x)"},{"cell_type":"markdown","metadata":{},"source":["Create a new array of 2*5 uints, filled with 6."]},{"cell_type":"code","execution_count":49,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[6, 6, 6, 6, 6],\n"," [6, 6, 6, 6, 6]], dtype=uint32)"]},"execution_count":49,"metadata":{},"output_type":"execute_result"}],"source":"np.full((2, 5), 6, dtype=np.uint)"},{"cell_type":"code","execution_count":50,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[6, 6, 6, 6, 6],\n"," [6, 6, 6, 6, 6]], dtype=uint32)"]},"execution_count":50,"metadata":{},"output_type":"execute_result"}],"source":"np.ones([2, 5], dtype=np.uint) * 6"},{"cell_type":"markdown","metadata":{},"source":["Let x = np.arange(4, dtype=np.int64). Create an array of 6's with the same shape and type as X."]},{"cell_type":"code","execution_count":79,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([6, 6, 6, 6], dtype=int64)"]},"execution_count":79,"metadata":{},"output_type":"execute_result"}],"source":"x = np.arange(4, dtype=np.int64)\nnp.full_like(x, 6)"},{"cell_type":"code","execution_count":81,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([6, 6, 6, 6], dtype=int64)"]},"execution_count":81,"metadata":{},"output_type":"execute_result"}],"source":"np.ones_like(x) * 6"},{"cell_type":"markdown","metadata":{},"source":["## From existing data"]},{"cell_type":"markdown","metadata":{},"source":["Create an array of [1, 2, 3]."]},{"cell_type":"code","execution_count":53,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([1, 2, 3])"]},"execution_count":53,"metadata":{},"output_type":"execute_result"}],"source":"np.array([1, 2, 3])"},{"cell_type":"markdown","metadata":{},"source":["Let x = [1, 2]. Convert it into an array."]},{"cell_type":"code","execution_count":60,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([1, 2])"]},"execution_count":60,"metadata":{},"output_type":"execute_result"}],"source":"x = [1,2]\nnp.asarray(x)"},{"cell_type":"markdown","metadata":{},"source":["Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix."]},{"cell_type":"code","execution_count":62,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["matrix([[1, 2],\n"," [3, 4]])"]},"execution_count":62,"metadata":{},"output_type":"execute_result"}],"source":"X = np.array([[1, 2], [3, 4]])\nnp.asmatrix(X)"},{"cell_type":"markdown","metadata":{},"source":["Let x = [1, 2]. Conver it into an array of `float`."]},{"cell_type":"code","execution_count":63,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([ 1., 2.])"]},"execution_count":63,"metadata":{},"output_type":"execute_result"}],"source":"x = [1, 2]\nnp.asfarray(x)"},{"cell_type":"code","execution_count":64,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([ 1., 2.])"]},"execution_count":64,"metadata":{},"output_type":"execute_result"}],"source":"np.asarray(x, float)"},{"cell_type":"markdown","metadata":{},"source":["Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30."]},{"cell_type":"code","execution_count":67,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["30"]},"execution_count":67,"metadata":{},"output_type":"execute_result"}],"source":"x = np.array([30])\nnp.asscalar(x)"},{"cell_type":"code","execution_count":68,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["30"]},"execution_count":68,"metadata":{},"output_type":"execute_result"}],"source":"x[0]"},{"cell_type":"markdown","metadata":{},"source":["Let x = np.array([1, 2, 3]). Create a array copy of x, which has a different id from x."]},{"cell_type":"code","execution_count":76,"metadata":{"collapsed":false},"outputs":[{"name":"stdout","output_type":"stream","text":["70140352 [1 2 3]\n","70140752 [1 2 3]\n"]}],"source":"x = np.array([1, 2, 3])\ny = np.copy(x)\nprint (id(x), x)\nprint (id(y), y)"},{"cell_type":"markdown","metadata":{},"source":["## Numerical ranges"]},{"cell_type":"markdown","metadata":{},"source":["Create an array of 2, 4, 6, 8, ..., 100."]},{"cell_type":"code","execution_count":85,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,\n"," 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52,\n"," 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78,\n"," 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])"]},"execution_count":85,"metadata":{},"output_type":"execute_result"}],"source":"np.arange(2, 101, 2)"},{"cell_type":"markdown","metadata":{},"source":["Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive."]},{"cell_type":"code","execution_count":86,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([ 3. , 3.14285714, 3.28571429, 3.42857143,\n"," 3.57142857, 3.71428571, 3.85714286, 4. ,\n"," 4.14285714, 4.28571429, 4.42857143, 4.57142857,\n"," 4.71428571, 4.85714286, 5. , 5.14285714,\n"," 5.28571429, 5.42857143, 5.57142857, 5.71428571,\n"," 5.85714286, 6. , 6.14285714, 6.28571429,\n"," 6.42857143, 6.57142857, 6.71428571, 6.85714286,\n"," 7. , 7.14285714, 7.28571429, 7.42857143,\n"," 7.57142857, 7.71428571, 7.85714286, 8. ,\n"," 8.14285714, 8.28571429, 8.42857143, 8.57142857,\n"," 8.71428571, 8.85714286, 9. , 9.14285714,\n"," 9.28571429, 9.42857143, 9.57142857, 9.71428571,\n"," 9.85714286, 10. ])"]},"execution_count":86,"metadata":{},"output_type":"execute_result"}],"source":"np.linspace(3., 10, 50)"},{"cell_type":"markdown","metadata":{},"source":["Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive."]},{"cell_type":"code","execution_count":88,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([ 1.00000000e+03, 1.38038426e+03, 1.90546072e+03,\n"," 2.63026799e+03, 3.63078055e+03, 5.01187234e+03,\n"," 6.91830971e+03, 9.54992586e+03, 1.31825674e+04,\n"," 1.81970086e+04, 2.51188643e+04, 3.46736850e+04,\n"," 4.78630092e+04, 6.60693448e+04, 9.12010839e+04,\n"," 1.25892541e+05, 1.73780083e+05, 2.39883292e+05,\n"," 3.31131121e+05, 4.57088190e+05, 6.30957344e+05,\n"," 8.70963590e+05, 1.20226443e+06, 1.65958691e+06,\n"," 2.29086765e+06, 3.16227766e+06, 4.36515832e+06,\n"," 6.02559586e+06, 8.31763771e+06, 1.14815362e+07,\n"," 1.58489319e+07, 2.18776162e+07, 3.01995172e+07,\n"," 4.16869383e+07, 5.75439937e+07, 7.94328235e+07,\n"," 1.09647820e+08, 1.51356125e+08, 2.08929613e+08,\n"," 2.88403150e+08, 3.98107171e+08, 5.49540874e+08,\n"," 7.58577575e+08, 1.04712855e+09, 1.44543977e+09,\n"," 1.99526231e+09, 2.75422870e+09, 3.80189396e+09,\n"," 5.24807460e+09, 7.24435960e+09])"]},"execution_count":88,"metadata":{},"output_type":"execute_result"}],"source":"np.logspace(3., 10., 50, endpoint=False)"},{"cell_type":"markdown","metadata":{},"source":["## Building matrices"]},{"cell_type":"markdown","metadata":{},"source":["Let X = np.array([[ 0, 1, 2, 3],\n"," [ 4, 5, 6, 7],\n"," [ 8, 9, 10, 11]]).\n"," Get the diagonal of X, that is, [0, 5, 10]."]},{"cell_type":"code","execution_count":93,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([ 0, 5, 10])"]},"execution_count":93,"metadata":{},"output_type":"execute_result"}],"source":"X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])\nnp.diag(X)"},{"cell_type":"code","execution_count":94,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([ 0, 5, 10])"]},"execution_count":94,"metadata":{},"output_type":"execute_result"}],"source":"X.diagonal()"},{"cell_type":"markdown","metadata":{},"source":["Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere."]},{"cell_type":"code","execution_count":95,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[1, 0, 0, 0],\n"," [0, 2, 0, 0],\n"," [0, 0, 3, 0],\n"," [0, 0, 0, 4]])"]},"execution_count":95,"metadata":{},"output_type":"execute_result"}],"source":"np.diagflat([1, 2, 3, 4])"},{"cell_type":"markdown","metadata":{},"source":["Create an array which looks like below.\n","array([[ 0., 0., 0., 0., 0.],\n"," [ 1., 0., 0., 0., 0.],\n"," [ 1., 1., 0., 0., 0.]])"]},{"cell_type":"code","execution_count":97,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[ 0., 0., 0., 0., 0.],\n"," [ 1., 0., 0., 0., 0.],\n"," [ 1., 1., 0., 0., 0.]])"]},"execution_count":97,"metadata":{},"output_type":"execute_result"}],"source":"np.tri(3, 5, -1)"},{"cell_type":"markdown","metadata":{},"source":["Create an array which looks like below.\n","array([[ 0, 0, 0],\n"," [ 4, 0, 0],\n"," [ 7, 8, 0],\n"," [10, 11, 12]])"]},{"cell_type":"code","execution_count":101,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[ 0, 0, 0],\n"," [ 4, 0, 0],\n"," [ 7, 8, 0],\n"," [10, 11, 12]])"]},"execution_count":101,"metadata":{},"output_type":"execute_result"}],"source":"np.tril(np.arange(1, 13).reshape(4, 3), -1)"},{"cell_type":"markdown","metadata":{},"source":["Create an array which looks like below. array([[ 1, 2, 3],\n"," [ 4, 5, 6],\n"," [ 0, 8, 9],\n"," [ 0, 0, 12]])"]},{"cell_type":"code","execution_count":102,"metadata":{"collapsed":false},"outputs":[{"data":{"text/plain":["array([[ 1, 2, 3],\n"," [ 4, 5, 6],\n"," [ 0, 8, 9],\n"," [ 0, 0, 12]])"]},"execution_count":102,"metadata":{},"output_type":"execute_result"}],"source":"np.triu(np.arange(1, 13).reshape(4, 3), -1)"}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
\ No newline at end of file
diff --git a/2_Array_manipulation_routines_Solutions.ipynb b/2_Array_manipulation_routines_Solutions.ipynb
index aa56247..b458179 100644
--- a/2_Array_manipulation_routines_Solutions.ipynb
+++ b/2_Array_manipulation_routines_Solutions.ipynb
@@ -1,815 +1 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Array manipulation routines"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1.11.2'"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.__version__"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "collapsed": true
- },
- "source": [
- "Q1. Let x be a ndarray [10, 10, 3] with all elements set to one. Reshape x so that the size of the second dimension equals 150."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1.]\n",
- " [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
- " 1. 1. 1. 1. 1. 1.]]\n"
- ]
- }
- ],
- "source": [
- "x = np.ones([10, 10, 3])\n",
- "out = np.reshape(x, [-1, 150])\n",
- "print out\n",
- "assert np.allclose(out, np.ones([10, 10, 3]).reshape([-1, 150]))"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q2. Let x be array [[1, 2, 3], [4, 5, 6]]. Convert it to [1 4 2 5 3 6]."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1 4 2 5 3 6]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
- "out1 = np.ravel(x, order='F')\n",
- "out2 = x.flatten(order=\"F\")\n",
- "assert np.allclose(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q3. Let x be array [[1, 2, 3], [4, 5, 6]]. Get the 5th element."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "5\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
- "out1 = x.flat[4]\n",
- "out2 = np.ravel(x)[4]\n",
- "assert np.allclose(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q4. Let x be an arbitrary 3-D array of shape (3, 4, 5). Permute the dimensions of x such that the new shape will be (4,3,5).\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(4L, 3L, 5L)\n"
- ]
- }
- ],
- "source": [
- "x = np.zeros((3, 4, 5))\n",
- "out1 = np.swapaxes(x, 1, 0)\n",
- "out2 = x.transpose([1, 0, 2])\n",
- "assert out1.shape == out2.shape\n",
- "print out1.shape"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q5. Let x be an arbitrary 2-D array of shape (3, 4). Permute the dimensions of x such that the new shape will be (4,3)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(4L, 3L)\n"
- ]
- }
- ],
- "source": [
- "x = np.zeros((3, 4))\n",
- "out1 = np.swapaxes(x, 1, 0)\n",
- "out2 = x.transpose()\n",
- "out3 = x.T\n",
- "assert out1.shape == out2.shape == out3.shape\n",
- "print out1.shape"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q5. Let x be an arbitrary 2-D array of shape (3, 4). Insert a nex axis such that the new shape will be (3, 1, 4)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(3L, 1L, 4L)\n"
- ]
- }
- ],
- "source": [
- "x = np.zeros((3, 4))\n",
- "print np.expand_dims(x, axis=1).shape"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q6. Let x be an arbitrary 3-D array of shape (3, 4, 1). Remove a single-dimensional entries such that the new shape will be (3, 4)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "(3L, 4L)\n"
- ]
- }
- ],
- "source": [
- "x = np.zeros((3, 4, 1))\n",
- "print np.squeeze(x).shape"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q7. Lex x be an array
\n",
- "[[ 1 2 3]
\n",
- "[ 4 5 6].
\n",
- "and y be an array
\n",
- "[[ 7 8 9]
\n",
- "[10 11 12]].
\n",
- "Concatenate x and y so that a new array looks like
[[1, 2, 3, 7, 8, 9],
[4, 5, 6, 10, 11, 12]].\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[ 1 2 3 7 8 9]\n",
- " [ 4 5 6 10 11 12]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
- "y = np.array([[7, 8, 9], [10, 11, 12]])\n",
- "out1 = np.concatenate((x, y), 1)\n",
- "out2 = np.hstack((x, y))\n",
- "assert np.allclose(out1, out2)\n",
- "print out2"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q8. Lex x be an array
\n",
- "[[ 1 2 3]
\n",
- "[ 4 5 6].
\n",
- "and y be an array
\n",
- "[[ 7 8 9]
\n",
- "[10 11 12]].
\n",
- "Concatenate x and y so that a new array looks like
[[ 1 2 3]
\n",
- " [ 4 5 6]
\n",
- " [ 7 8 9]
\n",
- " [10 11 12]]\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[ 1 2 3]\n",
- " [ 4 5 6]\n",
- " [ 7 8 9]\n",
- " [10 11 12]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
- "y = np.array([[7, 8, 9], [10, 11, 12]])\n",
- "out1 = np.concatenate((x, y), 0)\n",
- "out2 = np.vstack((x, y))\n",
- "assert np.allclose(out1, out2)\n",
- "print out2"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q8. Let x be an array [1 2 3] and y be [4 5 6]. Convert it to [[1, 4], [2, 5], [3, 6]]."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "array([[1, 4],\n",
- " [2, 5],\n",
- " [3, 6]])"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "x = np.array((1,2,3))\n",
- "y = np.array((4,5,6))\n",
- "out1 = np.column_stack((x, y))\n",
- "out2 = np.squeeze(np.dstack((x, y)))\n",
- "out3 = np.vstack((x, y)).T\n",
- "assert np.allclose(out1, out2)\n",
- "assert np.allclose(out2, out3)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q9. Let x be an array [[1],[2],[3]] and y be [[4], [5], [6]]. Convert x to [[[1, 4]], [[2, 5]], [[3, 6]]]."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[[1 4]]\n",
- "\n",
- " [[2 5]]\n",
- "\n",
- " [[3 6]]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1],[2],[3]])\n",
- "y = np.array([[4],[5],[6]])\n",
- "out = np.dstack((x, y))\n",
- "print out\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q10. Let x be an array [1, 2, 3, ..., 9]. Split x into 3 arrays, each of which has 4, 2, and 3 elements in the original order."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 62,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[array([1, 2, 3, 4]), array([5, 6]), array([7, 8, 9])]\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(1, 10)\n",
- "print np.split(x, [4, 6])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q11. Let x be an array
\n",
- "[[[ 0., 1., 2., 3.],
\n",
- " [ 4., 5., 6., 7.]],
\n",
- " \n",
- " [[ 8., 9., 10., 11.],
\n",
- " [ 12., 13., 14., 15.]]].
\n",
- "Split it into two such that the first array looks like
\n",
- "[[[ 0., 1., 2.],
\n",
- " [ 4., 5., 6.]],
\n",
- " \n",
- " [[ 8., 9., 10.],
\n",
- " [ 12., 13., 14.]]].
\n",
- " \n",
- "and the second one look like:
\n",
- " \n",
- "[[[ 3.],
\n",
- " [ 7.]],
\n",
- " \n",
- " [[ 11.],
\n",
- " [ 15.]]].
"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 72,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[array([[[ 0, 1, 2],\n",
- " [ 4, 5, 6]],\n",
- "\n",
- " [[ 8, 9, 10],\n",
- " [12, 13, 14]]]), array([[[ 3],\n",
- " [ 7]],\n",
- "\n",
- " [[11],\n",
- " [15]]])]\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(16).reshape(2, 2, 4)\n",
- "out1 = np.split(x, [3],axis=2)\n",
- "out2 = np.dsplit(x, [3])\n",
- "assert np.allclose(out1[0], out2[0])\n",
- "assert np.allclose(out1[1], out2[1])\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q12. Let x be an array
\n",
- "[[ 0., 1., 2., 3.],
\n",
- " [ 4., 5., 6., 7.],
\n",
- " [ 8., 9., 10., 11.],
\n",
- " [ 12., 13., 14., 15.]].
\n",
- "Split it into two arrays along the second axis."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 74,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[array([[ 0, 1],\n",
- " [ 4, 5],\n",
- " [ 8, 9],\n",
- " [12, 13]]), array([[ 2, 3],\n",
- " [ 6, 7],\n",
- " [10, 11],\n",
- " [14, 15]])]\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(16).reshape((4, 4))\n",
- "out1 = np.hsplit(x, 2)\n",
- "out2 = np.split(x, 2, 1)\n",
- "assert np.allclose(out1[0], out2[0])\n",
- "assert np.allclose(out1[1], out2[1])\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q13. Let x be an array
\n",
- "[[ 0., 1., 2., 3.],
\n",
- " [ 4., 5., 6., 7.],
\n",
- " [ 8., 9., 10., 11.],
\n",
- " [ 12., 13., 14., 15.]].
\n",
- "Split it into two arrays along the first axis."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 75,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[array([[0, 1, 2, 3],\n",
- " [4, 5, 6, 7]]), array([[ 8, 9, 10, 11],\n",
- " [12, 13, 14, 15]])]\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(16).reshape((4, 4))\n",
- "out1 = np.vsplit(x, 2)\n",
- "out2 = np.split(x, 2, 0)\n",
- "assert np.allclose(out1[0], out2[0])\n",
- "assert np.allclose(out1[1], out2[1])\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q14. Let x be an array [0, 1, 2]. Convert it to
\n",
- "[[0, 1, 2, 0, 1, 2],
\n",
- " [0, 1, 2, 0, 1, 2]]."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 93,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[0 1 2 0 1 2]\n",
- " [0 1 2 0 1 2]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([0, 1, 2])\n",
- "out1 = np.tile(x, [2, 2])\n",
- "out2 = np.resize(x, [2, 6])\n",
- "assert np.allclose(out1, out2)\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q15. Let x be an array [0, 1, 2]. Convert it to
\n",
- "[0, 0, 1, 1, 2, 2]."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 83,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0 0 1 1 2 2]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([0, 1, 2])\n",
- "print np.repeat(x, 2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q16. Let x be an array [0, 0, 0, 1, 2, 3, 0, 2, 1, 0].
\n",
- "remove the leading the trailing zeros."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 105,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1 2 3 0 2 1]\n"
- ]
- }
- ],
- "source": [
- "x = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))\n",
- "out = np.trim_zeros(x)\n",
- "print out"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q17. Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 107,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1 2 3 4 5] [2 3 1 1 2]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([2, 2, 1, 5, 4, 5, 1, 2, 3])\n",
- "u, indices = np.unique(x, return_counts=True)\n",
- "print u, indices"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q18. Lex x be an array
\n",
- "[[ 1 2]
\n",
- " [ 3 4].
\n",
- "Flip x along the second axis."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 120,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[2 1]\n",
- " [4 3]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1,2], [3,4]])\n",
- "out1 = np.fliplr(x)\n",
- "out2 = x[:, ::-1]\n",
- "assert np.allclose(out1, out2)\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q19. Lex x be an array
\n",
- "[[ 1 2]
\n",
- " [ 3 4].
\n",
- "Flip x along the first axis."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 121,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[3 4]\n",
- " [1 2]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1,2], [3,4]])\n",
- "out1 = np.flipud(x)\n",
- "out2 = x[::-1, :]\n",
- "assert np.allclose(out1, out2)\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q20. Lex x be an array
\n",
- "[[ 1 2]
\n",
- " [ 3 4].
\n",
- "Rotate x 90 degrees counter-clockwise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 122,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[2 4]\n",
- " [1 3]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1,2], [3,4]])\n",
- "out = np.rot90(x)\n",
- "print out"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q21 Lex x be an array
\n",
- "[[ 1 2 3 4]
\n",
- " [ 5 6 7 8].
\n",
- "Shift elements one step to right along the second axis."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 126,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[4 1 2 3]\n",
- " [8 5 6 7]]\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(1, 9).reshape([2, 4])\n",
- "print np.roll(x, 1, axis=1)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "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.7.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 1
-}
+{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Array manipulation routines"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":"import numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"'1.16.5'"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"np.__version__"},{"cell_type":"markdown","metadata":{"collapsed":true},"source":"Q1. Let x be a ndarray [10, 10, 3] with all elements set to one. Reshape x so that the size of the second dimension equals 150."},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1.]\n [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n 1. 1. 1. 1. 1. 1.]]\n"}],"source":"x = np.ones([10, 10, 3])\nout = np.reshape(x, [-1, 150])\nprint (out)\nassert np.allclose(out, np.ones([10, 10, 3]).reshape([-1, 150]))"},{"cell_type":"markdown","metadata":{},"source":["Q2. Let x be array [[1, 2, 3], [4, 5, 6]]. Convert it to [1 4 2 5 3 6]."]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1 4 2 5 3 6]\n"}],"source":"x = np.array([[1, 2, 3], [4, 5, 6]])\nout1 = np.ravel(x, order='F')\nout2 = x.flatten(order=\"F\")\nassert np.allclose(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q3. Let x be array [[1, 2, 3], [4, 5, 6]]. Get the 5th element."]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"5\n"}],"source":"x = np.array([[1, 2, 3], [4, 5, 6]])\nout1 = x.flat[4]\nout2 = np.ravel(x)[4]\nassert np.allclose(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q4. Let x be an arbitrary 3-D array of shape (3, 4, 5). Permute the dimensions of x such that the new shape will be (4,3,5).\n"]},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"(4, 3, 5)\n"}],"source":"x = np.zeros((3, 4, 5))\nout1 = np.swapaxes(x, 1, 0)\nout2 = x.transpose([1, 0, 2])\nassert out1.shape == out2.shape\nprint (out1.shape)"},{"cell_type":"markdown","metadata":{},"source":["Q5. Let x be an arbitrary 2-D array of shape (3, 4). Permute the dimensions of x such that the new shape will be (4,3)."]},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"(4, 3)\n"}],"source":"x = np.zeros((3, 4))\nout1 = np.swapaxes(x, 1, 0)\nout2 = x.transpose()\nout3 = x.T\nassert out1.shape == out2.shape == out3.shape\nprint (out1.shape)"},{"cell_type":"markdown","metadata":{},"source":["Q5. Let x be an arbitrary 2-D array of shape (3, 4). Insert a nex axis such that the new shape will be (3, 1, 4)."]},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"(3, 1, 4)\n"}],"source":"x = np.zeros((3, 4))\nprint (np.expand_dims(x, axis=1).shape)"},{"cell_type":"markdown","metadata":{},"source":["Q6. Let x be an arbitrary 3-D array of shape (3, 4, 1). Remove a single-dimensional entries such that the new shape will be (3, 4)."]},{"cell_type":"code","execution_count":10,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"(3, 4)\n"}],"source":"x = np.zeros((3, 4, 1))\nprint (np.squeeze(x).shape)"},{"cell_type":"markdown","metadata":{},"source":"Q7. Lex x be an array
\n[[ 1 2 3]
\n[ 4 5 6]].
\nand y be an array
\n[[ 7 8 9]
\n[10 11 12]].
\nConcatenate x and y so that a new array looks like
[[1, 2, 3, 7, 8, 9],
[4, 5, 6, 10, 11, 12]].\n"},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[ 1 2 3 7 8 9]\n [ 4 5 6 10 11 12]]\n"}],"source":"x = np.array([[1, 2, 3], [4, 5, 6]])\ny = np.array([[7, 8, 9], [10, 11, 12]])\nout1 = np.concatenate((x, y), 1)\nout2 = np.hstack((x, y))\nassert np.allclose(out1, out2)\nprint (out2)"},{"cell_type":"markdown","metadata":{},"source":["Q8. Lex x be an array
\n","[[ 1 2 3]
\n","[ 4 5 6].
\n","and y be an array
\n","[[ 7 8 9]
\n","[10 11 12]].
\n","Concatenate x and y so that a new array looks like
[[ 1 2 3]
\n"," [ 4 5 6]
\n"," [ 7 8 9]
\n"," [10 11 12]]\n"]},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[ 1 2 3]\n [ 4 5 6]\n [ 7 8 9]\n [10 11 12]]\n"}],"source":"x = np.array([[1, 2, 3], [4, 5, 6]])\ny = np.array([[7, 8, 9], [10, 11, 12]])\nout1 = np.concatenate((x, y), 0)\nout2 = np.vstack((x, y))\nassert np.allclose(out1, out2)\nprint (out2)"},{"cell_type":"markdown","metadata":{},"source":["Q8. Let x be an array [1 2 3] and y be [4 5 6]. Convert it to [[1, 4], [2, 5], [3, 6]]."]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[1 4]\n [2 5]\n [3 6]]\n"}],"source":"x = np.array((1,2,3))\ny = np.array((4,5,6))\nout1 = np.column_stack((x, y))\nout2 = np.squeeze(np.dstack((x, y)))\nout3 = np.vstack((x, y)).T\nassert np.allclose(out1, out2)\nassert np.allclose(out2, out3)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q9. Let x be an array [[1],[2],[3]] and y be [[4], [5], [6]]. Convert x to [[[1, 4]], [[2, 5]], [[3, 6]]]."]},{"cell_type":"code","execution_count":14,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[[1 4]]\n\n [[2 5]]\n\n [[3 6]]]\n"}],"source":"x = np.array([[1],[2],[3]])\ny = np.array([[4],[5],[6]])\nout = np.dstack((x, y))\nprint (out)\n"},{"cell_type":"markdown","metadata":{},"source":["Q10. Let x be an array [1, 2, 3, ..., 9]. Split x into 3 arrays, each of which has 4, 2, and 3 elements in the original order."]},{"cell_type":"code","execution_count":15,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[array([1, 2, 3, 4]), array([5, 6]), array([7, 8, 9])]\n"}],"source":"x = np.arange(1, 10)\nprint (np.split(x, [4, 6]))"},{"cell_type":"markdown","metadata":{},"source":"Q11. Let x be an array
\n[[[ 0., 1., 2., 3.],
\n [ 4., 5., 6., 7.]],
\n \n [[ 8., 9., 10., 11.],
\n [ 12., 13., 14., 15.]]].
\nSplit it into two such that the first array looks like
\n[[[ 0., 1., 2.],
\n [ 4., 5., 6.]],
\n \n [[ 8., 9., 10.],
\n [ 12., 13., 14.]]].
\n \nand the second one look like:
\n \n[[[ 3.],
\n [ 7.]],
\n \n [[ 11.],
\n [ 15.]]].
"},{"cell_type":"code","execution_count":16,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[array([[[ 0, 1, 2],\n [ 4, 5, 6]],\n\n [[ 8, 9, 10],\n [12, 13, 14]]]), array([[[ 3],\n [ 7]],\n\n [[11],\n [15]]])]\n"}],"source":"x = np.arange(16).reshape(2, 2, 4)\nout1 = np.split(x, [3],axis=2)\nout2 = np.dsplit(x, [3])\nassert np.allclose(out1[0], out2[0])\nassert np.allclose(out1[1], out2[1])\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":"Q12. Let x be an array
\n[[ 0., 1., 2., 3.],
\n [ 4., 5., 6., 7.],
\n [ 8., 9., 10., 11.],
\n [ 12., 13., 14., 15.]].
\nSplit it into two arrays along the second axis."},{"cell_type":"code","execution_count":17,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[array([[ 0, 1],\n [ 4, 5],\n [ 8, 9],\n [12, 13]]), array([[ 2, 3],\n [ 6, 7],\n [10, 11],\n [14, 15]])]\n"}],"source":"x = np.arange(16).reshape((4, 4))\nout1 = np.hsplit(x, 2)\nout2 = np.split(x, 2, 1)\nassert np.allclose(out1[0], out2[0])\nassert np.allclose(out1[1], out2[1])\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":"Q13. Let x be an array
\n[[ 0., 1., 2., 3.],
\n [ 4., 5., 6., 7.],
\n [ 8., 9., 10., 11.],
\n [ 12., 13., 14., 15.]].
\nSplit it into two arrays along the first axis."},{"cell_type":"code","execution_count":18,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[array([[0, 1, 2, 3],\n [4, 5, 6, 7]]), array([[ 8, 9, 10, 11],\n [12, 13, 14, 15]])]\n"}],"source":"x = np.arange(16).reshape((4, 4))\nout1 = np.vsplit(x, 2)\nout2 = np.split(x, 2, 0)\nassert np.allclose(out1[0], out2[0])\nassert np.allclose(out1[1], out2[1])\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":"Q14. Let x be an array [0, 1, 2]. Convert it to
\n[[0, 1, 2, 0, 1, 2],
\n [0, 1, 2, 0, 1, 2]]."},{"cell_type":"code","execution_count":19,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[0 1 2 0 1 2]\n [0 1 2 0 1 2]]\n"}],"source":"x = np.array([0, 1, 2])\nout1 = np.tile(x, [2, 2])\nout2 = np.resize(x, [2, 6])\nassert np.allclose(out1, out2)\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":"Q15. Let x be an array [0, 1, 2]. Convert it to
\n[0, 0, 1, 1, 2, 2]."},{"cell_type":"code","execution_count":20,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0 0 1 1 2 2]\n"}],"source":"x = np.array([0, 1, 2])\nprint (np.repeat(x, 2))"},{"cell_type":"markdown","metadata":{},"source":"Q16. Let x be an array [0, 0, 0, 1, 2, 3, 0, 2, 1, 0].
\nremove the leading the trailing zeros."},{"cell_type":"code","execution_count":21,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1 2 3 0 2 1]\n"}],"source":"x = np.array((0, 0, 0, 1, 2, 3, 0, 2, 1, 0))\nout = np.trim_zeros(x)\nprint (out)"},{"cell_type":"markdown","metadata":{},"source":"Q17. Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.\n"},{"cell_type":"code","execution_count":22,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1 2 3 4 5] [2 3 1 1 2]\n"}],"source":"x = np.array([2, 2, 1, 5, 4, 5, 1, 2, 3])\nu, indices = np.unique(x, return_counts=True)\nprint (u, indices)"},{"cell_type":"markdown","metadata":{},"source":["Q18. Lex x be an array
\n","[[ 1 2]
\n"," [ 3 4].
\n","Flip x along the second axis."]},{"cell_type":"code","execution_count":23,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[2 1]\n [4 3]]\n"}],"source":"x = np.array([[1,2], [3,4]])\nout1 = np.fliplr(x)\nout2 = x[:, ::-1]\nassert np.allclose(out1, out2)\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":"Q19. Lex x be an array
\n[[ 1 2]
\n [ 3 4].
\nFlip x along the first axis."},{"cell_type":"code","execution_count":24,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[3 4]\n [1 2]]\n"}],"source":"x = np.array([[1,2], [3,4]])\nout1 = np.flipud(x)\nout2 = x[::-1, :]\nassert np.allclose(out1, out2)\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":["Q20. Lex x be an array
\n","[[ 1 2]
\n"," [ 3 4].
\n","Rotate x 90 degrees counter-clockwise."]},{"cell_type":"code","execution_count":25,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[2 4]\n [1 3]]\n"}],"source":"x = np.array([[1,2], [3,4]])\nout = np.rot90(x)\nprint (out)"},{"cell_type":"markdown","metadata":{},"source":"Q21 Lex x be an array
\n[[ 1 2 3 4]
\n [ 5 6 7 8].
\nShift elements one step to right along the second axis."},{"cell_type":"code","execution_count":26,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[4 1 2 3]\n [8 5 6 7]]\n"}],"source":"x = np.arange(1, 9).reshape([2, 4])\nprint (np.roll(x, 1, axis=1))"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":""}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
\ No newline at end of file
diff --git a/6_Linear_algebra_Solutions.ipynb b/6_Linear_algebra_Solutions.ipynb
index 3e78a91..16ce4e5 100644
--- a/6_Linear_algebra_Solutions.ipynb
+++ b/6_Linear_algebra_Solutions.ipynb
@@ -1,602 +1 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Linear algebra"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1.11.2'"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.__version__"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Matrix and vector products"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q1. Predict the results of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 61,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[8 5]\n",
- "[6 6]\n",
- "[8 5]\n",
- "[6 6]\n",
- "[6 6]\n"
- ]
- }
- ],
- "source": [
- "x = [1,2]\n",
- "y = [[4, 1], [2, 2]]\n",
- "print np.dot(x, y)\n",
- "print np.dot(y, x)\n",
- "print np.matmul(x, y)\n",
- "print np.inner(x, y)\n",
- "print np.inner(y, x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q2. Predict the results of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 62,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[4 1]\n",
- " [2 2]\n",
- " [1 1]]\n",
- "[[4 1]\n",
- " [2 2]\n",
- " [1 1]]\n"
- ]
- }
- ],
- "source": [
- "x = [[1, 0], [0, 1]]\n",
- "y = [[4, 1], [2, 2], [1, 1]]\n",
- "print np.dot(y, x)\n",
- "print np.matmul(y, x)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q3. Predict the results of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 63,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "30\n",
- "30\n",
- "30\n",
- "30\n",
- "30\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 4], [5, 6]])\n",
- "y = np.array([[4, 1], [2, 2]])\n",
- "print np.vdot(x, y)\n",
- "print np.vdot(y, x)\n",
- "print np.dot(x.flatten(), y.flatten())\n",
- "print np.inner(x.flatten(), y.flatten())\n",
- "print (x*y).sum()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q4. Predict the results of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 65,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "abb\n",
- "abb\n",
- "[['a' 'aa']\n",
- " ['b' 'bb']]\n",
- "[['a' 'b']\n",
- " ['aa' 'bb']]\n"
- ]
- }
- ],
- "source": [
- "x = np.array(['a', 'b'], dtype=object)\n",
- "y = np.array([1, 2])\n",
- "print np.inner(x, y)\n",
- "print np.inner(y, x)\n",
- "print np.outer(x, y)\n",
- "print np.outer(y, x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Decompositions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q5. Get the lower-trianglular `L` in the Cholesky decomposition of x and verify it."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 97,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[ 2. 0. 0.]\n",
- " [ 6. 1. 0.]\n",
- " [-8. 5. 3.]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=np.int32)\n",
- "L = np.linalg.cholesky(x)\n",
- "print L\n",
- "assert np.array_equal(np.dot(L, L.T.conjugate()), x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q6. Compute the qr factorization of x and verify it."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 107,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "q=\n",
- "[[-0.85714287 0.39428571 0.33142856]\n",
- " [-0.42857143 -0.90285712 -0.03428571]\n",
- " [ 0.2857143 -0.17142858 0.94285715]] \n",
- "r=\n",
- "[[ -14. -21. 14.]\n",
- " [ 0. -175. 70.]\n",
- " [ 0. 0. -35.]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=np.float32)\n",
- "q, r = np.linalg.qr(x)\n",
- "print \"q=\\n\", q, \"\\nr=\\n\", r\n",
- "assert np.allclose(np.dot(q, r), x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q7. Factor x by Singular Value Decomposition and verify it."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 165,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "U=\n",
- "[[ 0. 1. 0. 0.]\n",
- " [ 1. 0. 0. 0.]\n",
- " [ 0. 0. 0. -1.]\n",
- " [ 0. 0. 1. 0.]] \n",
- "s=\n",
- "[ 3. 2.23606801 2. 0. ] \n",
- "V=\n",
- "[[ 1. 0. 0.]\n",
- " [ 0. 1. 0.]\n",
- " [ 0. 0. 1.]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0]], dtype=np.float32)\n",
- "U, s, V = np.linalg.svd(x, full_matrices=False)\n",
- "print \"U=\\n\", U, \"\\ns=\\n\", s, \"\\nV=\\n\", v\n",
- "assert np.allclose(np.dot(U, np.dot(np.diag(s), V)), x)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Matrix eigenvalues"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q8. Compute the eigenvalues and right eigenvectors of x. (Name them eigenvals and eigenvecs, respectively)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 68,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "eigenvalues are\n",
- "[ 1. 2. 3.]\n",
- "eigenvectors are\n",
- "[[ 1. 0. 0.]\n",
- " [ 0. 1. 0.]\n",
- " [ 0. 0. 1.]]\n"
- ]
- }
- ],
- "source": [
- "x = np.diag((1, 2, 3))\n",
- "eigenvals = np.linalg.eig(x)[0]\n",
- "eigenvals_ = np.linalg.eigvals(x)\n",
- "assert np.array_equal(eigenvals, eigenvals_)\n",
- "print \"eigenvalues are\\n\", eigenvals\n",
- "eigenvecs = np.linalg.eig(x)[1]\n",
- "print \"eigenvectors are\\n\", eigenvecs"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q9. Predict the results of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 69,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n"
- ]
- }
- ],
- "source": [
- "print np.array_equal(np.dot(x, eigenvecs), eigenvals * eigenvecs)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Norms and other numbers"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q10. Calculate the Frobenius norm and the condition number of x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "16.8819430161\n",
- "4.56177073661e+17\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(1, 10).reshape((3, 3))\n",
- "print np.linalg.norm(x, 'fro')\n",
- "print np.linalg.cond(x, 'fro')"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q11. Calculate the determinant of x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "-2.0\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(1, 5).reshape((2, 2))\n",
- "out1 = np.linalg.det(x)\n",
- "out2 = x[0, 0] * x[1, 1] - x[0, 1] * x[1, 0]\n",
- "assert np.allclose(out1, out2)\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q12. Calculate the rank of x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 35,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "4\n"
- ]
- }
- ],
- "source": [
- "x = np.eye(4)\n",
- "out1 = np.linalg.matrix_rank(x)\n",
- "out2 = np.linalg.svd(x)[1].size\n",
- "assert out1 == out2\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q13. Compute the sign and natural logarithm of the determinant of x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "-1.0 0.69314718056\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(1, 5).reshape((2, 2))\n",
- "sign, logdet = np.linalg.slogdet(x)\n",
- "det = np.linalg.det(x)\n",
- "assert sign == np.sign(det)\n",
- "assert logdet == np.log(np.abs(det))\n",
- "print sign, logdet\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q14. Return the sum along the diagonal of x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 57,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "4.0\n"
- ]
- }
- ],
- "source": [
- "x = np.eye(4)\n",
- "out1 = np.trace(x)\n",
- "out2 = x.diagonal().sum()\n",
- "assert out1 == out2\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Solving equations and inverting matrices"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "collapsed": false
- },
- "source": [
- "Q15. Compute the inverse of x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 60,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[-2. 1. ]\n",
- " [ 1.5 -0.5]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1., 2.], [3., 4.]])\n",
- "out1 = np.linalg.inv(x)\n",
- "assert np.allclose(np.dot(x, out1), np.eye(2))\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.10"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
+{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Linear algebra"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":"import numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"'1.16.5'"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"np.__version__"},{"cell_type":"markdown","metadata":{},"source":["## Matrix and vector products"]},{"cell_type":"markdown","metadata":{},"source":["Q1. Predict the results of the following code."]},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[8 5]\n[6 6]\n[8 5]\n[6 6]\n[6 6]\n"}],"source":"x = [1,2]\ny = [[4, 1], [2, 2]]\nprint (np.dot(x, y))\nprint (np.dot(y, x))\nprint (np.matmul(x, y))\nprint (np.inner(x, y))\nprint (np.inner(y, x))"},{"cell_type":"markdown","metadata":{},"source":["Q2. Predict the results of the following code."]},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[4 1]\n [2 2]\n [1 1]]\n[[4 1]\n [2 2]\n [1 1]]\n"}],"source":"x = [[1, 0], [0, 1]]\ny = [[4, 1], [2, 2], [1, 1]]\nprint (np.dot(y, x))\nprint (np.matmul(y, x))\n"},{"cell_type":"markdown","metadata":{},"source":["Q3. Predict the results of the following code."]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"30\n30\n30\n30\n30\n"}],"source":"x = np.array([[1, 4], [5, 6]])\ny = np.array([[4, 1], [2, 2]])\nprint (np.vdot(x, y))\nprint (np.vdot(y, x))\nprint (np.dot(x.flatten(), y.flatten()))\nprint (np.inner(x.flatten(), y.flatten()))\nprint ((x*y).sum())"},{"cell_type":"markdown","metadata":{},"source":["Q4. Predict the results of the following code."]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"abb\nabb\n[['a' 'aa']\n ['b' 'bb']]\n[['a' 'b']\n ['aa' 'bb']]\n"}],"source":"x = np.array(['a', 'b'], dtype=object)\ny = np.array([1, 2])\nprint (np.inner(x, y))\nprint (np.inner(y, x))\nprint (np.outer(x, y))\nprint (np.outer(y, x))"},{"cell_type":"markdown","metadata":{},"source":["## Decompositions"]},{"cell_type":"markdown","metadata":{},"source":["Q5. Get the lower-trianglular `L` in the Cholesky decomposition of x and verify it."]},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[ 2. 0. 0.]\n [ 6. 1. 0.]\n [-8. 5. 3.]]\n"}],"source":"x = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=np.int32)\nL = np.linalg.cholesky(x)\nprint (L)\nassert np.array_equal(np.dot(L, L.T.conjugate()), x)"},{"cell_type":"markdown","metadata":{},"source":["Q6. Compute the qr factorization of x and verify it."]},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"q=\n [[-0.85714287 0.3942857 0.33142856]\n [-0.42857143 -0.9028571 -0.03428571]\n [ 0.2857143 -0.17142858 0.94285715]] \nr=\n [[ -14. -21. 14.]\n [ 0. -175. 70.]\n [ 0. 0. -35.]]\n"}],"source":"x = np.array([[12, -51, 4], [6, 167, -68], [-4, 24, -41]], dtype=np.float32)\nq, r = np.linalg.qr(x)\nprint (\"q=\\n\", q, \"\\nr=\\n\", r)\nassert np.allclose(np.dot(q, r), x)"},{"cell_type":"markdown","metadata":{},"source":["Q7. Factor x by Singular Value Decomposition and verify it."]},{"cell_type":"code","execution_count":10,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"U=\n [[ 0. 1. 0. 0.]\n [ 1. 0. 0. 0.]\n [ 0. 0. 0. -1.]\n [ 0. 0. 1. 0.]] \ns=\n [3. 2.236068 2. 0. ] \nV=\n [[-0. 0. 1. 0. 0. ]\n [ 0.4472136 0. 0. 0. 0.8944272]\n [-0. 1. 0. 0. 0. ]\n [ 0. 0. 0. 1. 0. ]]\n"}],"source":"x = np.array([[1, 0, 0, 0, 2], [0, 0, 3, 0, 0], [0, 0, 0, 0, 0], [0, 2, 0, 0, 0]], dtype=np.float32)\nU, s, V = np.linalg.svd(x, full_matrices=False)\nprint (\"U=\\n\", U, \"\\ns=\\n\", s, \"\\nV=\\n\", V)\nassert np.allclose(np.dot(U, np.dot(np.diag(s), V)), x)\n"},{"cell_type":"markdown","metadata":{},"source":["## Matrix eigenvalues"]},{"cell_type":"markdown","metadata":{},"source":["Q8. Compute the eigenvalues and right eigenvectors of x. (Name them eigenvals and eigenvecs, respectively)"]},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"eigenvalues are\n [1. 2. 3.]\neigenvectors are\n [[1. 0. 0.]\n [0. 1. 0.]\n [0. 0. 1.]]\n"}],"source":"x = np.diag((1, 2, 3))\neigenvals = np.linalg.eig(x)[0]\neigenvals_ = np.linalg.eigvals(x)\nassert np.array_equal(eigenvals, eigenvals_)\nprint (\"eigenvalues are\\n\", eigenvals)\neigenvecs = np.linalg.eig(x)[1]\nprint (\"eigenvectors are\\n\", eigenvecs)"},{"cell_type":"markdown","metadata":{},"source":["Q9. Predict the results of the following code."]},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"True\n"}],"source":"print (np.array_equal(np.dot(x, eigenvecs), eigenvals * eigenvecs))"},{"cell_type":"markdown","metadata":{},"source":["## Norms and other numbers"]},{"cell_type":"markdown","metadata":{},"source":["Q10. Calculate the Frobenius norm and the condition number of x."]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"16.881943016134134\n3.193239515623568e+17\n"}],"source":"x = np.arange(1, 10).reshape((3, 3))\nprint (np.linalg.norm(x, 'fro'))\nprint (np.linalg.cond(x, 'fro'))"},{"cell_type":"markdown","metadata":{},"source":["Q11. Calculate the determinant of x."]},{"cell_type":"code","execution_count":14,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"-2.0000000000000004\n"}],"source":"x = np.arange(1, 5).reshape((2, 2))\nout1 = np.linalg.det(x)\nout2 = x[0, 0] * x[1, 1] - x[0, 1] * x[1, 0]\nassert np.allclose(out1, out2)\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":["Q12. Calculate the rank of x."]},{"cell_type":"code","execution_count":15,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"4\n"}],"source":"x = np.eye(4)\nout1 = np.linalg.matrix_rank(x)\nout2 = np.linalg.svd(x)[1].size\nassert out1 == out2\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":"Q13. Compute the sign and natural logarithm of the determinant of x."},{"cell_type":"code","execution_count":16,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"-1.0 0.6931471805599455\n"}],"source":"x = np.arange(1, 5).reshape((2, 2))\nsign, logdet = np.linalg.slogdet(x)\ndet = np.linalg.det(x)\nassert sign == np.sign(det)\nassert logdet == np.log(np.abs(det))\nprint (sign, logdet)\n"},{"cell_type":"markdown","metadata":{},"source":["Q14. Return the sum along the diagonal of x."]},{"cell_type":"code","execution_count":17,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"4.0\n"}],"source":"x = np.eye(4)\nout1 = np.trace(x)\nout2 = x.diagonal().sum()\nassert out1 == out2\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":["## Solving equations and inverting matrices"]},{"cell_type":"markdown","metadata":{"collapsed":false},"source":["Q15. Compute the inverse of x."]},{"cell_type":"code","execution_count":18,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[-2. 1. ]\n [ 1.5 -0.5]]\n"}],"source":"x = np.array([[1., 2.], [3., 4.]])\nout1 = np.linalg.inv(x)\nassert np.allclose(np.dot(x, out1), np.eye(2))\nprint (out1)\n"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":""}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
\ No newline at end of file
diff --git a/8_Logic_functions_Solutions.ipynb b/8_Logic_functions_Solutions.ipynb
index fdd7d85..21e4b21 100644
--- a/8_Logic_functions_Solutions.ipynb
+++ b/8_Logic_functions_Solutions.ipynb
@@ -1,379 +1 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Logic functions"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1.11.2'"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.__version__"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Truth value testing\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q1. Let x be an arbitrary array. Return True if none of the elements of x is zero. Remind that 0 evaluates to False in python.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1,2,3])\n",
- "print np.all(x)\n",
- "\n",
- "x = np.array([1,0,3])\n",
- "print np.all(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q2. Let x be an arbitrary array. Return True if any of the elements of x is non-zero."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n",
- "False\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1,0,0])\n",
- "print np.any(x)\n",
- "\n",
- "x = np.array([0,0,0])\n",
- "print np.any(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Array contents\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q3. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "x = np.array([1, 0, np.nan, np.inf])\n",
- "#print np.isfinite(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q4. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "x = np.array([1, 0, np.nan, np.inf])\n",
- "#print np.isinf(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q5. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "x = np.array([1, 0, np.nan, np.inf])\n",
- "#print np.isnan(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Array type testing"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q6. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\n",
- "#print np.iscomplex(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q7. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\n",
- "#print np.isreal(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q8. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "#print np.isscalar(3)\n",
- "#print np.isscalar([3])\n",
- "#print np.isscalar(True)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Logical operations"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q9. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "#print np.logical_and([True, False], [False, False])\n",
- "#print np.logical_or([True, False, True], [True, False, False])\n",
- "#print np.logical_xor([True, False, True], [True, False, False])\n",
- "#print np.logical_not([True, False, 0, 1])\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Comparison"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q10. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "#print np.allclose([3], [2.999999])\n",
- "#print np.array_equal([3], [2.999999])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q11. Write numpy comparison functions such that they return the results as you see."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ True False]\n",
- "[ True True]\n",
- "[False False]\n",
- "[False True]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([4, 5])\n",
- "y = np.array([2, 5])\n",
- "print np.greater(x, y)\n",
- "print np.greater_equal(x, y)\n",
- "print np.less(x, y)\n",
- "print np.less_equal(x, y)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q12. Predict the result of the following code."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "#print np.equal([1, 2], [1, 2.000001])\n",
- "#print np.isclose([1, 2], [1, 2.000001])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.12"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
-}
+{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Logic functions"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":"import numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"'1.16.5'"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"np.__version__"},{"cell_type":"markdown","metadata":{},"source":["## Truth value testing\n"]},{"cell_type":"markdown","metadata":{},"source":["Q1. Let x be an arbitrary array. Return True if none of the elements of x is zero. Remind that 0 evaluates to False in python.\n"]},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"True\nFalse\n"}],"source":"x = np.array([1,2,3])\nprint (np.all(x))\n\nx = np.array([1,0,3])\nprint (np.all(x))"},{"cell_type":"markdown","metadata":{},"source":["Q2. Let x be an arbitrary array. Return True if any of the elements of x is non-zero."]},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"True\nFalse\n"}],"source":"x = np.array([1,0,0])\nprint (np.any(x))\n\nx = np.array([0,0,0])\nprint (np.any(x))"},{"cell_type":"markdown","metadata":{},"source":["## Array contents\n"]},{"cell_type":"markdown","metadata":{},"source":["Q3. Predict the result of the following code."]},{"cell_type":"code","execution_count":5,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ True True False False]\n"}],"source":"x = np.array([1, 0, np.nan, np.inf])\nprint (np.isfinite(x))"},{"cell_type":"markdown","metadata":{},"source":["Q4. Predict the result of the following code."]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[False False False True]\n"}],"source":"x = np.array([1, 0, np.nan, np.inf])\nprint (np.isinf(x))"},{"cell_type":"markdown","metadata":{},"source":["Q5. Predict the result of the following code."]},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[False False True False]\n"}],"source":"x = np.array([1, 0, np.nan, np.inf])\nprint (np.isnan(x))"},{"cell_type":"markdown","metadata":{},"source":["## Array type testing"]},{"cell_type":"markdown","metadata":{},"source":["Q6. Predict the result of the following code."]},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ True False False False False True]\n"}],"source":"x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\nprint (np.iscomplex(x))"},{"cell_type":"markdown","metadata":{},"source":["Q7. Predict the result of the following code."]},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[False True True True True False]\n"}],"source":"x = np.array([1+1j, 1+0j, 4.5, 3, 2, 2j])\nprint (np.isreal(x))"},{"cell_type":"markdown","metadata":{},"source":["Q8. Predict the result of the following code."]},{"cell_type":"code","execution_count":10,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"True\nFalse\nTrue\n"}],"source":"print (np.isscalar(3))\nprint (np.isscalar([3]))\nprint (np.isscalar(True))"},{"cell_type":"markdown","metadata":{},"source":["## Logical operations"]},{"cell_type":"markdown","metadata":{},"source":["Q9. Predict the result of the following code."]},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[False False]\n[ True False True]\n[False False True]\n[False True True False]\n"}],"source":"print (np.logical_and([True, False], [False, False]))\nprint (np.logical_or([True, False, True], [True, False, False]))\nprint (np.logical_xor([True, False, True], [True, False, False]))\nprint (np.logical_not([True, False, 0, 1]))\n"},{"cell_type":"markdown","metadata":{},"source":"## Comparison"},{"cell_type":"markdown","metadata":{},"source":"Q10. Predict the result of the following code."},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"True\nFalse\n"}],"source":"print (np.allclose([3], [2.999999]))\nprint (np.array_equal([3], [2.999999]))"},{"cell_type":"markdown","metadata":{},"source":["Q11. Write numpy comparison functions such that they return the results as you see."]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ True False]\n[ True True]\n[False False]\n[False True]\n"}],"source":"x = np.array([4, 5])\ny = np.array([2, 5])\nprint (np.greater(x, y))\nprint (np.greater_equal(x, y))\nprint (np.less(x, y))\nprint (np.less_equal(x, y))\n"},{"cell_type":"markdown","metadata":{},"source":["Q12. Predict the result of the following code."]},{"cell_type":"code","execution_count":14,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ True False]\n[ True True]\n"}],"source":"print (np.equal([1, 2], [1, 2.000001]))\nprint (np.isclose([1, 2], [1, 2.000001]))"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":""}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
\ No newline at end of file
diff --git a/9_Mathematical_functions_solutions.ipynb b/9_Mathematical_functions_solutions.ipynb
index ccad259..379ee57 100644
--- a/9_Mathematical_functions_solutions.ipynb
+++ b/9_Mathematical_functions_solutions.ipynb
@@ -1,1193 +1 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Mathematical functions"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": [
- "import numpy as np"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1.11.2'"
- ]
- },
- "execution_count": 2,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "np.__version__"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {
- "collapsed": false
- },
- "outputs": [],
- "source": [
- "__author__ = \"kyubyong. kbpark.linguist@gmail.com. https://github.com/kyubyong\""
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Trigonometric functions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q1. Calculate sine, cosine, and tangent of x, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "sine: [ 0. 0.84147098 -0.98803162 0.89399666]\n",
- "cosine: [ 1. 0.54030231 0.15425145 -0.44807362]\n",
- "tangent: [ 0. 1.55740772 -6.4053312 -1.99520041]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([0., 1., 30, 90])\n",
- "print \"sine:\", np.sin(x)\n",
- "print \"cosine:\", np.cos(x)\n",
- "print \"tangent:\", np.tan(x)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q2. Calculate inverse sine, inverse cosine, and inverse tangent of x, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "inverse sine: [ 1.57079633 0. 1.57079633]\n",
- "inverse cosine: [ 0. 1.57079633 0. ]\n",
- "inverse tangent: [ 0.78539816 0. 0.78539816]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([-1., 0, 1.])\n",
- "print \"inverse sine:\", np.arcsin(x2)\n",
- "print \"inverse cosine:\", np.arccos(x2)\n",
- "print \"inverse tangent:\", np.arctan(x2)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q3. Convert angles from radians to degrees."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 45,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[-180. -90. 90. 180.]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])\n",
- "\n",
- "out1 = np.degrees(x)\n",
- "out2 = np.rad2deg(x)\n",
- "assert np.array_equiv(out1, out2)\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q4. Convert angles from degrees to radians."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[-3.14159265 -1.57079633 1.57079633 3.14159265]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([-180., -90., 90., 180.])\n",
- "\n",
- "out1 = np.radians(x)\n",
- "out2 = np.deg2rad(x)\n",
- "assert np.array_equiv(out1, out2)\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Hyperbolic functions"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q5. Calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent of x, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 65,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[-1.17520119 0. 1.17520119]\n",
- "[ 1.54308063 1. 1.54308063]\n",
- "[-0.76159416 0. 0.76159416]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([-1., 0, 1.])\n",
- "print np.sinh(x)\n",
- "print np.cosh(x)\n",
- "print np.tanh(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Rounding"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q6. Predict the results of these, paying attention to the difference among the family functions."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 84,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 2. 2. 2. 3. -2. -2. -3.]\n",
- "[ 2. 1. 2. 2. -3. -3. -3.]\n",
- "[ 3. 2. 3. 3. -2. -2. -2.]\n",
- "[ 2. 1. 2. 2. -2. -2. -2.]\n",
- "[2.0, 2.0, 3.0, 3.0, -2.0, -3.0, -3.0]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([2.1, 1.5, 2.5, 2.9, -2.1, -2.5, -2.9])\n",
- "\n",
- "out1 = np.around(x)\n",
- "out2 = np.floor(x)\n",
- "out3 = np.ceil(x)\n",
- "out4 = np.trunc(x)\n",
- "out5 = [round(elem) for elem in x]\n",
- "\n",
- "print out1\n",
- "print out2\n",
- "print out3\n",
- "print out4\n",
- "print out5\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q7. Implement out5 in the above question using numpy."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 87,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 2. 2. 3. 3. -2. -3. -3.]\n"
- ]
- }
- ],
- "source": [
- "print np.floor(np.abs(x) + 0.5) * np.sign(x) \n",
- "# Read http://numpy-discussion.10968.n7.nabble.com/why-numpy-round-get-a-different-result-from-python-round-function-td19098.html"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Sums, products, differences"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q8. Predict the results of these."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 99,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "('->', 36)\n",
- "('->', array([ 6, 8, 10, 12]))\n",
- "('->', array([[10],\n",
- " [26]]))\n",
- "\n",
- "('->', 40320)\n",
- "('->', array([ 5, 12, 21, 32]))\n",
- "('->', array([[ 24],\n",
- " [1680]]))\n",
- "\n",
- "('->', array([ 1, 3, 6, 10, 15, 21, 28, 36]))\n",
- "('->', array([[ 1, 2, 3, 4],\n",
- " [ 6, 8, 10, 12]]))\n",
- "('->', array([[ 1, 3, 6, 10],\n",
- " [ 5, 11, 18, 26]]))\n",
- "\n",
- "('->', array([ 1, 2, 6, 24, 120, 720, 5040, 40320]))\n",
- "('->', array([[ 1, 2, 3, 4],\n",
- " [ 5, 12, 21, 32]]))\n",
- "('->', array([[ 1, 2, 6, 24],\n",
- " [ 5, 30, 210, 1680]]))\n",
- "\n",
- "('->', 1)\n",
- "('->', array([1, 2, 3, 4]))\n",
- "('->', array([[1],\n",
- " [5]]))\n",
- "\n",
- "('->', 8)\n",
- "('->', array([5, 6, 7, 8]))\n",
- "('->', array([[4],\n",
- " [8]]))\n",
- "\n",
- "('->', 4.5)\n",
- "('->', array([ 3., 4., 5., 6.]))\n",
- "('->', array([[ 2.5],\n",
- " [ 6.5]]))\n"
- ]
- },
- {
- "name": "stderr",
- "output_type": "stream",
- "text": [
- "/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:34: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n"
- ]
- }
- ],
- "source": [
- "x = np.array(\n",
- " [[1, 2, 3, 4],\n",
- " [5, 6, 7, 8]])\n",
- "\n",
- "outs = [np.sum(x),\n",
- " np.sum(x, axis=0),\n",
- " np.sum(x, axis=1, keepdims=True),\n",
- " \"\",\n",
- " np.prod(x),\n",
- " np.prod(x, axis=0),\n",
- " np.prod(x, axis=1, keepdims=True),\n",
- " \"\",\n",
- " np.cumsum(x),\n",
- " np.cumsum(x, axis=0),\n",
- " np.cumsum(x, axis=1),\n",
- " \"\",\n",
- " np.cumprod(x),\n",
- " np.cumprod(x, axis=0),\n",
- " np.cumprod(x, axis=1),\n",
- " \"\",\n",
- " np.min(x),\n",
- " np.min(x, axis=0),\n",
- " np.min(x, axis=1, keepdims=True),\n",
- " \"\",\n",
- " np.max(x),\n",
- " np.max(x, axis=0),\n",
- " np.max(x, axis=1, keepdims=True),\n",
- " \"\",\n",
- " np.mean(x),\n",
- " np.mean(x, axis=0),\n",
- " np.mean(x, axis=1, keepdims=True)]\n",
- " \n",
- "for out in outs:\n",
- " if out == \"\":\n",
- " print\n",
- " else:\n",
- " print(\"->\", out)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q9. Calculate the difference between neighboring elements, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 100,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 1 2 3 -7]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, 2, 4, 7, 0])\n",
- "print np.diff(x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q10. Calculate the difference between neighboring elements, element-wise, and\n",
- "prepend [0, 0] and append[100] to it."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 108,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 0 0 1 2 3 -7 100]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, 2, 4, 7, 0])\n",
- "\n",
- "out1 = np.ediff1d(x, to_begin=[0, 0], to_end=[100])\n",
- "out2 = np.insert(np.append(np.diff(x), 100), 0, [0, 0])\n",
- "assert np.array_equiv(out1, out2)\n",
- "print out2"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q11. Return the cross product of x and y."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 110,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[-3 6 -3]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, 2, 3])\n",
- "y = np.array([4, 5, 6])\n",
- "\n",
- "print np.cross(x, y)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Exponents and logarithms"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q12. Compute $e^x$, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 115,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 2.71828175 7.38905621 20.08553696]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1., 2., 3.], np.float32)\n",
- "\n",
- "out = np.exp(x)\n",
- "print out\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q13. Calculate exp(x) - 1 for all elements in x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 118,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 1.71828175 6.38905621 19.08553696]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1., 2., 3.], np.float32)\n",
- "\n",
- "out1 = np.expm1(x)\n",
- "out2 = np.exp(x) - 1.\n",
- "assert np.allclose(out1, out2)\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q14. Calculate $2^p$ for all p in x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 124,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 2. 4. 8.]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1., 2., 3.], np.float32)\n",
- "\n",
- "out1 = np.exp2(x)\n",
- "out2 = 2 ** x\n",
- "assert np.allclose(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q15. Compute natural, base 10, and base 2 logarithms of x element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 128,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "natural log = [ 0. 1. 2.]\n",
- "common log = [ 0. 0.43429448 0.86858896]\n",
- "base 2 log = [ 0. 1.44269504 2.88539008]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, np.e, np.e**2])\n",
- "\n",
- "print \"natural log =\", np.log(x)\n",
- "print \"common log =\", np.log10(x)\n",
- "print \"base 2 log =\", np.log2(x)\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q16. Compute the natural logarithm of one plus each element in x in floating-point accuracy."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 131,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 1.00000000e-099 1.00000000e-100]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1e-99, 1e-100])\n",
- "\n",
- "print np.log1p(x)\n",
- "# Compare it with np.log(1 +x)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Floating point routines"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q17. Return element-wise True where signbit is set."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 135,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ True True True False False False False]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([-3, -2, -1, 0, 1, 2, 3])\n",
- "\n",
- "out1 = np.signbit(x)\n",
- "out2 = x < 0\n",
- "assert np.array_equiv(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q18. Change the sign of x to that of y, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 140,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[-1. -0. -1.]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([-1, 0, 1])\n",
- "y = -1.1\n",
- "\n",
- "print np.copysign(x, y)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Arithmetic operations"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q19. Add x and y element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 141,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0 0 0]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, 2, 3])\n",
- "y = np.array([-1, -2, -3])\n",
- "\n",
- "out1 = np.add(x, y)\n",
- "out2 = x + y\n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q20. Subtract y from x element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 142,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0 1 2]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([3, 4, 5])\n",
- "y = np.array(3)\n",
- "\n",
- "out1 = np.subtract(x, y)\n",
- "out2 = x - y \n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q21. Multiply x by y element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 144,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 3 0 -5]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([3, 4, 5])\n",
- "y = np.array([1, 0, -1])\n",
- "\n",
- "out1 = np.multiply(x, y)\n",
- "out2 = x * y \n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q22. Divide x by y element-wise in two different ways."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 161,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 3. 2. 1.66666667]\n",
- "[ 3. 2. 1.]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([3., 4., 5.])\n",
- "y = np.array([1., 2., 3.])\n",
- "\n",
- "out1 = np.true_divide(x, y)\n",
- "out2 = x / y \n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n",
- "\n",
- "out3 = np.floor_divide(x, y)\n",
- "out4 = x // y\n",
- "assert np.array_equal(out3, out4)\n",
- "print out3\n",
- "\n",
- "# Note that in Python 2 and 3, the handling of `divide` differs.\n",
- "# See https://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html#numpy.divide\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q23. Compute numerical negative value of x, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 146,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[-1 1]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, -1])\n",
- "\n",
- "out1 = np.negative(x)\n",
- "out2 = -x\n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q24. Compute the reciprocal of x, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 155,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 1. 0.5 5. ]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1., 2., .2])\n",
- "\n",
- "out1 = np.reciprocal(x)\n",
- "out2 = 1/x\n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q25. Compute $x^y$, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 163,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[ 1 4]\n",
- " [ 3 16]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 2], [3, 4]])\n",
- "y = np.array([[1, 2], [1, 2]])\n",
- "\n",
- "out = np.power(x, y)\n",
- "print out\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q26. Compute the remainder of x / y element-wise in two different ways."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 168,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1 0 1 1 0 1]\n",
- "[-1 0 -1 1 0 1]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([-3, -2, -1, 1, 2, 3])\n",
- "y = 2\n",
- "\n",
- "out1 = np.mod(x, y)\n",
- "out2 = x % y\n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n",
- "\n",
- "out3 = np.fmod(x, y)\n",
- "print out3"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "## Miscellaneous"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q27. If an element of x is smaller than 3, replace it with 3.\n",
- "And if an element of x is bigger than 7, replace it with 7."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 174,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[3 3 3 3 4 5 6 7 7 7]\n"
- ]
- }
- ],
- "source": [
- "x = np.arange(10)\n",
- "\n",
- "out1 = np.clip(x, 3, 7)\n",
- "out2 = np.copy(x)\n",
- "out2[out2 < 3] = 3\n",
- "out2[out2 > 7] = 7\n",
- "\n",
- "assert np.array_equiv(out1, out2)\n",
- "print out1"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q28. Compute the square of x, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 176,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1 4 1]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, 2, -1])\n",
- "\n",
- "out1 = np.square(x)\n",
- "out2 = x * x\n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q29. Compute square root of x element-wise.\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 177,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 1. 2. 3.]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1., 4., 9.])\n",
- "\n",
- "out = np.sqrt(x)\n",
- "print out\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q30. Compute the absolute value of x."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 178,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[1 1]\n",
- " [3 3]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, -1], [3, -3]])\n",
- "\n",
- "out = np.abs(x)\n",
- "print out\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "Q31. Compute an element-wise indication of the sign of x, element-wise."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 181,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[ 1 1 0 -1 -1]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([1, 3, 0, -1, -3])\n",
- "\n",
- "out1 = np.sign(x)\n",
- "out2 = np.copy(x)\n",
- "out2[out2 > 0] = 1\n",
- "out2[out2 < 0] = -1\n",
- "assert np.array_equal(out1, out2)\n",
- "print out1\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 2",
- "language": "python",
- "name": "python2"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 2
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython2",
- "version": "2.7.6"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
+{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Mathematical functions"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[],"source":"import numpy as np"},{"cell_type":"code","execution_count":2,"metadata":{},"outputs":[{"data":{"text/plain":"'1.16.5'"},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":"np.__version__"},{"cell_type":"code","execution_count":3,"metadata":{},"outputs":[],"source":"__author__ = \"kyubyong. kbpark.linguist@gmail.com. https://github.com/kyubyong\""},{"cell_type":"markdown","metadata":{},"source":["## Trigonometric functions"]},{"cell_type":"markdown","metadata":{},"source":["Q1. Calculate sine, cosine, and tangent of x, element-wise."]},{"cell_type":"code","execution_count":4,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"sine: [ 0. 0.84147098 -0.98803162 0.89399666]\ncosine: [ 1. 0.54030231 0.15425145 -0.44807362]\ntangent: [ 0. 1.55740772 -6.4053312 -1.99520041]\n"}],"source":"x = np.array([0., 1., 30, 90])\nprint (\"sine:\", np.sin(x))\nprint (\"cosine:\", np.cos(x))\nprint (\"tangent:\", np.tan(x))\n"},{"cell_type":"markdown","metadata":{},"source":["Q2. Calculate inverse sine, inverse cosine, and inverse tangent of x, element-wise."]},{"cell_type":"code","execution_count":6,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"inverse sine: [-1.57079633 0. 1.57079633]\ninverse cosine: [3.14159265 1.57079633 0. ]\ninverse tangent: [-0.78539816 0. 0.78539816]\n"}],"source":"x = np.array([-1., 0, 1.])\nprint (\"inverse sine:\", np.arcsin(x))\nprint (\"inverse cosine:\", np.arccos(x))\nprint (\"inverse tangent:\", np.arctan(x))"},{"cell_type":"markdown","metadata":{},"source":["Q3. Convert angles from radians to degrees."]},{"cell_type":"code","execution_count":7,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[-180. -90. 90. 180.]\n"}],"source":"x = np.array([-np.pi, -np.pi/2, np.pi/2, np.pi])\n\nout1 = np.degrees(x)\nout2 = np.rad2deg(x)\nassert np.array_equiv(out1, out2)\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":["Q4. Convert angles from degrees to radians."]},{"cell_type":"code","execution_count":8,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[-3.14159265 -1.57079633 1.57079633 3.14159265]\n"}],"source":"x = np.array([-180., -90., 90., 180.])\n\nout1 = np.radians(x)\nout2 = np.deg2rad(x)\nassert np.array_equiv(out1, out2)\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":"## Hyperbolic functions"},{"cell_type":"markdown","metadata":{},"source":["Q5. Calculate hyperbolic sine, hyperbolic cosine, and hyperbolic tangent of x, element-wise."]},{"cell_type":"code","execution_count":9,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[-1.17520119 0. 1.17520119]\n[1.54308063 1. 1.54308063]\n[-0.76159416 0. 0.76159416]\n"}],"source":"x = np.array([-1., 0, 1.])\nprint (np.sinh(x))\nprint (np.cosh(x))\nprint (np.tanh(x))"},{"cell_type":"markdown","metadata":{},"source":["## Rounding"]},{"cell_type":"markdown","metadata":{},"source":["Q6. Predict the results of these, paying attention to the difference among the family functions."]},{"cell_type":"code","execution_count":10,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ 2. 2. 2. 3. -2. -2. -3.]\n[ 2. 1. 2. 2. -3. -3. -3.]\n[ 3. 2. 3. 3. -2. -2. -2.]\n[ 2. 1. 2. 2. -2. -2. -2.]\n[2.0, 2.0, 2.0, 3.0, -2.0, -2.0, -3.0]\n"}],"source":"x = np.array([2.1, 1.5, 2.5, 2.9, -2.1, -2.5, -2.9])\n\nout1 = np.around(x)\nout2 = np.floor(x)\nout3 = np.ceil(x)\nout4 = np.trunc(x)\nout5 = [round(elem) for elem in x]\n\nprint (out1)\nprint (out2)\nprint (out3)\nprint (out4)\nprint (out5)\n"},{"cell_type":"markdown","metadata":{},"source":["Q7. Implement out5 in the above question using numpy."]},{"cell_type":"code","execution_count":11,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ 2. 2. 3. 3. -2. -3. -3.]\n"}],"source":"print (np.floor(np.abs(x) + 0.5) * np.sign(x))\n# Read http://numpy-discussion.10968.n7.nabble.com/why-numpy-round-get-a-different-result-from-python-round-function-td19098.html"},{"cell_type":"markdown","metadata":{},"source":["## Sums, products, differences"]},{"cell_type":"markdown","metadata":{},"source":["Q8. Predict the results of these."]},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"-> 36\n-> [ 6 8 10 12]\n-> [[10]\n [26]]\n-> 40320\n-> [ 5 12 21 32]\n-> [[ 24]\n [1680]]\n-> [ 1 3 6 10 15 21 28 36]\n-> [[ 1 2 3 4]\n [ 6 8 10 12]]\n-> [[ 1 3 6 10]\n [ 5 11 18 26]]\n-> [ 1 2 6 24 120 720 5040 40320]\n-> [[ 1 2 3 4]\n [ 5 12 21 32]]\n-> [[ 1 2 6 24]\n [ 5 30 210 1680]]\n-> 1\n-> [1 2 3 4]\n-> [[1]\n [5]]\n-> 8\n-> [5 6 7 8]\n-> [[4]\n [8]]\n-> 4.5\n-> [3. 4. 5. 6.]\n-> [[2.5]\n [6.5]]\nC:\\Users\\zhaijianfeng\\AppData\\Local\\Continuum\\anaconda\\lib\\site-packages\\ipykernel_launcher.py:34: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n"}],"source":"x = np.array(\n [[1, 2, 3, 4],\n [5, 6, 7, 8]])\n\nouts = [np.sum(x),\n np.sum(x, axis=0),\n np.sum(x, axis=1, keepdims=True),\n \"\",\n np.prod(x),\n np.prod(x, axis=0),\n np.prod(x, axis=1, keepdims=True),\n \"\",\n np.cumsum(x),\n np.cumsum(x, axis=0),\n np.cumsum(x, axis=1),\n \"\",\n np.cumprod(x),\n np.cumprod(x, axis=0),\n np.cumprod(x, axis=1),\n \"\",\n np.min(x),\n np.min(x, axis=0),\n np.min(x, axis=1, keepdims=True),\n \"\",\n np.max(x),\n np.max(x, axis=0),\n np.max(x, axis=1, keepdims=True),\n \"\",\n np.mean(x),\n np.mean(x, axis=0),\n np.mean(x, axis=1, keepdims=True)]\n \nfor out in outs:\n if out == \"\":\n print\n else:\n print(\"->\", out)\n"},{"cell_type":"markdown","metadata":{},"source":["Q9. Calculate the difference between neighboring elements, element-wise."]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ 1 2 3 -7]\n"}],"source":"x = np.array([1, 2, 4, 7, 0])\nprint (np.diff(x))"},{"cell_type":"markdown","metadata":{},"source":"Q10. Calculate the difference between neighboring elements, element-wise, and\nprepend [0, 0] and append[100] to it."},{"cell_type":"code","execution_count":14,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ 0 0 1 2 3 -7 100]\n"}],"source":"x = np.array([1, 2, 4, 7, 0])\n\nout1 = np.ediff1d(x, to_begin=[0, 0], to_end=[100])\nout2 = np.insert(np.append(np.diff(x), 100), 0, [0, 0])\nassert np.array_equiv(out1, out2)\nprint (out2)"},{"cell_type":"markdown","metadata":{},"source":["Q11. Return the cross product of x and y."]},{"cell_type":"code","execution_count":15,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[-3 6 -3]\n"}],"source":"x = np.array([1, 2, 3])\ny = np.array([4, 5, 6])\n\nprint (np.cross(x, y))"},{"cell_type":"markdown","metadata":{},"source":["## Exponents and logarithms"]},{"cell_type":"markdown","metadata":{},"source":["Q12. Compute $e^x$, element-wise."]},{"cell_type":"code","execution_count":16,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ 2.7182817 7.389056 20.085537 ]\n"}],"source":"x = np.array([1., 2., 3.], np.float32)\n\nout = np.exp(x)\nprint (out)\n"},{"cell_type":"markdown","metadata":{},"source":["Q13. Calculate exp(x) - 1 for all elements in x."]},{"cell_type":"code","execution_count":17,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ 1.7182817 6.389056 19.085537 ]\n"}],"source":"x = np.array([1., 2., 3.], np.float32)\n\nout1 = np.expm1(x)\nout2 = np.exp(x) - 1.\nassert np.allclose(out1, out2)\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":["Q14. Calculate $2^p$ for all p in x."]},{"cell_type":"code","execution_count":18,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[2. 4. 8.]\n"}],"source":"x = np.array([1., 2., 3.], np.float32)\n\nout1 = np.exp2(x)\nout2 = 2 ** x\nassert np.allclose(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q15. Compute natural, base 10, and base 2 logarithms of x element-wise."]},{"cell_type":"code","execution_count":19,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"natural log = [0. 1. 2.]\ncommon log = [0. 0.43429448 0.86858896]\nbase 2 log = [0. 1.44269504 2.88539008]\n"}],"source":"x = np.array([1, np.e, np.e**2])\n\nprint (\"natural log =\", np.log(x))\nprint (\"common log =\", np.log10(x))\nprint (\"base 2 log =\", np.log2(x))\n"},{"cell_type":"markdown","metadata":{},"source":["Q16. Compute the natural logarithm of one plus each element in x in floating-point accuracy."]},{"cell_type":"code","execution_count":20,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1.e-099 1.e-100]\n[0. 0.]\n"}],"source":"x = np.array([1e-99, 1e-100])\n\nprint (np.log1p(x))\n# Compare it with np.log(1 +x)\nprint (np.log(1 +x))"},{"cell_type":"markdown","metadata":{},"source":["## Floating point routines"]},{"cell_type":"markdown","metadata":{},"source":["Q17. Return element-wise True where signbit is set."]},{"cell_type":"code","execution_count":21,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ True True True False False False False]\n"}],"source":"x = np.array([-3, -2, -1, 0, 1, 2, 3])\n\nout1 = np.signbit(x)\nout2 = x < 0\nassert np.array_equiv(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q18. Change the sign of x to that of y, element-wise."]},{"cell_type":"code","execution_count":22,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[-1. -0. -1.]\n"}],"source":"x = np.array([-1, 0, 1])\ny = -1.1\n\nprint (np.copysign(x, y))"},{"cell_type":"markdown","metadata":{},"source":["## Arithmetic operations"]},{"cell_type":"markdown","metadata":{},"source":["Q19. Add x and y element-wise."]},{"cell_type":"code","execution_count":23,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0 0 0]\n"}],"source":"x = np.array([1, 2, 3])\ny = np.array([-1, -2, -3])\n\nout1 = np.add(x, y)\nout2 = x + y\nassert np.array_equal(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q20. Subtract y from x element-wise."]},{"cell_type":"code","execution_count":24,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[0 1 2]\n"}],"source":"x = np.array([3, 4, 5])\ny = np.array(3)\n\nout1 = np.subtract(x, y)\nout2 = x - y \nassert np.array_equal(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q21. Multiply x by y element-wise."]},{"cell_type":"code","execution_count":25,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ 3 0 -5]\n"}],"source":"x = np.array([3, 4, 5])\ny = np.array([1, 0, -1])\n\nout1 = np.multiply(x, y)\nout2 = x * y \nassert np.array_equal(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q22. Divide x by y element-wise in two different ways."]},{"cell_type":"code","execution_count":26,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[3. 2. 1.66666667]\n[3. 2. 1.]\n"}],"source":"x = np.array([3., 4., 5.])\ny = np.array([1., 2., 3.])\n\nout1 = np.true_divide(x, y)\nout2 = x / y \nassert np.array_equal(out1, out2)\nprint (out1)\n\nout3 = np.floor_divide(x, y)\nout4 = x // y\nassert np.array_equal(out3, out4)\nprint (out3)\n\n# Note that in Python 2 and 3, the handling of `divide` differs.\n# See https://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html#numpy.divide\n"},{"cell_type":"markdown","metadata":{},"source":["Q23. Compute numerical negative value of x, element-wise."]},{"cell_type":"code","execution_count":27,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[-1 1]\n"}],"source":"x = np.array([1, -1])\n\nout1 = np.negative(x)\nout2 = -x\nassert np.array_equal(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q24. Compute the reciprocal of x, element-wise."]},{"cell_type":"code","execution_count":28,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1. 0.5 5. ]\n"}],"source":"x = np.array([1., 2., .2])\n\nout1 = np.reciprocal(x)\nout2 = 1/x\nassert np.array_equal(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q25. Compute $x^y$, element-wise."]},{"cell_type":"code","execution_count":29,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[ 1 4]\n [ 3 16]]\n"}],"source":"x = np.array([[1, 2], [3, 4]])\ny = np.array([[1, 2], [1, 2]])\n\nout = np.power(x, y)\nprint (out)\n"},{"cell_type":"markdown","metadata":{},"source":["Q26. Compute the remainder of x / y element-wise in two different ways."]},{"cell_type":"code","execution_count":31,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1 0 1 1 0 1]\n[-1 0 -1 1 0 1]\n"}],"source":"x = np.array([-3, -2, -1, 1, 2, 3])\ny = 2\n\nout1 = np.mod(x, y)\nout2 = x % y\nassert np.array_equal(out1, out2)\nprint (out1)\n\nout3 = np.fmod(x, y)\nprint (out3)"},{"cell_type":"markdown","metadata":{},"source":["## Miscellaneous"]},{"cell_type":"markdown","metadata":{},"source":["Q27. If an element of x is smaller than 3, replace it with 3.\n","And if an element of x is bigger than 7, replace it with 7."]},{"cell_type":"code","execution_count":32,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[3 3 3 3 4 5 6 7 7 7]\n"}],"source":"x = np.arange(10)\n\nout1 = np.clip(x, 3, 7)\nout2 = np.copy(x)\nout2[out2 < 3] = 3\nout2[out2 > 7] = 7\n\nassert np.array_equiv(out1, out2)\nprint (out1)"},{"cell_type":"markdown","metadata":{},"source":["Q28. Compute the square of x, element-wise."]},{"cell_type":"code","execution_count":33,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1 4 1]\n"}],"source":"x = np.array([1, 2, -1])\n\nout1 = np.square(x)\nout2 = x * x\nassert np.array_equal(out1, out2)\nprint (out1)\n"},{"cell_type":"markdown","metadata":{},"source":["Q29. Compute square root of x element-wise.\n"]},{"cell_type":"code","execution_count":34,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[1. 2. 3.]\n"}],"source":"x = np.array([1., 4., 9.])\n\nout = np.sqrt(x)\nprint (out)\n"},{"cell_type":"markdown","metadata":{},"source":["Q30. Compute the absolute value of x."]},{"cell_type":"code","execution_count":35,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[[1 1]\n [3 3]]\n"}],"source":"x = np.array([[1, -1], [3, -3]])\n\nout = np.abs(x)\nprint (out)\n"},{"cell_type":"markdown","metadata":{},"source":["Q31. Compute an element-wise indication of the sign of x, element-wise."]},{"cell_type":"code","execution_count":36,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":"[ 1 1 0 -1 -1]\n"}],"source":"x = np.array([1, 3, 0, -1, -3])\n\nout1 = np.sign(x)\nout2 = np.copy(x)\nout2[out2 > 0] = 1\nout2[out2 < 0] = -1\nassert np.array_equal(out1, out2)\nprint (out1)\n"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":true},"outputs":[],"source":""}],"nbformat":4,"nbformat_minor":2,"metadata":{"language_info":{"name":"python","codemirror_mode":{"name":"ipython","version":3}},"orig_nbformat":2,"file_extension":".py","mimetype":"text/x-python","name":"python","npconvert_exporter":"python","pygments_lexer":"ipython3","version":3}}
\ No newline at end of file