diff --git a/Array_creation_routines.ipynb b/Array_creation_routines.ipynb index 50b16b2..d949cdf 100644 --- a/Array_creation_routines.ipynb +++ b/Array_creation_routines.ipynb @@ -1,798 +1,798 @@ -{ - "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": [] + }, + { + "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]. Convert it into an array." + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2])" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = [1,2]\n" + ] + }, + { + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/Array_creation_routines_solution.ipynb b/Array_creation_routines_solution.ipynb index 2787cf2..67aa9ac 100644 --- a/Array_creation_routines_solution.ipynb +++ b/Array_creation_routines_solution.ipynb @@ -1,949 +1,949 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Array creation routines" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Ones and zeros" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import numpy as np" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create a new array of 2*2 integers, without initializing entries." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[0, 0],\n", - " [0, 0]])" - ] - }, - "execution_count": 27, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.empty([2,2], int)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let X = np.array([1,2,3], [4,5,6], np.int32). \n", - "Create a new array with the same shape and type as X." - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[1, 2, 3],\n", - " [4, 5, 6]])" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "X = np.array([[1,2,3], [4,5,6]], np.int32)\n", - "np.empty_like(X)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create a 3-D array with ones on the diagonal and zeros elsewhere." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 1., 0., 0.],\n", - " [ 0., 1., 0.],\n", - " [ 0., 0., 1.]])" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.eye(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 1., 0., 0.],\n", - " [ 0., 1., 0.],\n", - " [ 0., 0., 1.]])" - ] - }, - "execution_count": 35, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.identity(3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create a new array of 3*2 float numbers, filled with ones." - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 1., 1.],\n", - " [ 1., 1.],\n", - " [ 1., 1.]])" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.ones([3,2], float)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let x = np.arange(4, dtype=np.int64). Create an array of ones with the same shape and type as X." - ] - }, - { - "cell_type": "code", - "execution_count": 59, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([1, 1, 1, 1], dtype=int64)" - ] - }, - "execution_count": 59, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = np.arange(4, dtype=np.int64)\n", - "np.ones_like(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create a new array of 3*2 float numbers, filled with zeros." - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 0., 0.],\n", - " [ 0., 0.],\n", - " [ 0., 0.]])" - ] - }, - "execution_count": 45, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.zeros((3,2), float)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let x = np.arange(4, dtype=np.int64). Create an array of zeros with the same shape and type as X." - ] - }, - { - "cell_type": "code", - "execution_count": 58, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([0, 0, 0, 0], dtype=int64)" - ] - }, - "execution_count": 58, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = np.arange(4, dtype=np.int64)\n", - "np.zeros_like(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create a new array of 2*5 uints, filled with 6." - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[6, 6, 6, 6, 6],\n", - " [6, 6, 6, 6, 6]], dtype=uint32)" - ] - }, - "execution_count": 49, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.full((2, 5), 6, dtype=np.uint)" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[6, 6, 6, 6, 6],\n", - " [6, 6, 6, 6, 6]], dtype=uint32)" - ] - }, - "execution_count": 50, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.ones([2, 5], dtype=np.uint) * 6" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let x = np.arange(4, dtype=np.int64). Create an array of 6's with the same shape and type as X." - ] - }, - { - "cell_type": "code", - "execution_count": 79, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([6, 6, 6, 6], dtype=int64)" - ] - }, - "execution_count": 79, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = np.arange(4, dtype=np.int64)\n", - "np.full_like(x, 6)" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([6, 6, 6, 6], dtype=int64)" - ] - }, - "execution_count": 81, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.ones_like(x) * 6" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## From existing data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create an array of [1, 2, 3]." - ] - }, - { - "cell_type": "code", - "execution_count": 53, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([1, 2, 3])" - ] - }, - "execution_count": 53, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.array([1, 2, 3])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let x = [1, 2]. 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.asarray(x)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix." - ] - }, - { - "cell_type": "code", - "execution_count": 62, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "matrix([[1, 2],\n", - " [3, 4]])" - ] - }, - "execution_count": 62, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "X = np.array([[1, 2], [3, 4]])\n", - "np.asmatrix(X)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let x = [1, 2]. Conver it into an array of `float`." - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 1., 2.])" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = [1, 2]\n", - "np.asfarray(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 1., 2.])" - ] - }, - "execution_count": 64, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.asarray(x, float)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30." - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "30" - ] - }, - "execution_count": 67, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = np.array([30])\n", - "np.asscalar(x)" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "30" - ] - }, - "execution_count": 68, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x[0]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let x = np.array([1, 2, 3]). Create a array copy of x, which has a different id from x." - ] - }, - { - "cell_type": "code", - "execution_count": 76, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "70140352 [1 2 3]\n", - "70140752 [1 2 3]\n" - ] - } - ], - "source": [ - "x = np.array([1, 2, 3])\n", - "y = np.copy(x)\n", - "print id(x), x\n", - "print id(y), y" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Numerical ranges" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create an array of 2, 4, 6, 8, ..., 100." - ] - }, - { - "cell_type": "code", - "execution_count": 85, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,\n", - " 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52,\n", - " 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78,\n", - " 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])" - ] - }, - "execution_count": 85, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.arange(2, 101, 2)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive." - ] - }, - { - "cell_type": "code", - "execution_count": 86, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 3. , 3.14285714, 3.28571429, 3.42857143,\n", - " 3.57142857, 3.71428571, 3.85714286, 4. ,\n", - " 4.14285714, 4.28571429, 4.42857143, 4.57142857,\n", - " 4.71428571, 4.85714286, 5. , 5.14285714,\n", - " 5.28571429, 5.42857143, 5.57142857, 5.71428571,\n", - " 5.85714286, 6. , 6.14285714, 6.28571429,\n", - " 6.42857143, 6.57142857, 6.71428571, 6.85714286,\n", - " 7. , 7.14285714, 7.28571429, 7.42857143,\n", - " 7.57142857, 7.71428571, 7.85714286, 8. ,\n", - " 8.14285714, 8.28571429, 8.42857143, 8.57142857,\n", - " 8.71428571, 8.85714286, 9. , 9.14285714,\n", - " 9.28571429, 9.42857143, 9.57142857, 9.71428571,\n", - " 9.85714286, 10. ])" - ] - }, - "execution_count": 86, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.linspace(3., 10, 50)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive." - ] - }, - { - "cell_type": "code", - "execution_count": 88, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 1.00000000e+03, 1.38038426e+03, 1.90546072e+03,\n", - " 2.63026799e+03, 3.63078055e+03, 5.01187234e+03,\n", - " 6.91830971e+03, 9.54992586e+03, 1.31825674e+04,\n", - " 1.81970086e+04, 2.51188643e+04, 3.46736850e+04,\n", - " 4.78630092e+04, 6.60693448e+04, 9.12010839e+04,\n", - " 1.25892541e+05, 1.73780083e+05, 2.39883292e+05,\n", - " 3.31131121e+05, 4.57088190e+05, 6.30957344e+05,\n", - " 8.70963590e+05, 1.20226443e+06, 1.65958691e+06,\n", - " 2.29086765e+06, 3.16227766e+06, 4.36515832e+06,\n", - " 6.02559586e+06, 8.31763771e+06, 1.14815362e+07,\n", - " 1.58489319e+07, 2.18776162e+07, 3.01995172e+07,\n", - " 4.16869383e+07, 5.75439937e+07, 7.94328235e+07,\n", - " 1.09647820e+08, 1.51356125e+08, 2.08929613e+08,\n", - " 2.88403150e+08, 3.98107171e+08, 5.49540874e+08,\n", - " 7.58577575e+08, 1.04712855e+09, 1.44543977e+09,\n", - " 1.99526231e+09, 2.75422870e+09, 3.80189396e+09,\n", - " 5.24807460e+09, 7.24435960e+09])" - ] - }, - "execution_count": 88, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.logspace(3., 10., 50, endpoint=False)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Building matrices" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Let X = np.array([[ 0, 1, 2, 3],\n", - " [ 4, 5, 6, 7],\n", - " [ 8, 9, 10, 11]]).\n", - " Get the diagonal of X, that is, [0, 5, 10]." - ] - }, - { - "cell_type": "code", - "execution_count": 93, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 0, 5, 10])" - ] - }, - "execution_count": 93, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])\n", - "np.diag(X)" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ 0, 5, 10])" - ] - }, - "execution_count": 94, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "X.diagonal()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere." - ] - }, - { - "cell_type": "code", - "execution_count": 95, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[1, 0, 0, 0],\n", - " [0, 2, 0, 0],\n", - " [0, 0, 3, 0],\n", - " [0, 0, 0, 4]])" - ] - }, - "execution_count": 95, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.diagflat([1, 2, 3, 4])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create an array which looks like below.\n", - "array([[ 0., 0., 0., 0., 0.],\n", - " [ 1., 0., 0., 0., 0.],\n", - " [ 1., 1., 0., 0., 0.]])" - ] - }, - { - "cell_type": "code", - "execution_count": 97, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 0., 0., 0., 0., 0.],\n", - " [ 1., 0., 0., 0., 0.],\n", - " [ 1., 1., 0., 0., 0.]])" - ] - }, - "execution_count": 97, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.tri(3, 5, -1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Create an array which looks like below. 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": { - "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.eye(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 1., 0., 0.],\n", + " [ 0., 1., 0.],\n", + " [ 0., 0., 1.]])" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.identity(3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a new array of 3*2 float numbers, filled with ones." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 1., 1.],\n", + " [ 1., 1.],\n", + " [ 1., 1.]])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones([3,2], float)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let x = np.arange(4, dtype=np.int64). Create an array of ones with the same shape and type as X." + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 1, 1, 1], dtype=int64)" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = np.arange(4, dtype=np.int64)\n", + "np.ones_like(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a new array of 3*2 float numbers, filled with zeros." + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0., 0.],\n", + " [ 0., 0.],\n", + " [ 0., 0.]])" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros((3,2), float)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let x = np.arange(4, dtype=np.int64). Create an array of zeros with the same shape and type as X." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0, 0, 0, 0], dtype=int64)" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = np.arange(4, dtype=np.int64)\n", + "np.zeros_like(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a new array of 2*5 uints, filled with 6." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6]], dtype=uint32)" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.full((2, 5), 6, dtype=np.uint)" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6]], dtype=uint32)" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones([2, 5], dtype=np.uint) * 6" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let x = np.arange(4, dtype=np.int64). Create an array of 6's with the same shape and type as X." + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([6, 6, 6, 6], dtype=int64)" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = np.arange(4, dtype=np.int64)\n", + "np.full_like(x, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([6, 6, 6, 6], dtype=int64)" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones_like(x) * 6" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## From existing data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create an array of [1, 2, 3]." + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.array([1, 2, 3])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let x = [1, 2]. Convert it into an array." + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2])" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = [1,2]\n", + "np.asarray(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "matrix([[1, 2],\n", + " [3, 4]])" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X = np.array([[1, 2], [3, 4]])\n", + "np.asmatrix(X)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let x = [1, 2]. Conver it into an array of `float`." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1., 2.])" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = [1, 2]\n", + "np.asfarray(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1., 2.])" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.asarray(x, float)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30." + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = np.array([30])\n", + "np.asscalar(x)" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let x = np.array([1, 2, 3]). Create a array copy of x, which has a different id from x." + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "70140352 [1 2 3]\n", + "70140752 [1 2 3]\n" + ] + } + ], + "source": [ + "x = np.array([1, 2, 3])\n", + "y = np.copy(x)\n", + "print id(x), x\n", + "print id(y), y" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Numerical ranges" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create an array of 2, 4, 6, 8, ..., 100." + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,\n", + " 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52,\n", + " 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78,\n", + " 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(2, 101, 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive." + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 3. , 3.14285714, 3.28571429, 3.42857143,\n", + " 3.57142857, 3.71428571, 3.85714286, 4. ,\n", + " 4.14285714, 4.28571429, 4.42857143, 4.57142857,\n", + " 4.71428571, 4.85714286, 5. , 5.14285714,\n", + " 5.28571429, 5.42857143, 5.57142857, 5.71428571,\n", + " 5.85714286, 6. , 6.14285714, 6.28571429,\n", + " 6.42857143, 6.57142857, 6.71428571, 6.85714286,\n", + " 7. , 7.14285714, 7.28571429, 7.42857143,\n", + " 7.57142857, 7.71428571, 7.85714286, 8. ,\n", + " 8.14285714, 8.28571429, 8.42857143, 8.57142857,\n", + " 8.71428571, 8.85714286, 9. , 9.14285714,\n", + " 9.28571429, 9.42857143, 9.57142857, 9.71428571,\n", + " 9.85714286, 10. ])" + ] + }, + "execution_count": 86, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.linspace(3., 10, 50)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 1-D array of 50 element spaced evenly on a log scale between 3. and 10., exclusive." + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1.00000000e+03, 1.38038426e+03, 1.90546072e+03,\n", + " 2.63026799e+03, 3.63078055e+03, 5.01187234e+03,\n", + " 6.91830971e+03, 9.54992586e+03, 1.31825674e+04,\n", + " 1.81970086e+04, 2.51188643e+04, 3.46736850e+04,\n", + " 4.78630092e+04, 6.60693448e+04, 9.12010839e+04,\n", + " 1.25892541e+05, 1.73780083e+05, 2.39883292e+05,\n", + " 3.31131121e+05, 4.57088190e+05, 6.30957344e+05,\n", + " 8.70963590e+05, 1.20226443e+06, 1.65958691e+06,\n", + " 2.29086765e+06, 3.16227766e+06, 4.36515832e+06,\n", + " 6.02559586e+06, 8.31763771e+06, 1.14815362e+07,\n", + " 1.58489319e+07, 2.18776162e+07, 3.01995172e+07,\n", + " 4.16869383e+07, 5.75439937e+07, 7.94328235e+07,\n", + " 1.09647820e+08, 1.51356125e+08, 2.08929613e+08,\n", + " 2.88403150e+08, 3.98107171e+08, 5.49540874e+08,\n", + " 7.58577575e+08, 1.04712855e+09, 1.44543977e+09,\n", + " 1.99526231e+09, 2.75422870e+09, 3.80189396e+09,\n", + " 5.24807460e+09, 7.24435960e+09])" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.logspace(3., 10., 50, endpoint=False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Building matrices" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let X = np.array([[ 0, 1, 2, 3],\n", + " [ 4, 5, 6, 7],\n", + " [ 8, 9, 10, 11]]).\n", + " Get the diagonal of X, that is, [0, 5, 10]." + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 5, 10])" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])\n", + "np.diag(X)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 0, 5, 10])" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X.diagonal()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere." + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 0, 0, 0],\n", + " [0, 2, 0, 0],\n", + " [0, 0, 3, 0],\n", + " [0, 0, 0, 4]])" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.diagflat([1, 2, 3, 4])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create an array which looks like below.\n", + "array([[ 0., 0., 0., 0., 0.],\n", + " [ 1., 0., 0., 0., 0.],\n", + " [ 1., 1., 0., 0., 0.]])" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 0., 0., 0., 0., 0.],\n", + " [ 1., 0., 0., 0., 0.],\n", + " [ 1., 1., 0., 0., 0.]])" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.tri(3, 5, -1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create an array which looks like below. 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": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}