diff --git a/Random_sampling.ipynb b/10_Random_sampling.ipynb
similarity index 100%
rename from Random_sampling.ipynb
rename to 10_Random_sampling.ipynb
diff --git a/Random_sampling_Solutions.ipynb b/10_Random_sampling_Solutions.ipynb
similarity index 100%
rename from Random_sampling_Solutions.ipynb
rename to 10_Random_sampling_Solutions.ipynb
diff --git a/Set_routines.ipynb b/11_Set_routines.ipynb
similarity index 77%
rename from Set_routines.ipynb
rename to 11_Set_routines.ipynb
index 12ebb12..7f740eb 100644
--- a/Set_routines.ipynb
+++ b/11_Set_routines.ipynb
@@ -1,270 +1,247 @@
-{
- "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",
- "\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"
- ]
- },
- {
- "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"
- ]
- },
- {
- "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"
- ]
- },
- {
- "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",
- "\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"
- ]
- },
- {
- "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": 4,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "import numpy as np"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "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": {},
+ "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": {},
+ "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",
+ "\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": {},
+ "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"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Q3. Find the unique intersection of x and y."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {},
+ "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"
+ ]
+ },
+ {
+ "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": {},
+ "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"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Q5. Find the xor elements of x and y."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 40,
+ "metadata": {},
+ "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"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Q6. Find the union of x and y."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 42,
+ "metadata": {},
+ "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"
+ ]
+ },
+ {
+ "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
+}
diff --git a/Set_routines_Solutions.ipynb b/11_Set_routines_Solutions.ipynb
similarity index 100%
rename from Set_routines_Solutions.ipynb
rename to 11_Set_routines_Solutions.ipynb
diff --git a/Sorting_searching_and_counting.ipynb b/12_Sorting_searching_and_counting.ipynb
similarity index 100%
rename from Sorting_searching_and_counting.ipynb
rename to 12_Sorting_searching_and_counting.ipynb
diff --git a/Sorting_searching_and_counting_Solutions.ipynb b/12_Sorting_searching_and_counting_Solutions.ipynb
similarity index 87%
rename from Sorting_searching_and_counting_Solutions.ipynb
rename to 12_Sorting_searching_and_counting_Solutions.ipynb
index c5a630e..b26aab0 100644
--- a/Sorting_searching_and_counting_Solutions.ipynb
+++ b/12_Sorting_searching_and_counting_Solutions.ipynb
@@ -1,479 +1,453 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# Soring, searching, and counting"
- ]
- },
- {
- "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": 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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": 100,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[0 1 2 3 4 5]\n"
- ]
- }
- ],
- "source": [
- "x = np.array([[1, 2, 3], [1, 3, 5]])\n",
- "print np.flatnonzero(x)\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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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 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": [
+ "# 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
+}
diff --git a/Statistics.ipynb b/13_Statistics.ipynb
similarity index 100%
rename from Statistics.ipynb
rename to 13_Statistics.ipynb
diff --git a/Statistics_solutions.ipynb b/13_Statistics_solutions.ipynb
similarity index 100%
rename from Statistics_solutions.ipynb
rename to 13_Statistics_solutions.ipynb
diff --git a/Array_creation_routines.ipynb b/1_Array_creation_routines.ipynb
similarity index 100%
rename from Array_creation_routines.ipynb
rename to 1_Array_creation_routines.ipynb
diff --git a/Array_creation_routines_solution.ipynb b/1_Array_creation_routines_Solution.ipynb
similarity index 100%
rename from Array_creation_routines_solution.ipynb
rename to 1_Array_creation_routines_Solution.ipynb
diff --git a/Array_manipulation_routines.ipynb b/2_Array_manipulation_routines.ipynb
similarity index 100%
rename from Array_manipulation_routines.ipynb
rename to 2_Array_manipulation_routines.ipynb
diff --git a/Array_manipulation_routines_Solutions.ipynb b/2_Array_manipulation_routines_Solutions.ipynb
similarity index 88%
rename from Array_manipulation_routines_Solutions.ipynb
rename to 2_Array_manipulation_routines_Solutions.ipynb
index 49584ee..aa56247 100644
--- a/Array_manipulation_routines_Solutions.ipynb
+++ b/2_Array_manipulation_routines_Solutions.ipynb
@@ -1,860 +1,815 @@
-{
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": 54,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[[1 4]\n",
- " [2 5]\n",
- " [3 6]]\n"
- ]
- }
- ],
- "source": [
- "x = np.array((1,2,3))\n",
- "y = np.array((4,5,6))\n",
- "out1 = np.column_stack((x, y))\n",
- "out2 = 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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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": {
- "collapsed": false
- },
- "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 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": [
+ "# 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
+}
diff --git a/String_operations.ipynb b/3_String_operations.ipynb
similarity index 100%
rename from String_operations.ipynb
rename to 3_String_operations.ipynb
diff --git a/String_operations_solutions.ipynb b/3_String_operations_solutions.ipynb
similarity index 100%
rename from String_operations_solutions.ipynb
rename to 3_String_operations_solutions.ipynb
diff --git a/Numpy-specific_help_functions.ipynb b/4_Numpy-specific_help_functions.ipynb
similarity index 100%
rename from Numpy-specific_help_functions.ipynb
rename to 4_Numpy-specific_help_functions.ipynb
diff --git a/Numpy-specific_help_functions_Solutions.ipynb b/4_Numpy-specific_help_functions_Solutions.ipynb
similarity index 100%
rename from Numpy-specific_help_functions_Solutions.ipynb
rename to 4_Numpy-specific_help_functions_Solutions.ipynb
diff --git a/Input_and_Output.ipynb b/5_Input_and_Output.ipynb
similarity index 100%
rename from Input_and_Output.ipynb
rename to 5_Input_and_Output.ipynb
diff --git a/Input_and_Output_Solutions.ipynb b/5_Input_and_Output_Solutions.ipynb
similarity index 100%
rename from Input_and_Output_Solutions.ipynb
rename to 5_Input_and_Output_Solutions.ipynb
diff --git a/Linear_algebra.ipynb b/6_Linear_algebra.ipynb
similarity index 100%
rename from Linear_algebra.ipynb
rename to 6_Linear_algebra.ipynb
diff --git a/Linear_algebra_Solutions.ipynb b/6_Linear_algebra_Solutions.ipynb
similarity index 100%
rename from Linear_algebra_Solutions.ipynb
rename to 6_Linear_algebra_Solutions.ipynb
diff --git a/Discrete_Fourier_Transform.ipynb b/7_Discrete_Fourier_Transform.ipynb
similarity index 100%
rename from Discrete_Fourier_Transform.ipynb
rename to 7_Discrete_Fourier_Transform.ipynb
diff --git a/Discrete_Fourier_Transform_solutions.ipynb b/7_Discrete_Fourier_Transform_solutions.ipynb
similarity index 100%
rename from Discrete_Fourier_Transform_solutions.ipynb
rename to 7_Discrete_Fourier_Transform_solutions.ipynb
diff --git a/Logic_functions.ipynb b/8_Logic_functions.ipynb
similarity index 100%
rename from Logic_functions.ipynb
rename to 8_Logic_functions.ipynb
diff --git a/Logic_functions_Solutions.ipynb b/8_Logic_functions_Solutions.ipynb
similarity index 100%
rename from Logic_functions_Solutions.ipynb
rename to 8_Logic_functions_Solutions.ipynb
diff --git a/Mathematical_functions.ipynb b/9_Mathematical_functions.ipynb
similarity index 100%
rename from Mathematical_functions.ipynb
rename to 9_Mathematical_functions.ipynb
diff --git a/Mathematical_functions_solutions.ipynb b/9_Mathematical_functions_solutions.ipynb
similarity index 100%
rename from Mathematical_functions_solutions.ipynb
rename to 9_Mathematical_functions_solutions.ipynb