From a5324f12769068f5cf50a3fdf59a91fe6f9a3106 Mon Sep 17 00:00:00 2001 From: CircuitsOfAIM <61307825+CircuitsOfAIM@users.noreply.github.com> Date: Thu, 2 May 2024 21:36:54 +0000 Subject: [PATCH 1/6] modified: 1_Array_creation_routines.ipynb --- 1_Array_creation_routines.ipynb | 182 +++++++++++++++++--------------- 1 file changed, 96 insertions(+), 86 deletions(-) diff --git a/1_Array_creation_routines.ipynb b/1_Array_creation_routines.ipynb index 71790b6..e316895 100644 --- a/1_Array_creation_routines.ipynb +++ b/1_Array_creation_routines.ipynb @@ -34,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 19, "metadata": { "collapsed": false }, @@ -42,16 +42,18 @@ { "data": { "text/plain": [ - "array([[0, 0],\n", - " [0, 0]])" + "array([[ 1262746869, -2117160148],\n", + " [ 406929304, 32530]], dtype=int32)" ] }, - "execution_count": 27, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], - "source": [] + "source": [ + "np.empty((2,2),dtype='int32')" + ] }, { "cell_type": "markdown", @@ -63,7 +65,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 23, "metadata": { "collapsed": false }, @@ -71,17 +73,18 @@ { "data": { "text/plain": [ - "array([[1, 2, 3],\n", - " [4, 5, 6]])" + "array([[142352912, 22016, 0],\n", + " [ 0, 0, 0]], dtype=int32)" ] }, - "execution_count": 32, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "X = np.array([[1,2,3], [4,5,6]], np.int32)\n" + "X = np.array([[1,2,3], [4,5,6]], np.int32)\n", + "np.empty_like(X)" ] }, { @@ -93,47 +96,41 @@ }, { "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, + "execution_count": 92, "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" + "name": "stdout", + "output_type": "stream", + "text": [ + "[[[1. 0. 0.]\n", + " [0. 1. 0.]\n", + " [0. 0. 1.]]\n", + "\n", + " [[1. 0. 0.]\n", + " [0. 1. 0.]\n", + " [0. 0. 1.]]\n", + "\n", + " [[1. 0. 0.]\n", + " [0. 1. 0.]\n", + " [0. 0. 1.]]]\n" + ] } ], - "source": [] + "source": [ + "x= np.zeros((3,3,3))\n", + "x[:,[0,1,2],[0,1,2]]=1\n", + "# print(x)\n", + "\n", + "# arr_eye = np.eye(3,3)\n", + "# # print(arr_eye)\n", + "\n", + "stacked_eye=np.stack([np.eye(3,3) for _ in range(3)],axis=0)\n", + "print(stacked_eye)\n", + "\n" + ] }, { "cell_type": "markdown", @@ -144,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 59, "metadata": { "collapsed": false }, @@ -152,17 +149,19 @@ { "data": { "text/plain": [ - "array([[ 1., 1.],\n", - " [ 1., 1.],\n", - " [ 1., 1.]])" + "array([[1., 1.],\n", + " [1., 1.],\n", + " [1., 1.]], dtype=float32)" ] }, - "execution_count": 36, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], - "source": [] + "source": [ + "np.ones((3,2),dtype='float32')" + ] }, { "cell_type": "markdown", @@ -173,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": 59, + "execution_count": 63, "metadata": { "collapsed": false }, @@ -181,16 +180,17 @@ { "data": { "text/plain": [ - "array([1, 1, 1, 1], dtype=int64)" + "array([1, 1, 1, 1])" ] }, - "execution_count": 59, + "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "x = np.arange(4, dtype=np.int64)\n" + "x = np.arange(4, dtype=np.int64)\n", + "np.ones_like(x)" ] }, { @@ -202,7 +202,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 64, "metadata": { "collapsed": false }, @@ -210,17 +210,19 @@ { "data": { "text/plain": [ - "array([[ 0., 0.],\n", - " [ 0., 0.],\n", - " [ 0., 0.]])" + "array([[0., 0.],\n", + " [0., 0.],\n", + " [0., 0.]])" ] }, - "execution_count": 45, + "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], - "source": [] + "source": [ + "np.zeros((3,2),dtype='float')" + ] }, { "cell_type": "markdown", @@ -231,7 +233,7 @@ }, { "cell_type": "code", - "execution_count": 58, + "execution_count": 66, "metadata": { "collapsed": false }, @@ -239,16 +241,17 @@ { "data": { "text/plain": [ - "array([0, 0, 0, 0], dtype=int64)" + "array([0, 0, 0, 0])" ] }, - "execution_count": 58, + "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "x = np.arange(4, dtype=np.int64)\n" + "x = np.arange(4, dtype=np.int64)\n", + "np.zeros_like(x)" ] }, { @@ -260,7 +263,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 94, "metadata": { "collapsed": false }, @@ -269,15 +272,17 @@ "data": { "text/plain": [ "array([[6, 6, 6, 6, 6],\n", - " [6, 6, 6, 6, 6]], dtype=uint32)" + " [6, 6, 6, 6, 6]])" ] }, - "execution_count": 49, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], - "source": [] + "source": [ + "np.full((2,5),fill_value=6)" + ] }, { "cell_type": "markdown", @@ -288,7 +293,7 @@ }, { "cell_type": "code", - "execution_count": 79, + "execution_count": 95, "metadata": { "collapsed": false }, @@ -296,16 +301,17 @@ { "data": { "text/plain": [ - "array([6, 6, 6, 6], dtype=int64)" + "array([6, 6, 6, 6])" ] }, - "execution_count": 79, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "x = np.arange(4, dtype=np.int64)\n" + "x = np.arange(4, dtype=np.int64)\n", + "np.full_like(x,fill_value=6)" ] }, { @@ -324,7 +330,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 96, "metadata": { "collapsed": false }, @@ -335,12 +341,14 @@ "array([1, 2, 3])" ] }, - "execution_count": 53, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], - "source": [] + "source": [ + "np.array([1,2,3])" + ] }, { "cell_type": "markdown", @@ -351,7 +359,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": 97, "metadata": { "collapsed": false }, @@ -362,13 +370,14 @@ "array([1, 2])" ] }, - "execution_count": 60, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "x = [1,2]\n" + "x = [1,2]\n", + "np.array(x)" ] }, { @@ -380,7 +389,7 @@ }, { "cell_type": "code", - "execution_count": 62, + "execution_count": 99, "metadata": { "collapsed": false }, @@ -392,13 +401,14 @@ " [3, 4]])" ] }, - "execution_count": 62, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "X = np.array([[1, 2], [3, 4]])\n" + "X = np.array([[1, 2], [3, 4]])\n", + "np.matrix(X)" ] }, { @@ -776,21 +786,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.6" + "pygments_lexer": "ipython3", + "version": "3.10.13" } }, "nbformat": 4, From b2b894c809265207c2e15f6c036c63548579342f Mon Sep 17 00:00:00 2001 From: CircuitsOfAIM <61307825+CircuitsOfAIM@users.noreply.github.com> Date: Thu, 2 May 2024 23:01:46 +0000 Subject: [PATCH 2/6] linspace versus uniform distribution --- 1_Array_creation_routines.ipynb | 150 +++++++++++++++++--------------- 1 file changed, 79 insertions(+), 71 deletions(-) diff --git a/1_Array_creation_routines.ipynb b/1_Array_creation_routines.ipynb index e316895..c271899 100644 --- a/1_Array_creation_routines.ipynb +++ b/1_Array_creation_routines.ipynb @@ -16,9 +16,10 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": { - "collapsed": true + "collapsed": true, + "metadata": {} }, "outputs": [], "source": [ @@ -420,24 +421,26 @@ }, { "cell_type": "code", - "execution_count": 63, + "execution_count": 3, "metadata": { - "collapsed": false + "collapsed": false, + "metadata": {} }, "outputs": [ { "data": { "text/plain": [ - "array([ 1., 2.])" + "array([1., 2.])" ] }, - "execution_count": 63, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "x = [1, 2]\n" + "x = [1, 2]\n", + "np.array(x,dtype='float')" ] }, { @@ -449,24 +452,27 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 28, "metadata": { - "collapsed": false + "collapsed": false, + "metadata": {} }, "outputs": [ { - "data": { - "text/plain": [ - "30" - ] - }, - "execution_count": 67, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.0, 0.0, 0.0, 0.0]\n" + ] } ], "source": [ - "x = np.array([30])\n" + "x = np.array([30])\n", + "x.item()\n", + "#for more than 1 elem ndarray\n", + "arr= np.empty((4,),dtype='float')\n", + "arr_ls = arr.astype('float').tolist()\n", + "print(arr_ls)" ] }, { @@ -478,22 +484,26 @@ }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 33, "metadata": { - "collapsed": false + "collapsed": false, + "metadata": {} }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "70140352 [1 2 3]\n", - "70140752 [1 2 3]\n" + "140251354479184 140251354672240 140251354479184\n" ] } ], "source": [ - "x = np.array([1, 2, 3])\n" + "x = np.array([1, 2, 3])\n", + "x_copy = x.copy()\n", + "x_pseudo_copy = x\n", + "#id for comparing the memory address (unique py object id)\n", + "print(id(x),id(x_copy),id(x_pseudo_copy))" ] }, { @@ -512,9 +522,10 @@ }, { "cell_type": "code", - "execution_count": 85, + "execution_count": 35, "metadata": { - "collapsed": false + "collapsed": false, + "metadata": {} }, "outputs": [ { @@ -526,12 +537,14 @@ " 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100])" ] }, - "execution_count": 85, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], - "source": [] + "source": [ + "np.arange(2,102,2)" + ] }, { "cell_type": "markdown", @@ -542,35 +555,31 @@ }, { "cell_type": "code", - "execution_count": 86, + "execution_count": 91, "metadata": { - "collapsed": false + "collapsed": false, + "metadata": {} }, "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" + "name": "stdout", + "output_type": "stream", + "text": [ + "linear space : [3. 3.14 3.28 3.42 3.56]\n", + "_____________________\n", + "uniform distribution: [10.09585471 10.82061904 3.43391913 5.51637347 8.10445252]\n" + ] } ], - "source": [] + "source": [ + "arr_linspace = np.linspace(start=3,stop=10,endpoint=False,num=50)\n", + "arr_uniform= np.random.uniform(low=3,high=11,size=50)\n", + "##there is a difference between the uniform distribution and linear space (distribution versus ruler)\n", + "print('linear space :',arr_linspace[:5])\n", + "print('_____________________')\n", + "print('uniform distribution:',arr_uniform[:5])\n", + "\n" + ] }, { "cell_type": "markdown", @@ -581,39 +590,38 @@ }, { "cell_type": "code", - "execution_count": 88, + "execution_count": 79, "metadata": { - "collapsed": false + "collapsed": false, + "metadata": {} }, "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])" + "array([1.00000000e+03, 1.38949549e+03, 1.93069773e+03, 2.68269580e+03,\n", + " 3.72759372e+03, 5.17947468e+03, 7.19685673e+03, 1.00000000e+04,\n", + " 1.38949549e+04, 1.93069773e+04, 2.68269580e+04, 3.72759372e+04,\n", + " 5.17947468e+04, 7.19685673e+04, 1.00000000e+05, 1.38949549e+05,\n", + " 1.93069773e+05, 2.68269580e+05, 3.72759372e+05, 5.17947468e+05,\n", + " 7.19685673e+05, 1.00000000e+06, 1.38949549e+06, 1.93069773e+06,\n", + " 2.68269580e+06, 3.72759372e+06, 5.17947468e+06, 7.19685673e+06,\n", + " 1.00000000e+07, 1.38949549e+07, 1.93069773e+07, 2.68269580e+07,\n", + " 3.72759372e+07, 5.17947468e+07, 7.19685673e+07, 1.00000000e+08,\n", + " 1.38949549e+08, 1.93069773e+08, 2.68269580e+08, 3.72759372e+08,\n", + " 5.17947468e+08, 7.19685673e+08, 1.00000000e+09, 1.38949549e+09,\n", + " 1.93069773e+09, 2.68269580e+09, 3.72759372e+09, 5.17947468e+09,\n", + " 7.19685673e+09, 1.00000000e+10])" ] }, - "execution_count": 88, + "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], - "source": [] + "source": [ + "np.logspace(start=3,stop=10,endpoint=True,num=50)" + ] }, { "cell_type": "markdown", From be3dd90147258db7bc9a9e43adfb685983c8ab56 Mon Sep 17 00:00:00 2001 From: CircuitsOfAIM <61307825+CircuitsOfAIM@users.noreply.github.com> Date: Fri, 3 May 2024 23:50:03 +0000 Subject: [PATCH 3/6] creation_routine done. solutions deleted modified: 1_Array_creation_routines.ipynb deleted: 1_Array_creation_routines_Solution.ipynb --- 1_Array_creation_routines.ipynb | 114 +-- 1_Array_creation_routines_Solution.ipynb | 949 ----------------------- 2 files changed, 65 insertions(+), 998 deletions(-) delete mode 100644 1_Array_creation_routines_Solution.ipynb diff --git a/1_Array_creation_routines.ipynb b/1_Array_creation_routines.ipynb index c271899..5f46bfa 100644 --- a/1_Array_creation_routines.ipynb +++ b/1_Array_creation_routines.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "collapsed": true, "metadata": {} @@ -642,7 +642,7 @@ }, { "cell_type": "code", - "execution_count": 93, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -653,13 +653,14 @@ "array([ 0, 5, 10])" ] }, - "execution_count": 93, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])\n" + "X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])\n", + "X.diagonal()" ] }, { @@ -671,26 +672,27 @@ }, { "cell_type": "code", - "execution_count": 95, + "execution_count": 17, "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" + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 0 0 0]\n", + " [0 2 0 0]\n", + " [0 0 3 0]\n", + " [0 0 0 4]]\n" + ] } ], - "source": [] + "source": [ + "init_arr = np.zeros((4,4),dtype='int')\n", + "init_arr[[0,1,2,3],[0,1,2,3]] = np.arange(1,5)\n", + "print(init_arr)\n" + ] }, { "cell_type": "markdown", @@ -704,7 +706,7 @@ }, { "cell_type": "code", - "execution_count": 97, + "execution_count": 25, "metadata": { "collapsed": false }, @@ -712,17 +714,23 @@ { "data": { "text/plain": [ - "array([[ 0., 0., 0., 0., 0.],\n", - " [ 1., 0., 0., 0., 0.],\n", - " [ 1., 1., 0., 0., 0.]])" + "array([[0, 0, 0, 0, 0],\n", + " [1, 0, 0, 0, 0],\n", + " [1, 1, 0, 0, 0]])" ] }, - "execution_count": 97, + "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], - "source": [] + "source": [ + "resarr= np.ones((3,5),dtype='int')\n", + "#tril gets lower part of diagonal\n", + "np.tril(resarr,k=-1)\n", + "#triu upper part of diagonal\n", + "# np.triu(resarr,k=1)\n" + ] }, { "cell_type": "markdown", @@ -737,26 +745,34 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 38, "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" + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3]\n", + " [ 4 5 6]\n", + " [ 7 8 9]\n", + " [10 11 12]]\n", + "[[ 0 0 0]\n", + " [ 4 0 0]\n", + " [ 7 8 0]\n", + " [10 11 12]]\n" + ] } ], - "source": [] + "source": [ + "#natanaiil azamt dar negahe towst na daran chizi ke be aan minegari (full arange created array got zero on parts)\n", + "pre_arr = np.arange(1,13).reshape(4,3)\n", + "post_arr = np.tril(pre_arr,k=-1)\n", + "print(pre_arr)\n", + "print(post_arr)\n", + "#reshape is the most important thing in this matter" + ] }, { "cell_type": "markdown", @@ -770,26 +786,26 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 40, "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" + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 1 2 3]\n", + " [ 4 5 6]\n", + " [ 0 8 9]\n", + " [ 0 0 12]]\n" + ] } ], - "source": [] + "source": [ + "post_arr_2 = np.triu(pre_arr,k=-1)\n", + "print(post_arr_2)" + ] } ], "metadata": { diff --git a/1_Array_creation_routines_Solution.ipynb b/1_Array_creation_routines_Solution.ipynb deleted file mode 100644 index fb1ecfd..0000000 --- a/1_Array_creation_routines_Solution.ipynb +++ /dev/null @@ -1,949 +0,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.\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 -} From ae004f1d554a1c957f85be46362b453e8a79e0a2 Mon Sep 17 00:00:00 2001 From: CircuitsOfAIM <61307825+CircuitsOfAIM@users.noreply.github.com> Date: Sat, 4 May 2024 23:15:54 +0000 Subject: [PATCH 4/6] man routines initiated modified: 2_Array_manipulation_routines.ipynb --- 2_Array_manipulation_routines.ipynb | 1497 ++++++++++++++------------- 1 file changed, 761 insertions(+), 736 deletions(-) diff --git a/2_Array_manipulation_routines.ipynb b/2_Array_manipulation_routines.ipynb index 85243a3..eb2fb32 100644 --- a/2_Array_manipulation_routines.ipynb +++ b/2_Array_manipulation_routines.ipynb @@ -1,736 +1,761 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Array manipulation routines" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import numpy as np" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'1.11.2'" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "np.__version__" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "Q1. Let x be a ndarray [10, 10, 3] with all elements set to one. Reshape x so that the size of the second dimension equals 150." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1.]\n", - " [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", - " 1. 1. 1. 1. 1. 1.]]\n" - ] - } - ], - "source": [] - }, - { - "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.26.4'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.__version__" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "Q1. Let x be a ndarray [10, 10, 3] with all elements set to one. Reshape x so that the size of the second dimension equals 150." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[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.,\n", + " 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.,\n", + " 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.,\n", + " 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.,\n", + " 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.,\n", + " 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.,\n", + " 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.,\n", + " 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.,\n", + " 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.,\n", + " 1., 1., 1., 1., 1., 1.]])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones([10,10,3]).reshape(-1,150)\n", + "# -1 acts as a place holder to fill the remaining but only for 1-dim\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": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3, 4, 5, 6])" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x_q2 = [[1,2,3],[4,5,6]]\n", + "np.concatenate(x_q2)" + ] + }, + { + "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": 25, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.concatenate(x_q2)[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": 28, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 4, 5)\n", + "(4, 3, 5)\n" + ] + } + ], + "source": [ + "x_q4= np.full([3,4,5],fill_value=5)\n", + "print(x_q4.shape)\n", + "x_q4= x_q4.reshape(4,3,5)\n", + "print(x_q4.shape)\n" + ] + }, + { + "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 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.13" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} From 30c4e37ef36c8469c4ab213256bd2eac4181439f Mon Sep 17 00:00:00 2001 From: CircuitsOfAIM <61307825+CircuitsOfAIM@users.noreply.github.com> Date: Sun, 5 May 2024 22:39:08 +0000 Subject: [PATCH 5/6] concatenate, squeeze, transpose, reshape modified: 2_Array_manipulation_routines.ipynb --- 2_Array_manipulation_routines.ipynb | 113 ++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 30 deletions(-) diff --git a/2_Array_manipulation_routines.ipynb b/2_Array_manipulation_routines.ipynb index eb2fb32..f9434cb 100644 --- a/2_Array_manipulation_routines.ipynb +++ b/2_Array_manipulation_routines.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -130,20 +130,21 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" + "ename": "NameError", + "evalue": "name 'x_q2' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m np\u001b[38;5;241m.\u001b[39mconcatenate(\u001b[43mx_q2\u001b[49m)[\u001b[38;5;241m4\u001b[39m]\n", + "\u001b[0;31mNameError\u001b[0m: name 'x_q2' is not defined" + ] } ], "source": [ @@ -159,7 +160,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 16, "metadata": { "collapsed": false }, @@ -176,8 +177,9 @@ "source": [ "x_q4= np.full([3,4,5],fill_value=5)\n", "print(x_q4.shape)\n", - "x_q4= x_q4.reshape(4,3,5)\n", - "print(x_q4.shape)\n" + "#swapping the axes\n", + "x_q4_swapped = np.swapaxes(x_q4,0,1)\n", + "print(x_q4_swapped.shape)\n" ] }, { @@ -189,7 +191,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 14, "metadata": { "collapsed": false }, @@ -198,11 +200,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "(4L, 3L)\n" + "(4, 3)\n" ] } ], - "source": [] + "source": [ + "X_q5 = np.transpose(np.zeros((3,4)))\n", + "print(X_q5.shape)\n" + ] }, { "cell_type": "markdown", @@ -213,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 9, "metadata": { "collapsed": false }, @@ -222,11 +227,31 @@ "name": "stdout", "output_type": "stream", "text": [ - "(3L, 1L, 4L)\n" + "[[1. 1. 1. 1.]\n", + " [1. 1. 1. 1.]\n", + " [1. 1. 1. 1.]] (3, 4)\n" ] + }, + { + "data": { + "text/plain": [ + "array([[[1., 1., 1., 1.]],\n", + "\n", + " [[1., 1., 1., 1.]],\n", + "\n", + " [[1., 1., 1., 1.]]])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" } ], - "source": [] + "source": [ + "X_q5_b = np.ones((3,4))\n", + "print(X_q5_b,X_q5_b.shape)\n", + "X_q5_b.reshape((3,1,4))" + ] }, { "cell_type": "markdown", @@ -237,7 +262,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 21, "metadata": { "collapsed": false }, @@ -246,11 +271,32 @@ "name": "stdout", "output_type": "stream", "text": [ - "(3L, 4L)\n" + "[[[0.]\n", + " [0.]\n", + " [0.]\n", + " [0.]]\n", + "\n", + " [[0.]\n", + " [0.]\n", + " [0.]\n", + " [0.]]\n", + "\n", + " [[0.]\n", + " [0.]\n", + " [0.]\n", + " [0.]]]\n", + "[[0. 0. 0. 0.]\n", + " [0. 0. 0. 0.]\n", + " [0. 0. 0. 0.]]\n" ] } ], - "source": [] + "source": [ + "X_q6 = np.zeros((3,4,1))\n", + "print(X_q6)\n", + "print(np.squeeze(X_q6))\n", + "\n" + ] }, { "cell_type": "markdown", @@ -267,21 +313,28 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "[[ 1 2 3 7 8 9]\n", - " [ 4 5 6 10 11 12]]\n" - ] + "data": { + "text/plain": [ + "array([[ 1, 2, 3, 7, 8, 9],\n", + " [ 4, 5, 6, 10, 11, 12]])" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" } ], - "source": [] + "source": [ + "x= [[1,2,3],[4,5,6]]\n", + "y= [[7,8,9],[10,11,12]]\n", + "np.concatenate((x,y),axis=1)" + ] }, { "cell_type": "markdown", From 2d6be9079de07c029ba1150e47994c2552e22b54 Mon Sep 17 00:00:00 2001 From: CircuitsOfAIM <61307825+CircuitsOfAIM@users.noreply.github.com> Date: Mon, 6 May 2024 23:46:36 +0000 Subject: [PATCH 6/6] transpose, rot90 , contcatenate versus stack modified: 2_Array_manipulation_routines.ipynb --- 2_Array_manipulation_routines.ipynb | 43 +++++++++++++------ 2_Array_manipulation_routines_Solutions.ipynb | 2 +- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/2_Array_manipulation_routines.ipynb b/2_Array_manipulation_routines.ipynb index f9434cb..5bff89e 100644 --- a/2_Array_manipulation_routines.ipynb +++ b/2_Array_manipulation_routines.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": { "collapsed": true }, @@ -354,23 +354,30 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 2, "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" - ] + "data": { + "text/plain": [ + "array([[ 1, 2, 3],\n", + " [ 4, 5, 6],\n", + " [ 7, 8, 9],\n", + " [10, 11, 12]])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" } ], - "source": [] + "source": [ + "x= [[1,2,3],[4,5,6]]\n", + "y= [[7,8,9],[10,11,12]]\n", + "np.concatenate((x,y),axis=0)" + ] }, { "cell_type": "markdown", @@ -381,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": 54, + "execution_count": 34, "metadata": { "collapsed": false }, @@ -396,7 +403,17 @@ ] } ], - "source": [] + "source": [ + "x ,y = np.array([1,2,3]),np.array([4,5,6])\n", + "\n", + "#rotation is anti clockwise\n", + "# print(np.rot90(x))\n", + "# print(np.atleast_2d(x).T)#???\n", + "#transpose returns same for vectors, so np.atleast_2d makes it 2D i.e (3,) become (3,1)\n", + "print(np.concatenate((np.atleast_2d(x).T,np.atleast_2d(y).T),axis=1))\n", + "\n", + "# print(np.squeeze(np.stack((np.atleast_2d(x).T,np.atleast_2d(y).T),axis=2),axis=1))" + ] }, { "cell_type": "markdown", diff --git a/2_Array_manipulation_routines_Solutions.ipynb b/2_Array_manipulation_routines_Solutions.ipynb index aa56247..a7f7188 100644 --- a/2_Array_manipulation_routines_Solutions.ipynb +++ b/2_Array_manipulation_routines_Solutions.ipynb @@ -807,7 +807,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.1" + "version": "3.10.13" } }, "nbformat": 4,