From 5de6705882188bd42b222cae408111bcef40aae5 Mon Sep 17 00:00:00 2001 From: Surya S Date: Fri, 27 Jan 2017 09:59:40 +0530 Subject: [PATCH] Added Files --- Array_creation_routines.ipynb | 1637 +++++++++--------- Array_manipulation_routines.ipynb | 1485 ++++++++-------- Array_manipulation_routines_Solutions.ipynb | 1721 ++++++++++--------- Linear_algebra.ipynb | 1034 +++++------ Logic_functions.ipynb | 9 +- Random_sampling.ipynb | 606 +++---- 6 files changed, 3284 insertions(+), 3208 deletions(-) diff --git a/Array_creation_routines.ipynb b/Array_creation_routines.ipynb index 50b16b2..f7ad43c 100644 --- a/Array_creation_routines.ipynb +++ b/Array_creation_routines.ipynb @@ -1,798 +1,839 @@ -{ - "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": [] - }, - { - "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" - ] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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" - ] - }, - { - "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": [] - }, - { - "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" - ] - }, - { - "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": [] - }, - { - "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" - ] - }, - { - "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": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let x = [1, 2]. Conver 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" - ] - }, - { - "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" - ] - }, - { - "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" - ] - }, - { - "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" - ] - }, - { - "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" - ] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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" - ] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create an array which looks like below. array([[ 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0.]])\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": [] - }, - { - "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": [] - } - ], - "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 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.identity(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.eye(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],np.float32)" + ] + }, + { + "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],np.float32)" + ] + }, + { + "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.ones([2,5],np.int32)*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.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]. Conver 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.array(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.matrix(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.astype(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": "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": "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. array([[ 0., 0., 0., 0., 0.], [ 1., 0., 0., 0., 0.], [ 1., 1., 0., 0., 0.]])\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": { + "anaconda-cloud": {}, + "kernelspec": { + "display_name": "Python [conda root]", + "language": "python", + "name": "conda-root-py" + }, + "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": 0 +} diff --git a/Array_manipulation_routines.ipynb b/Array_manipulation_routines.ipynb index ec4daac..0f9ea65 100644 --- a/Array_manipulation_routines.ipynb +++ b/Array_manipulation_routines.ipynb @@ -1,736 +1,749 @@ -{ - "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 zero. 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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": { + "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 zero. 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, [2, 150])\n", + "print out\n" + ] + }, + { + "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", + "out = np.ravel(x, order='F')\n", + "print out" + ] + }, + { + "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", + "out = np.ravel(x, order='F')\n", + "print out[4]" + ] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "anaconda-cloud": {}, + "kernelspec": { + "display_name": "Python [conda env:tensorflow]", + "language": "python", + "name": "conda-env-tensorflow-py" + }, + "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": 0 +} diff --git a/Array_manipulation_routines_Solutions.ipynb b/Array_manipulation_routines_Solutions.ipynb index 54628ae..0e835ca 100644 --- a/Array_manipulation_routines_Solutions.ipynb +++ b/Array_manipulation_routines_Solutions.ipynb @@ -1,860 +1,861 @@ -{ - "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 zero. 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": { + "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 zero. 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": { + "anaconda-cloud": {}, + "kernelspec": { + "display_name": "Python [conda env:tensorflow]", + "language": "python", + "name": "conda-env-tensorflow-py" + }, + "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": 0 +} diff --git a/Linear_algebra.ipynb b/Linear_algebra.ipynb index c86d6c8..921146b 100644 --- a/Linear_algebra.ipynb +++ b/Linear_algebra.ipynb @@ -1,509 +1,525 @@ -{ - "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": 53, - "metadata": { - "collapsed": false - }, - "outputs": [], - "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": 52, - "metadata": { - "collapsed": false - }, - "outputs": [], - "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": 37, - "metadata": { - "collapsed": false - }, - "outputs": [], - "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": 45, - "metadata": { - "collapsed": false - }, - "outputs": [], - "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" - ] - }, - { - "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" - ] - }, - { - "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" - ] - }, - { - "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": 77, - "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" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Q9. Predict the results of the following code." - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": { - "collapsed": false - }, - "outputs": [], - "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" - ] - }, - { - "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" - ] - }, - { - "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" - ] - }, - { - "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", - "\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" - ] - }, - { - "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", - "\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": { + "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": 53, + "metadata": { + "collapsed": false + }, + "outputs": [], + "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": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "[8 5]\n", + "[6 6]\n", + "[8,5]\n", + "[6,6]\n", + "[6,6]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q2. Predict the results of the following code." + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": { + "collapsed": false + }, + "outputs": [], + "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": 37, + "metadata": { + "collapsed": false + }, + "outputs": [], + "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": 45, + "metadata": { + "collapsed": false + }, + "outputs": [], + "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" + ] + }, + { + "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" + ] + }, + { + "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" + ] + }, + { + "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": 77, + "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" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Q9. Predict the results of the following code." + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": { + "collapsed": false + }, + "outputs": [], + "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" + ] + }, + { + "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" + ] + }, + { + "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" + ] + }, + { + "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", + "\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" + ] + }, + { + "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", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "anaconda-cloud": {}, + "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 +} diff --git a/Logic_functions.ipynb b/Logic_functions.ipynb index aac97d8..abf0c3c 100644 --- a/Logic_functions.ipynb +++ b/Logic_functions.ipynb @@ -72,10 +72,10 @@ ], "source": [ "x = np.array([1,2,3])\n", - "#\n", + "print(np.all(x))\n", "\n", "x = np.array([1,0,3])\n", - "#" + "print(np.all(x))" ] }, { @@ -103,10 +103,10 @@ ], "source": [ "x = np.array([1,0,0])\n", - "#\n", + "print(np.any(x))\n", "\n", "x = np.array([0,0,0])\n", - "#" + "print(np.any(x))" ] }, { @@ -356,6 +356,7 @@ } ], "metadata": { + "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", diff --git a/Random_sampling.ipynb b/Random_sampling.ipynb index b3fc88f..342dbde 100644 --- a/Random_sampling.ipynb +++ b/Random_sampling.ipynb @@ -1,301 +1,305 @@ -{ - "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": [] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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": 3, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5 out of 10\n", - "2 out of 10\n", - "3 out of 10\n", - "5 out of 10\n", - "2 out of 10\n", - "5 out of 10\n", - "2 out of 10\n", - "2 out of 10\n", - "2 out of 10\n", - "5 out of 10\n" - ] - } - ], - "source": [ - "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']\n" - ] - }, - { - "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": [] - }, - { - "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": [] - }, - { - "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" - ] - }, - { - "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": [] - } - ], - "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": 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])" + ] + }, + { + "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": [] + }, + { + "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(4,size=[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": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 out of 10\n", + "2 out of 10\n", + "3 out of 10\n", + "5 out of 10\n", + "2 out of 10\n", + "5 out of 10\n", + "2 out of 10\n", + "2 out of 10\n", + "2 out of 10\n", + "5 out of 10\n" + ] + } + ], + "source": [ + "x = [b'3 out of 10', b'5 out of 10', b'2 out of 10']\n" + ] + }, + { + "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": [] + }, + { + "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": [] + }, + { + "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" + ] + }, + { + "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": [] + } + ], + "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 +}