{"id":721,"date":"2022-02-24T13:54:19","date_gmt":"2022-02-24T18:54:19","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=721"},"modified":"2022-02-24T13:54:21","modified_gmt":"2022-02-24T18:54:21","slug":"python-numpy","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/python-numpy\/","title":{"rendered":"Python NumPy"},"content":{"rendered":"\n<p>NumPy is a python library that makes it easy to perform mathematical and logical operations on arrays. It provides high performance n-dimensional array object and tools to work with the arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why use numpy instead of a python list<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>Takes less memory<\/li><li>Faster than python lists<\/li><li>More powerful<\/li><li>Easier to use<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">What is my numpy version<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nprint (np.__version__)\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1.9.3\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Creating numpy arrays<\/h3>\n\n\n\n<p>There are many ways to create numpy arrays<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># create arrays\navalues = &#91;12, 34, 45]\na = np.array(avalues)\nb = np.array(&#91;11.1,22.2,33.3])\n\n# 2D array, matrix\nc = np.array(&#91;(1, 2, 3), (4 , 5, 6)])\n\n# print results\nprint('a = ', a)              \nprint('b = ', b)               \nprint(c)\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a =  &#91;12 34 45]\nb =  &#91; 11.1  22.2  33.3]\n&#91;&#91;1 2 3]\n &#91;4 5 6]] \n<\/code><\/pre>\n\n\n\n<p>Using arange() function to create arrays<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d = np.arange(5)              # create array of 5 numbers starting from 0\ne = np.arange(9)+1            # create array of 9 numbers starting from 1\nf = np.arange(10, 20)         # array of numbers 10 to 19\ng = np.arange(10, 31, 5)      # array 10 to 30 with 5 increments\nh = len(np.arange(10,15))     # get size of array\ni = np.arange(10, 20).size    # get size of array\nj = np.arange(9).reshape(3,3) # create 2D array\n\n# print results\nprint('d = ', d)\nprint('e = ', e)\nprint('f = ', f)\nprint('g = ', g)\nprint('h = ', h)\nprint('i = ', i)\nprint(j)\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d =  &#91;0 1 2 3 4]\ne =  &#91;1 2 3 4 5 6 7 8 9]\nf =  &#91;10 11 12 13 14 15 16 17 18 19]\ng =  &#91;10 15 20 25 30]\nh =  5\ni =  10\n&#91;&#91;0 1 2]\n &#91;3 4 5]\n &#91;6 7 8]]\n<\/code><\/pre>\n\n\n\n<p>Using numpy function linspace()<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d = np.linspace(10, 20, 5)                # create elements with 2.5 intervals\ne = np.linspace(10, 20, 5, retstep=True)  # also shows the interval size\nprint(d)\nprint(e)\nprint(e&#91;1])                               # get the interval size\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91; 10.   12.5  15.   17.5  20. ]\n(array(&#91; 10. ,  12.5,  15. ,  17.5,  20. ]), 2.5)\n2.5\n<\/code><\/pre>\n\n\n\n<p>Using zeros() and ones() functions<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = np.zeros(4)               # array of 4 zero elements\nb = np.ones(4)                # array of 4 one elements\nc = np.ones(5, dtype='int64') # create int64 array\nd = np.zeros((4,3))           # 4 x 3 matrix of zeros\ne = np.zeros((2,3,4))         # Two 3 x 4 matrices of zeros\nprint(a, \"\\n\", b, \"\\n\", c, \"\\n-----\\n\", d, \"\\n-----\\n\", e)\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91; 0.  0.  0.  0.] \n &#91; 1.  1.  1.  1.] \n &#91;1 1 1 1 1] \n-----\n&#91;&#91; 0.  0.  0.]\n &#91; 0.  0.  0.]\n &#91; 0.  0.  0.]\n &#91; 0.  0.  0.]] \n-----\n &#91;&#91;&#91; 0.  0.  0.  0.]\n  &#91; 0.  0.  0.  0.]\n  &#91; 0.  0.  0.  0.]]\n\n &#91;&#91; 0.  0.  0.  0.]\n  &#91; 0.  0.  0.  0.]\n  &#91; 0.  0.  0.  0.]]]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Array Type<\/h3>\n\n\n\n<p>A numpy array can only have one type. To find the type of an array,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(a.dtype)         # int64\nprint(b.dtype)         # float64\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type of a =  int64\ntype of b =  float64\n<\/code><\/pre>\n\n\n\n<p>Since every element of a numpy array must have the same data type, in the following example, int and float elements will be converted to complex numbers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tpl = (10, -1.23, 2+3j)\nc = np.array(tpl)\nprint(c)\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91; 10.00+0.j  -1.23+0.j   2.00+3.j]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using arrays<\/h3>\n\n\n\n<p>Working with 1D array<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a = np.array(&#91;-3, -2, 0, 1, 2])\nprint(a)\nprint(a&#91;1])\na&#91;2] = 100\nprint(a)\nprint(a.size)\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;-3 -2  0  1  2]\n-2\n&#91; -3  -2 100   1   2]\n5\n<\/code><\/pre>\n\n\n\n<p>Working with 2D array<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>b = np.arange(21)\nb.shape = (3, 7)\nprint(b)\nprint('-----')\nprint(b&#91;2])\nprint(b&#91;2,1])\nprint(b&#91;2]&#91;1])\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&#91; 0  1  2  3  4  5  6]\n &#91; 7  8  9 10 11 12 13]\n &#91;14 15 16 17 18 19 20]]\n-----\n&#91;14 15 16 17 18 19 20]\n15\n15\n<\/code><\/pre>\n\n\n\n<p>Working with 3D array<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c = np.arange(27)\nc.shape = (3,3,3)\nprint(c)\nprint('-----')\nprint(c&#91;0])\nprint('-----')\nprint(c&#91;0,1])\nprint('-----')\nprint(c&#91;0,1,2])\nprint('-----')\nc&#91;0,1,2] = 999\nprint(c)\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&#91;&#91; 0  1  2]\n  &#91; 3  4  5]\n  &#91; 6  7  8]]\n\n &#91;&#91; 9 10 11]\n  &#91;12 13 14]\n  &#91;15 16 17]]\n\n &#91;&#91;18 19 20]\n  &#91;21 22 23]\n  &#91;24 25 26]]]\n-----\n&#91;&#91;0 1 2]\n &#91;3 4 5]\n &#91;6 7 8]]\n-----\n&#91;3 4 5]\n-----\n5\n-----\n&#91;&#91;&#91;  0   1   2]\n  &#91;  3   4 999]\n  &#91;  6   7   8]]\n\n &#91;&#91;  9  10  11]\n  &#91; 12  13  14]\n  &#91; 15  16  17]]\n\n &#91;&#91; 18  19  20]\n  &#91; 21  22  23]\n  &#91; 24  25  26]]]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Arithmetic operations on arrays<\/h3>\n\n\n\n<p>Note that numpy overloads operators operate differently on python arrays and numpy arrays.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># subtract all values\nprint(a - b)           # &#91;  0.9  11.8  11.7]\n# cube all values\nprint(a**3)            # &#91; 1728 39304 91125]\nprint(2 * np.cos(a))   # &#91;  1367.631  10941.048  36926.037]\nprint(a &lt; 30)          # &#91; True False False]\n# multiply values\nprint(a * b)           # &#91;  133.2   754.8  1498.5]\n# matrix product\nprint(a.dot(b))        # 2386.5\n<\/code><\/pre>\n\n\n\n<p>Output is in the code comments.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Special Functions<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport matplotlib  as pyplot as plt\nx = np.arange(0,3*np.pi, 0.1)\ny = np.sin(x)\nplt.plot(x,y)\nplt.show\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>You should see a sign function\n<\/code><\/pre>\n\n\n\n<p>You can also use cos() and tan() in a similar way. The following function calculates the e value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nx = np.array(&#91;1,2,3])\nprint(np.exp(x))\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91; 2.71828183  7.3890561  20.08553692 ]\n<\/code><\/pre>\n\n\n\n<p>The following code calculates the natural log:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\ny = np.array(&#91;1,2,3])\nprint(np.log(ar))\n<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91; 0.        0.693141718    1.09861229 ]\n<\/code><\/pre>\n\n\n\n<p>Similarly log10 with give log base 10 and so on. numpy packs more power than what we covered here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy is a python library that makes it easy to perform mathematical and logical operations on arrays. It provides high performance n-dimensional array object and tools to work with the arrays. Why use numpy instead of a python list Takes less memory Faster than python lists More powerful Easier to use What is my numpy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[208,137],"class_list":["post-721","post","type-post","status-publish","format-standard","hentry","category-python","tag-numpy","tag-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=721"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/721\/revisions"}],"predecessor-version":[{"id":722,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/721\/revisions\/722"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}