From ac94078f8586c50b03c62956150aee4aa37a96bc Mon Sep 17 00:00:00 2001 From: ppngg <131286838+ppngg@users.noreply.github.com> Date: Thu, 30 May 2024 19:27:50 +0700 Subject: [PATCH 1/2] Created using Colab --- 1_Array_creation_routines.ipynb | 1672 ++++++++++++++++--------------- 1 file changed, 878 insertions(+), 794 deletions(-) diff --git a/1_Array_creation_routines.ipynb b/1_Array_creation_routines.ipynb index 71790b6..101ade9 100644 --- a/1_Array_creation_routines.ipynb +++ b/1_Array_creation_routines.ipynb @@ -1,798 +1,882 @@ { - "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.\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" + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "roauurqdeElg" + }, + "source": [ + "# Array creation routines" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BQZo91NMeElh" + }, + "source": [ + "## Ones and zeros" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "id": "ZFoplO7NeElh" + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "o_461LiGeEli" + }, + "source": [ + "Create a new array of 2*2 integers, without initializing entries." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Am7QkKMMeEli", + "outputId": "a1ee7620-8540-4c70-bd46-aafc1fd9ef07" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0, 0],\n", + " [0, 0]])" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GIQDBbnveElj" + }, + "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": null, + "metadata": { + "id": "91XnXwi2eElj", + "outputId": "b8f21066-7b2a-4b56-c311-631a86918aec" + }, + "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": { + "id": "sM9HfndKeElj" + }, + "source": [ + "Create a 3-D array with ones on the diagonal and zeros elsewhere." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "logVYf0TeElk", + "outputId": "06036dbc-6868-4bea-b4f8-e5dbef567312" + }, + "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": null, + "metadata": { + "id": "ngNZDO68eElk", + "outputId": "c83ae391-89c6-458b-f7df-d73f325327da" + }, + "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": { + "id": "UWj77iwCeElk" + }, + "source": [ + "Create a new array of 3*2 float numbers, filled with ones." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "YR7RPnxoeEll", + "outputId": "c6369d94-af12-481b-f6ba-593285eb8e45" + }, + "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": { + "id": "NVpe1g4veEll" + }, + "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": null, + "metadata": { + "id": "Gq3XdbL9eEll", + "outputId": "7160f7ed-b04e-4427-f846-b6b50fca1551" + }, + "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": { + "id": "e_Xo2b8OeEll" + }, + "source": [ + "Create a new array of 3*2 float numbers, filled with zeros." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "8VJtogtfeEll", + "outputId": "f9861f89-19ba-4ae3-a272-7d510e367515" + }, + "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": { + "id": "Ax27lSp6eElm" + }, + "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": null, + "metadata": { + "id": "Dl4f5b6EeElm", + "outputId": "53643253-e490-416e-caf0-2f96c0c186e4" + }, + "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": { + "id": "1g7kGGgfeElm" + }, + "source": [ + "Create a new array of 2*5 uints, filled with 6." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "DNSX6DKXeElm", + "outputId": "c62d3c4f-2335-4bbb-b0c6-c8986b6257e9" + }, + "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": { + "id": "pMFieXqWeElm" + }, + "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": null, + "metadata": { + "id": "4aHPYbeaeEln", + "outputId": "52e8b2c8-6366-444f-e7be-360e45a6d1d2" + }, + "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": { + "id": "uztprUMKeEln" + }, + "source": [ + "## From existing data" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Z-wSGlLreEln" + }, + "source": [ + "Create an array of [1, 2, 3]." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "5M7x54WneEln", + "outputId": "b26de315-353d-4b2e-b25e-3646401e1d44" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eOpTep9WeEln" + }, + "source": [ + "Let x = [1, 2]. Convert it into an array." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Wmp-qaEbeEln", + "outputId": "437b9731-3543-48bc-b897-6e65b532c440" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2])" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = [1,2]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-9D0EsgeeEln" + }, + "source": [ + "Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "8zOd8ZW7eElo", + "outputId": "ffa8a120-c5e0-487d-b3ac-955f34316521" + }, + "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": { + "id": "MZmTcXcXeElo" + }, + "source": [ + "Let x = [1, 2]. Conver it into an array of `float`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "PHe_Dd-IeElo", + "outputId": "62c0907a-9ced-47b6-b9aa-2c2aeeb442b0" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1., 2.])" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = [1, 2]\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6Z79Jvk5eElo" + }, + "source": [ + "Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "bmBoInsheElo", + "outputId": "cc721c43-d93d-48a3-c5eb-79959031f57a" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = np.array([30])\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ebWNr8jJeElo" + }, + "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": null, + "metadata": { + "id": "16ZrC5-0eElo", + "outputId": "ce45bf90-bf29-49c3-b66f-f2daa3949325" + }, + "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": { + "id": "8ZeZE0nqeElp" + }, + "source": [ + "## Numerical ranges" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9SyfXeUYeElp" + }, + "source": [ + "Create an array of 2, 4, 6, 8, ..., 100." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "2KMwNp87eElp", + "outputId": "7305e3c4-3924-473e-993f-1e3c68b6fd0c" + }, + "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": { + "id": "AOD_38xpeElp" + }, + "source": [ + "Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "BA2vdthTeElp", + "outputId": "fa43b227-712d-45ec-bca3-d5b0a8c93681" + }, + "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": { + "id": "rUEG37ZGeElp" + }, + "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": null, + "metadata": { + "id": "mIreR7zteElp", + "outputId": "179f2ca5-21cb-4000-de63-d76a221ff814" + }, + "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": { + "id": "W6Y2-62qeElp" + }, + "source": [ + "## Building matrices" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_2y8kop_eElp" + }, + "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": null, + "metadata": { + "id": "MtewYxaxeElp", + "outputId": "42cf8bf1-5670-43d4-a9da-c145d13dc090" + }, + "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": { + "id": "v-39tDkseElp" + }, + "source": [ + "Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ynDb1ulUeElq", + "outputId": "89a2fe9e-cfe6-4f3d-f133-144199f080c8" + }, + "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": { + "id": "tFZxyK9UeElq" + }, + "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": null, + "metadata": { + "id": "V5gS1VLYeElq", + "outputId": "768bd8fd-e3c5-4357-cdae-04aa4b249e02" + }, + "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": { + "id": "andWYH0veElq" + }, + "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": null, + "metadata": { + "id": "_-NGEoSIeElq", + "outputId": "0b02dd2e-5da8-49c2-e72c-948ec92b2baa" + }, + "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": { + "id": "tPhRYj12eElq" + }, + "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": null, + "metadata": { + "id": "B_JiY7g3eElq", + "outputId": "54059146-db2e-476e-e288-cc91bc988540" + }, + "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": [] } - ], - "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" + ], + "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" + }, + "colab": { + "provenance": [] } - ], - "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 -} + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file From 978d8601b7ebc8f642b878c054b72b1d088730c7 Mon Sep 17 00:00:00 2001 From: ppngg <131286838+ppngg@users.noreply.github.com> Date: Thu, 30 May 2024 20:00:26 +0700 Subject: [PATCH 2/2] Created using Colab --- Copy_of_1_Array_creation_routines.ipynb | 992 ++++++++++++++++++++++++ 1 file changed, 992 insertions(+) create mode 100644 Copy_of_1_Array_creation_routines.ipynb diff --git a/Copy_of_1_Array_creation_routines.ipynb b/Copy_of_1_Array_creation_routines.ipynb new file mode 100644 index 0000000..6c31b71 --- /dev/null +++ b/Copy_of_1_Array_creation_routines.ipynb @@ -0,0 +1,992 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "roauurqdeElg" + }, + "source": [ + "# Array creation routines" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BQZo91NMeElh" + }, + "source": [ + "## Ones and zeros" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true, + "id": "ZFoplO7NeElh" + }, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "o_461LiGeEli" + }, + "source": [ + "Create a new array of 2*2 integers, without initializing entries." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Am7QkKMMeEli", + "outputId": "1cd341a8-ecbc-4145-8166-55bc4a62ab8a" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0, 1],\n", + " [2, 3]])" + ] + }, + "metadata": {}, + "execution_count": 28 + } + ], + "source": [ + "np.empty([2,2], int)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GIQDBbnveElj" + }, + "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": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "91XnXwi2eElj", + "outputId": "0e97b72a-ef50-48a0-bb36-11ecb1c8ee92" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 0, 1072693248, 0],\n", + " [1072693248, 0, 1072693248]], dtype=int32)" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ], + "source": [ + "X = np.array([[1,2,3], [4,5,6]], np.int32)\n", + "np.empty_like(X)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sM9HfndKeElj" + }, + "source": [ + "Create a 3-D array with ones on the diagonal and zeros elsewhere." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "logVYf0TeElk", + "outputId": "06036dbc-6868-4bea-b4f8-e5dbef567312" + }, + "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": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ngNZDO68eElk", + "outputId": "81be6c15-b5b3-4091-f592-ade6dbbc63d4" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1., 0., 0.],\n", + " [0., 1., 0.],\n", + " [0., 0., 1.]])" + ] + }, + "metadata": {}, + "execution_count": 31 + } + ], + "source": [ + "np.identity(3)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UWj77iwCeElk" + }, + "source": [ + "Create a new array of 3*2 float numbers, filled with ones." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "YR7RPnxoeEll", + "outputId": "a9945c00-1903-4c46-837a-53d30c6f342f" + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1., 1.],\n", + " [1., 1.],\n", + " [1., 1.]])" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ], + "source": [ + "np.ones([3,2], float)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "NVpe1g4veEll" + }, + "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": 4, + "metadata": { + "id": "Gq3XdbL9eEll", + "outputId": "86a03f4b-dd52-4d21-a83f-3619391621ae", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 1, 1, 1])" + ] + }, + "metadata": {}, + "execution_count": 4 + } + ], + "source": [ + "x = np.arange(4, dtype=np.int64)\n", + "np.ones_like(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "e_Xo2b8OeEll" + }, + "source": [ + "Create a new array of 3*2 float numbers, filled with zeros." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "id": "8VJtogtfeEll", + "outputId": "21681f89-93a9-417f-8aa6-1526d925b182", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0., 0.],\n", + " [0., 0.],\n", + " [0., 0.]])" + ] + }, + "metadata": {}, + "execution_count": 5 + } + ], + "source": [ + "np.zeros([3,2], float)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ax27lSp6eElm" + }, + "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": 7, + "metadata": { + "id": "Dl4f5b6EeElm", + "outputId": "1a040ea0-6eaf-4036-eed1-2285af98cf82", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([0, 0, 0, 0])" + ] + }, + "metadata": {}, + "execution_count": 7 + } + ], + "source": [ + "x = np.arange(4, dtype=np.int64)\n", + "np.zeros_like(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1g7kGGgfeElm" + }, + "source": [ + "Create a new array of 2*5 uints, filled with 6." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "DNSX6DKXeElm", + "outputId": "08fc5a10-0bd1-4961-90d3-bffc429cb4f6", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6]], dtype=uint64)" + ] + }, + "metadata": {}, + "execution_count": 10 + } + ], + "source": [ + "np.full([2,5], 6, np.uint)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pMFieXqWeElm" + }, + "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": 12, + "metadata": { + "id": "4aHPYbeaeEln", + "outputId": "0d6af765-d8c7-426a-d598-95acc3e8d7ad", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([6, 6, 6, 6])" + ] + }, + "metadata": {}, + "execution_count": 12 + } + ], + "source": [ + "x = np.arange(4, dtype=np.int64)\n", + "np.full_like(x, 6)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uztprUMKeEln" + }, + "source": [ + "## From existing data" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Z-wSGlLreEln" + }, + "source": [ + "Create an array of [1, 2, 3]." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "5M7x54WneEln", + "outputId": "360b3af9-d09a-49a8-9d2d-ee7491cc8b3b", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "metadata": {}, + "execution_count": 15 + } + ], + "source": [ + "np.array([1,2,3])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eOpTep9WeEln" + }, + "source": [ + "Let x = [1, 2]. Convert it into an array." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "Wmp-qaEbeEln", + "outputId": "6983059c-2a8a-4617-fee5-66d8f56fed70", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2])" + ] + }, + "metadata": {}, + "execution_count": 21 + } + ], + "source": [ + "x = [1,2]\n", + "np.asarray(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-9D0EsgeeEln" + }, + "source": [ + "Let X = np.array([[1, 2], [3, 4]]). Convert it into a matrix." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "id": "8zOd8ZW7eElo", + "outputId": "ed871aec-4446-4d20-b187-e9e5c3a51f12", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "matrix([[1, 2],\n", + " [3, 4]])" + ] + }, + "metadata": {}, + "execution_count": 20 + } + ], + "source": [ + "X = np.array([[1, 2], [3, 4]])\n", + "np.asmatrix(X)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MZmTcXcXeElo" + }, + "source": [ + "Let x = [1, 2]. Conver it into an array of `float`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "PHe_Dd-IeElo", + "outputId": "62c0907a-9ced-47b6-b9aa-2c2aeeb442b0" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 1., 2.])" + ] + }, + "execution_count": 63, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = [1, 2]\n", + "np.asarray(x, float)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6Z79Jvk5eElo" + }, + "source": [ + "Let x = np.array([30]). Convert it into scalar of its single element, i.e. 30." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "bmBoInsheElo", + "outputId": "13e64523-5a4a-4c44-e6bb-d47daa9f8ab3", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "30" + ] + }, + "metadata": {}, + "execution_count": 23 + } + ], + "source": [ + "x = np.array([30])\n", + "np.ndarray.item(x)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ebWNr8jJeElo" + }, + "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": 25, + "metadata": { + "id": "16ZrC5-0eElo", + "outputId": "0cd82e8e-0206-4e9c-faf4-b2bcfc8ccb4b", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "135801491023664 [1 2 3]\n", + "135801491025488 [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": { + "id": "8ZeZE0nqeElp" + }, + "source": [ + "## Numerical ranges" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9SyfXeUYeElp" + }, + "source": [ + "Create an array of 2, 4, 6, 8, ..., 100." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "id": "2KMwNp87eElp", + "outputId": "eaaf5a75-a398-43ff-c7ae-f75ddf7ea678", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "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])" + ] + }, + "metadata": {}, + "execution_count": 28 + } + ], + "source": [ + "np.arange(2, 102, 2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AOD_38xpeElp" + }, + "source": [ + "Create a 1-D array of 50 evenly spaced elements between 3. and 10., inclusive." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "id": "BA2vdthTeElp", + "outputId": "01d61a09-80b6-4e74-d7ef-6ae52b451dad", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 3. , 3.14285714, 3.28571429, 3.42857143, 3.57142857,\n", + " 3.71428571, 3.85714286, 4. , 4.14285714, 4.28571429,\n", + " 4.42857143, 4.57142857, 4.71428571, 4.85714286, 5. ,\n", + " 5.14285714, 5.28571429, 5.42857143, 5.57142857, 5.71428571,\n", + " 5.85714286, 6. , 6.14285714, 6.28571429, 6.42857143,\n", + " 6.57142857, 6.71428571, 6.85714286, 7. , 7.14285714,\n", + " 7.28571429, 7.42857143, 7.57142857, 7.71428571, 7.85714286,\n", + " 8. , 8.14285714, 8.28571429, 8.42857143, 8.57142857,\n", + " 8.71428571, 8.85714286, 9. , 9.14285714, 9.28571429,\n", + " 9.42857143, 9.57142857, 9.71428571, 9.85714286, 10. ])" + ] + }, + "metadata": {}, + "execution_count": 29 + } + ], + "source": [ + "np.linspace(3., 10, 50)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rUEG37ZGeElp" + }, + "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": 32, + "metadata": { + "id": "mIreR7zteElp", + "outputId": "06d97744-b41b-40e8-d34a-0caed439a76a", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1.00000000e+03, 1.38038426e+03, 1.90546072e+03, 2.63026799e+03,\n", + " 3.63078055e+03, 5.01187234e+03, 6.91830971e+03, 9.54992586e+03,\n", + " 1.31825674e+04, 1.81970086e+04, 2.51188643e+04, 3.46736850e+04,\n", + " 4.78630092e+04, 6.60693448e+04, 9.12010839e+04, 1.25892541e+05,\n", + " 1.73780083e+05, 2.39883292e+05, 3.31131121e+05, 4.57088190e+05,\n", + " 6.30957344e+05, 8.70963590e+05, 1.20226443e+06, 1.65958691e+06,\n", + " 2.29086765e+06, 3.16227766e+06, 4.36515832e+06, 6.02559586e+06,\n", + " 8.31763771e+06, 1.14815362e+07, 1.58489319e+07, 2.18776162e+07,\n", + " 3.01995172e+07, 4.16869383e+07, 5.75439937e+07, 7.94328235e+07,\n", + " 1.09647820e+08, 1.51356125e+08, 2.08929613e+08, 2.88403150e+08,\n", + " 3.98107171e+08, 5.49540874e+08, 7.58577575e+08, 1.04712855e+09,\n", + " 1.44543977e+09, 1.99526231e+09, 2.75422870e+09, 3.80189396e+09,\n", + " 5.24807460e+09, 7.24435960e+09])" + ] + }, + "metadata": {}, + "execution_count": 32 + } + ], + "source": [ + "np.logspace(3., 10, 50, endpoint=False)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "W6Y2-62qeElp" + }, + "source": [ + "## Building matrices" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_2y8kop_eElp" + }, + "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": 33, + "metadata": { + "id": "MtewYxaxeElp", + "outputId": "80da21ec-8172-451f-892d-24a371e1daa2", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 0, 5, 10])" + ] + }, + "metadata": {}, + "execution_count": 33 + } + ], + "source": [ + "X = np.array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])\n", + "np.diag(X)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "v-39tDkseElp" + }, + "source": [ + "Create a 2-D array whose diagonal equals [1, 2, 3, 4] and 0's elsewhere." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "id": "ynDb1ulUeElq", + "outputId": "0d308993-213d-401c-f0ce-42898c59aa61", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1, 0, 0, 0],\n", + " [0, 2, 0, 0],\n", + " [0, 0, 3, 0],\n", + " [0, 0, 0, 4]])" + ] + }, + "metadata": {}, + "execution_count": 35 + } + ], + "source": [ + "np.diagflat([1, 2, 3, 4])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tFZxyK9UeElq" + }, + "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": 36, + "metadata": { + "id": "V5gS1VLYeElq", + "outputId": "e8e54aff-9717-4233-b691-24eee63add1f", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0., 0., 0., 0., 0.],\n", + " [1., 0., 0., 0., 0.],\n", + " [1., 1., 0., 0., 0.]])" + ] + }, + "metadata": {}, + "execution_count": 36 + } + ], + "source": [ + "np.tri(3, 5, -1)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "andWYH0veElq" + }, + "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": 47, + "metadata": { + "id": "_-NGEoSIeElq", + "outputId": "d933be55-6345-426c-f53c-50c8ef5ac40b", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 0, 0, 0],\n", + " [ 4, 0, 0],\n", + " [ 7, 8, 0],\n", + " [10, 11, 12]])" + ] + }, + "metadata": {}, + "execution_count": 47 + } + ], + "source": [ + "np.tril(np.arange(1, 13).reshape(4, 3), -1)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tPhRYj12eElq" + }, + "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": 55, + "metadata": { + "id": "B_JiY7g3eElq", + "outputId": "c3916935-6149-4dcb-81d2-05bd69188da0", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 1, 2, 3],\n", + " [ 4, 5, 6],\n", + " [ 0, 8, 9],\n", + " [ 0, 0, 12]])" + ] + }, + "metadata": {}, + "execution_count": 55 + } + ], + "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" + }, + "colab": { + "provenance": [], + "include_colab_link": true + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file