{"id":908,"date":"2020-06-08T06:40:03","date_gmt":"2020-06-08T06:40:03","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=908"},"modified":"2020-06-08T06:40:03","modified_gmt":"2020-06-08T06:40:03","slug":"numpy-practical-examples","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=908","title":{"rendered":"7 Numpy Practical Examples: Sample Code for Beginners"},"content":{"rendered":"<p>In the previous tutorial, we have discussed some basic concepts of NumPy in <a href=\"https:\/\/devopscube.com\/python-numpy-tutorial\/\" rel=\"noreferrer noopener\">Python Numpy Tutorial<\/a> For Beginners With Examples. In this tutorial, we are going to discuss some problems and the solution with NumPy practical examples and code.<\/p>\n<p>As you might know, NumPy is one of the important Python modules used in the field of <a href=\"https:\/\/devopscube.com\/list-best-frameworks-data-scientists\/\" rel=\"noreferrer noopener\">data science and machine learning<\/a>. As a beginner, it is very important to know about a few NumPy practical examples.<\/p>\n<h2 id=\"numpy-practical-examples\">Numpy Practical Examples<\/h2>\n<p>Let&#8217;s have a look at 7 NumPy sample solutions covering some key NumPy concepts. Each example has code with a relevant NumPy library and its output.<\/p>\n<h3 id=\"how-to-search-the-maximum-and-minimum-element-in-the-given-array-using-numpy\">How to search the maximum and minimum element in the given array using NumPy?<\/h3>\n<p>Searching is a technique that helps finds the place of a given element or value in the list. In Numpy, one can perform various searching operations using the various functions that are provided in the library like <strong>argmax<\/strong>, <strong>argmin<\/strong>, etc.<\/p>\n<ol>\n<li><strong>numpy.argmax( )<\/strong><\/li>\n<\/ol>\n<p>This function returns indices of the maximum element of the array in a particular axis.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\n# Creating 5x4 array\narray = np.arange(20).reshape(5, 4)\nprint(array)\nprint()\n\n# If no axis mentioned, then it works on the entire array\nprint(np.argmax(array))\n\n# If axis=1, then it works on each row\nprint(np.argmax(array, axis=1))\n\n# If axis=0, then it works on each column\nprint(np.argmax(array, axis=0))<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[[ 0 1 2 3]\n[ 4 5 6 7]\n[ 8 9 10 11]\n[12 13 14 15]\n[16 17 18 19]]\n\n19\n[3 3 3 3 3]\n[4 4 4 4]<\/code><\/pre>\n<p>Similarly one can use <strong><code>numpy.argmin( <\/code>)<\/strong> to return indices of the minimum element of the array in a particular axis.<\/p>\n<h3 id=\"how-to-sort-the-elements-in-the-given-array-using-numpy\">How to sort the elements in the given array using Numpy?<\/h3>\n<p>Sorting refers to arrange data in a particular format. Sorting algorithm specifies the way to arrange data in a particular order. In Numpy, one can perform various sorting operations using the various functions that are provided in the library like <strong>sort<\/strong>, <strong>argsort,<\/strong> etc.<\/p>\n<ol>\n<li><strong>numpy.sort( )<\/strong><\/li>\n<\/ol>\n<p>This function returns a sorted copy of an array.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\narray = np.array([\n    [3, 7, 1],\n    [10, 3, 2],\n    [5, 6, 7]\n])\nprint(array)\nprint()\n\n# Sort the whole array\nprint(np.sort(array, axis=None))\n\n# Sort along each row\nprint(np.sort(array, axis=1))\n\n# Sort along each column\nprint(np.sort(array, axis=0))<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[[ 3 7 1]\n[10 3 2]\n[ 5 6 7]]\n\n[ 1 2 3 3 5 6 7 7 10]\n\n[[ 1 3 7]\n[ 2 3 10]\n[ 5 6 7]]\n\n[[ 3 3 1]\n[ 5 6 2]\n[10 7 7]]<\/code><\/pre>\n<ol>\n<li><strong>numpy.argsort( )<\/strong><\/li>\n<\/ol>\n<p>This function returns the indices that would sort an array.<\/p>\n<p><strong>Example<\/strong>:<\/p>\n<pre><code>import numpy as np\n\narray = np.array([28, 13, 45, 12, 4, 8, 0])\nprint(array)\n\nprint(np.argsort(array))<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[28 13 45 12 4 8 0]\n[6 4 5 3 1 0 2]<\/code><\/pre>\n<h3 id=\"how-to-find-the-mean-of-every-numpy-array-in-the-given-list\">How to find the mean of every NumPy array in the given list?<\/h3>\n<p>The problem statement is given a list of NumPy array, the task is to find mean of every NumPy array.<\/p>\n<ol>\n<li><strong>Using np.mean<\/strong>( )<\/li>\n<\/ol>\n<pre><code>import numpy as np\n\nlist = [\n    np.array([3, 2, 8, 9]),\n    np.array([4, 12, 34, 25, 78]),\n    np.array([23, 12, 67])\n]\n\nresult = []\nfor i in range(len(list)):\n    result.append(np.mean(list[i]))\nprint(result)<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[5.5, 30.6, 34.0]<\/code><\/pre>\n<h3 id=\"how-to-add-rows-and-columns-in-numpy-array\">How to add rows and columns in NumPy array?<\/h3>\n<p>The problem statement is given NumPy array, the task is to add rows\/columns basis on requirements to numpy array.<\/p>\n<ol>\n<li><strong>Adding Row<\/strong> <strong>using numpy.vstack( )<\/strong><\/li>\n<\/ol>\n<pre><code>import numpy as np\n\narray = np.array([\n    [3, 2, 8],\n    [4, 12, 34],\n    [23, 12, 67]\n])\n\nnewRow = np.array([2, 1, 8])\nnewArray = np.vstack((array, newRow))\nprint(newArray)<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[[ 3 2 8]\n[ 4 12 34]\n[23 12 67]\n[ 2 1 8]]<\/code><\/pre>\n<ol>\n<li><strong>Adding Column<\/strong> <strong>using numpy.column_stack( )<\/strong><\/li>\n<\/ol>\n<pre><code>import numpy as np\n\narray = np.array([\n    [3, 2, 8],\n    [4, 12, 34],\n    [23, 12, 67]\n])\n\nnewColumn = np.array([2, 1, 8])\nnewArray = np.column_stack((array, newColumn))\nprint(newArray)<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[[ 3 2 8 2]\n[ 4 12 34 1]\n[23 12 67 8]]<\/code><\/pre>\n<h3 id=\"how-to-reverse-a-numpy-array\">How to reverse a NumPy array?<\/h3>\n<p>The problem statement is given NumPy array, the task is to reverse the NumPy array.<\/p>\n<ol>\n<li><strong>Using numpy.flipud( )<\/strong><\/li>\n<\/ol>\n<pre><code>import numpy as np\n\narray = np.array([3, 6, 7, 2, 5, 1, 8])\nreversedArray = np.flipud(array)\nprint(reversedArray)<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[8 1 5 2 7 6 3]<\/code><\/pre>\n<h3 id=\"how-to-multiply-two-matrices-in-a-single-line-using-numpy\">How to multiply two matrices in a single line using NumPy?<\/h3>\n<p>The problem statement is given two matrices and one has to multiply those two matrices in a single line using NumPy.<\/p>\n<ol>\n<li><strong>Using numpy.dot( )<\/strong><\/li>\n<\/ol>\n<pre><code>import numpy as np\n\nmatrix1 = [\n    [3, 4, 2],\n    [5, 1, 8],\n    [3, 1, 9]\n]\n\nmatrix2 = [\n    [3, 7, 5],\n    [2, 9, 8],\n    [1, 5, 8]\n]\n\nresult = np.dot(matrix1, matrix2)\nprint(result)<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>[[19 67 63]\n[25 84 97]\n[20 75 95]]<\/code><\/pre>\n<h3 id=\"how-to-print-the-checkerboard-pattern-of-nxn-using-numpy\">How to print the checkerboard pattern of nxn using NumPy?<\/h3>\n<p>The problem statement is given n, print the checkerboard pattern for a nxn matrix considering that 0 for black and 1 for white.<\/p>\n<p><strong>Solution<\/strong>:<\/p>\n<pre><code>import numpy as np\n\nn = 8\n\n# Create a nxn matrix filled with 0\nmatrix = np.zeros((n, n), dtype=int)\n\n# fill 1 with alternate rows and column\nmatrix[::2, 1::2] = 1\nmatrix[1::2, ::2] = 1\n\n# Print the checkerboard pattern\nfor i in range(n):\n    for j in range(n):\n        print(matrix[i][j], end=\" \")\n    print()<\/code><\/pre>\n<p><strong>Output<\/strong>:<\/p>\n<pre><code>0 1 0 1 0 1 0 1\n1 0 1 0 1 0 1 0\n0 1 0 1 0 1 0 1\n1 0 1 0 1 0 1 0\n0 1 0 1 0 1 0 1\n1 0 1 0 1 0 1 0\n0 1 0 1 0 1 0 1\n1 0 1 0 1 0 1 0<\/code><\/pre>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/numpy-practical-examples\/\" target=\"_blank\" rel=\"noopener noreferrer\">7 Numpy Practical Examples: Sample Code for Beginners \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/numpy-practical-examples\/<\/p>\n","protected":false},"author":1,"featured_media":909,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=908"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/908\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/909"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}