{"id":8081,"date":"2019-01-12T16:12:11","date_gmt":"2019-01-12T16:12:11","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=8081"},"modified":"2019-01-23T22:26:05","modified_gmt":"2019-01-23T22:26:05","slug":"python-numpy-tutorial-linux-hint","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/12\/python-numpy-tutorial-linux-hint\/","title":{"rendered":"Python NumPy Tutorial \u2013 Linux Hint"},"content":{"rendered":"<p>In this lesson on Python\u00a0<a href=\"http:\/\/www.numpy.org\/\">NumPy<\/a>\u00a0library, we will look at how this library allows us to manage powerful N-dimensional array objects with sophisticated functions present to manipulate and operate over these arrays. To make this lesson complete, we will cover the following sections:<\/p>\n<ul>\n<li>What is Python NumPy package?<\/li>\n<li>NumPy arrays<\/li>\n<li>Different operations which can be done over NumPy arrays<\/li>\n<li>Some more special functions<\/li>\n<\/ul>\n<h3><strong>What is Python NumPy package?<\/strong><\/h3>\n<p>Simply put, NumPy stands for \u2018Numerical Python\u2019 and that is what it aims to fulfil, to allow complex numerical operations performed on N-dimensional array objects very easily and in an intuitive manner. It is the core library used in\u00a0<strong>scientific computing<\/strong>, with functions present to perform linear algebraic operations and statistical operations.<\/p>\n<div class=\"1ke7BnHj\"><\/div>\n<p>One of the most fundamental (and attractive) concepts to NumPy is its usage of N-dimensional array objects. We can take this array as just a\u00a0<strong>collection of rows and column<\/strong>, just like an MS-Excel file. It is possible to convert a Python list into a NumPy array and operate functions over it.<\/p>\n<div class=\"5bDthlPu\"><\/div>\n<div id=\"attachment_35253\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-35253\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/1-23.png\" alt=\"\" width=\"282\" height=\"152\" \/><\/p>\n<p class=\"wp-caption-text\">NumPy Array representation<\/p>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Just a note before starting, we use a\u00a0<a href=\"https:\/\/linuxhint.com\/virtual_environments_python3\/\">virtual environment<\/a>\u00a0for this lesson which we made with the following command:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">python -m virtualenv numpy<br \/>\nsource numpy\/bin\/activate<\/div>\n<div><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Once the virtual environment is active, we can install numpy library within the virtual env so that examples we create next can be executed:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">pip install numpy<\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">We see something like this when we execute the above command:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35254\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/2-22.png\" sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/2-22.png 1140w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/2-22-300x90.png 300w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/2-22-768x231.png 768w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/2-22-1024x309.png 1024w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/2-22-810x244.png 810w\" alt=\"\" width=\"1140\" height=\"344\" \/><\/p>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Let\u2019s quickly test if the NumPy package has been installed correctly with the following short code snippet:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">import<\/span>\u00a0numpy\u00a0<span class=\"kw1\">as<\/span>\u00a0np<br \/>\na\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">2<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>a<span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Once you run the above program, you should see the following output:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35256\" src=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/3-22.png\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" srcset=\"https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/3-22.png 602w, https:\/\/linuxhint.com\/wp-content\/uploads\/2019\/01\/3-22-300x132.png 300w\" alt=\"\" width=\"602\" height=\"264\" \/><\/p>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">We can also have multi-dimensional arrays with NumPy:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">multi_dimension\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">2<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">3<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"br0\">(<\/span><span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">5<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">6<\/span><span class=\"br0\">)<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>multi_dimension<span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">This will produce an output like:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"br0\">[<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">1<\/span>\u00a0<span class=\"nu0\">2<\/span>\u00a0<span class=\"nu0\">3<\/span><span class=\"br0\">]<\/span><br \/>\n<span class=\"br0\">[<\/span><span class=\"nu0\">4<\/span>\u00a0<span class=\"nu0\">5<\/span>\u00a0<span class=\"nu0\">6<\/span><span class=\"br0\">]<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">You can use Anaconda as well to run these examples which is easier and that is what we have used above. If you want to install it on your machine, look at the lesson which describes \u201c<a href=\"https:\/\/linuxhint.com\/install_anaconda_python_ubuntu_1804\/\">How to Install Anaconda Python on Ubuntu 18.04 LTS<\/a>\u201d and share your feedback. Now, let us move forward to various types of operations that can be performed with with Python NumPy arrays.<\/span><\/p>\n<h3><strong>Using NumPy arrays over Python lists<\/strong><\/h3>\n<p>It is important to ask that when Python already has a sophisticated data structure to hold multiple items than why do we need NumPy arrays at all? The NumPy arrays are\u00a0<strong>preferred over Python lists<\/strong>\u00a0due to the following reasons:<span id=\"ezoic-pub-ad-placeholder-122\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<ul>\n<li>Convenient to use for mathematical and compute intensive operations due to presence of compatible NumPy functions<\/li>\n<li>They are much fast faster due to the way they store data internally<\/li>\n<li>Less memory<\/li>\n<\/ul>\n<p>Let us\u00a0<strong>prove that NumPy arrays occupy less memory<\/strong>. This can be done by writing a very simple Python program:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">import<\/span>\u00a0numpy\u00a0<span class=\"kw1\">as<\/span>\u00a0np<\/p>\n<p><span class=\"kw1\">import<\/span>\u00a0<span class=\"kw3\">time<\/span><br \/>\n<span class=\"kw1\">import<\/span>\u00a0<span class=\"kw3\">sys<\/span><\/p>\n<p>python_list\u00a0<span class=\"sy0\">=<\/span>\u00a0<span class=\"kw2\">range<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">500<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span><span class=\"kw3\">sys<\/span>.<span class=\"me1\">getsizeof<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">)<\/span>\u00a0*\u00a0<span class=\"kw2\">len<\/span><span class=\"br0\">(<\/span>python_list<span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/p>\n<p>numpy_arr\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"me1\">arange<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">500<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>numpy_arr.<span class=\"me1\">size<\/span>\u00a0* numpy_arr.<span class=\"me1\">itemsize<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">When we run the above program, we will get the following output:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"nu0\">14000<\/span><br \/>\n<span class=\"nu0\">4000<\/span><\/div>\n<\/div>\n<p>This shows that the same size list is\u00a0<strong>more than 3 times<\/strong>\u00a0in size when compared to same size NumPy array.<\/p>\n<h3><strong>Performing NumPy operations<\/strong><\/h3>\n<p>In this section, let us quickly glance over the operations that can be performed on NumPy arrays.<\/p>\n<h4><strong>Finding dimensions in array<\/strong><\/h4>\n<p>As the NumPy array can be used in any dimensional space to hold data, we can find the dimension of an array with the following code snippet:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">import<\/span>\u00a0numpy\u00a0<span class=\"kw1\">as<\/span>\u00a0np<\/p>\n<p>numpy_arr\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">2<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">,<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">6<\/span><span class=\"br0\">)<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>numpy_arr.<span class=\"me1\">ndim<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p>We will see the output as \u201c2\u201d as this is a 2-dimensional array.<\/p>\n<h4><strong>Finding datatype of items in array<\/strong><\/h4>\n<p>We can use NumPy array to hold any data type. Let\u2019s now find out the data type of the data an array contains:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">other_arr\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8216;awe&#8217;<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"st0\">&#8216;b&#8217;<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"st0\">&#8216;cat&#8217;<\/span><span class=\"br0\">)<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>other_arr.<span class=\"me1\">dtype<\/span><span class=\"br0\">)<\/span><\/p>\n<p>numpy_arr\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">2<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">3<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">,<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">5<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">6<\/span><span class=\"br0\">)<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>numpy_arr.<span class=\"me1\">dtype<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">We used different type of elements in the above code snippet. Here is the output this script will show:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"sy0\">&lt;<\/span>U3<br \/>\nint64<\/div>\n<\/div>\n<p>This happens as characters are interpreted as unicode characters and second one is obvious.<\/p>\n<h4><strong>Reshape items of an array<\/strong><\/h4>\n<p>If a NumPy array consists of 2 rows and 4 columns, it can be reshaped to contain 4 rows and 2 columns. Let\u2019s write a simple code snippet for the same:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">original\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"br0\">(<\/span><span class=\"st0\">&#8216;1&#8217;<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"st0\">&#8216;b&#8217;<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"st0\">&#8216;c&#8217;<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"st0\">&#8216;4&#8217;<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"br0\">(<\/span><span class=\"st0\">&#8216;5&#8217;<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"st0\">&#8216;f&#8217;<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"st0\">&#8216;g&#8217;<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"st0\">&#8216;8&#8217;<\/span><span class=\"br0\">)<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>original<span class=\"br0\">)<\/span><br \/>\nreshaped\u00a0<span class=\"sy0\">=<\/span>\u00a0original.<span class=\"me1\">reshape<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">2<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>reshaped<span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Once we run the above code snippet, we will get the following output with both arrays printed to the screen:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"br0\">[<\/span><span class=\"br0\">[<\/span><span class=\"st0\">&#8216;1&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;b&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;c&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;4&#8217;<\/span><span class=\"br0\">]<\/span><br \/>\n<span class=\"br0\">[<\/span><span class=\"st0\">&#8216;5&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;f&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;g&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;8&#8217;<\/span><span class=\"br0\">]<\/span><span class=\"br0\">]<\/span><\/p>\n<p><span class=\"br0\">[<\/span><span class=\"br0\">[<\/span><span class=\"st0\">&#8216;1&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;b&#8217;<\/span><span class=\"br0\">]<\/span><br \/>\n<span class=\"br0\">[<\/span><span class=\"st0\">&#8216;c&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;4&#8217;<\/span><span class=\"br0\">]<\/span><br \/>\n<span class=\"br0\">[<\/span><span class=\"st0\">&#8216;5&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;f&#8217;<\/span><span class=\"br0\">]<\/span><br \/>\n<span class=\"br0\">[<\/span><span class=\"st0\">&#8216;g&#8217;<\/span>\u00a0<span class=\"st0\">&#8216;8&#8217;<\/span><span class=\"br0\">]<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p>Note how NumPy took care of shifting and associating the elements to new rows.<\/p>\n<h4><strong>Mathematical operations on items of an array<\/strong><\/h4>\n<p>Performing mathematical operations on items of an array is very simple. We will start by writing a simple code snippet to find out maximum, minimum and addition of all items of the array. Here is the code snippet:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">numpy_arr\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">1<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">2<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">3<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">5<\/span><span class=\"br0\">)<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>numpy_arr.<span class=\"kw2\">max<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>numpy_arr.<span class=\"kw2\">min<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>numpy_arr.<span class=\"kw2\">sum<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>numpy_arr.<span class=\"me1\">mean<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>np.<span class=\"me1\">sqrt<\/span><span class=\"br0\">(<\/span>numpy_arr<span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>np.<span class=\"me1\">std<\/span><span class=\"br0\">(<\/span>numpy_arr<span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">In the last 2 operations above, we also calculated the square root and standard deviation of each array items. The above snippet will provide the following output:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"nu0\">5<\/span><br \/>\n<span class=\"nu0\">1<\/span><br \/>\n<span class=\"nu0\">15<\/span><br \/>\n<span class=\"nu0\">3.0<\/span><br \/>\n<span class=\"br0\">[<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">1<\/span>.\u00a0\u00a0\u00a0<span class=\"nu0\">1.41421356<\/span>\u00a0\u00a0\u00a0<span class=\"nu0\">1.73205081<\/span>\u00a0\u00a0\u00a0<span class=\"nu0\">2<\/span>.\u00a0\u00a0\u00a0<span class=\"nu0\">2.23606798<\/span><span class=\"br0\">]<\/span><span class=\"br0\">]<\/span><br \/>\n<span class=\"nu0\">1.4142135623730951<\/span><\/div>\n<\/div>\n<h4><strong>Converting Python lists to NumPy arrays<\/strong><\/h4>\n<p>Even if you have been using Python lists in your existing programs and you don\u2019t want to change all of that code but still want to make use of NumPy arrays in your new code, it is good to know that we can easily convert a Python list to a NumPy array. Here is an example:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"co1\"># Create 2 new lists height and weight<\/span><br \/>\nheight\u00a0<span class=\"sy0\">=<\/span>\u00a0<span class=\"br0\">[<\/span><span class=\"nu0\">2.37<\/span><span class=\"sy0\">,<\/span>\u00a0\u00a0<span class=\"nu0\">2.87<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">1.52<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">1.51<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">1.70<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">2.05<\/span><span class=\"br0\">]<\/span><br \/>\nweight\u00a0<span class=\"sy0\">=<\/span>\u00a0<span class=\"br0\">[<\/span><span class=\"nu0\">91.65<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">97.52<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">68.25<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">88.98<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">86.18<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">88.45<\/span><span class=\"br0\">]<\/span><\/p>\n<p><span class=\"co1\"># Create 2 numpy arrays from height and weight<\/span><br \/>\nnp_height\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span>height<span class=\"br0\">)<\/span><br \/>\nnp_weight\u00a0<span class=\"sy0\">=<\/span>\u00a0np.<span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span>weight<span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Just to check, we can now print out the type of one of the variables:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">type<\/span><span class=\"br0\">(<\/span>np_height<span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">And this will show:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"sy0\">&lt;<\/span><span class=\"kw1\">class<\/span>\u00a0<span class=\"st0\">&#8216;numpy.ndarray&#8217;<\/span><span class=\"sy0\">&gt;<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">We can now perform a mathematical operations over all the items at once. Let\u2019s see how we can calculate the BMI of the people:<\/span><span id=\"ezoic-pub-ad-placeholder-160\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"co1\"># Calculate bmi<\/span><br \/>\nbmi\u00a0<span class=\"sy0\">=<\/span>\u00a0np_weight \/ np_height **\u00a0<span class=\"nu0\">2<\/span><\/p>\n<p><span class=\"co1\"># Print the result<\/span><br \/>\n<span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>bmi<span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">This will show the BMI of all the people calculated element-wise:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"br0\">[<\/span><span class=\"nu0\">16.31682957<\/span>\u00a0<span class=\"nu0\">11.8394056<\/span>\u00a0\u00a0<span class=\"nu0\">29.54033934<\/span>\u00a0<span class=\"nu0\">39.02460418<\/span>\u00a0<span class=\"nu0\">29.8200692<\/span>\u00a0\u00a0<span class=\"nu0\">21.04699584<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">Isn\u2019t that easy and handy? We can even filter data easily with a condition in place of an index inside square brackets:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">bmi<span class=\"br0\">[<\/span>bmi\u00a0<span class=\"sy0\">&gt;<\/span>\u00a0<span class=\"nu0\">25<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">This will give:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">29.54033934<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">39.02460418<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">29.8200692<\/span>\u00a0<span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<h3><strong>Create random sequences &amp; repetitions with NumPy<\/strong><\/h3>\n<p>With many features present in NumPy to create random data and arrange it in a required form, NumPy arrays are many times used in generating test dataset at many places, including debugging and testing purposes. For example, if you want to create an array from 0 to n, we can use the arange (note the single \u2018r\u2019) like the given snippet:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>np.<span class=\"me1\">arange<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">5<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">This will return the output as:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"br0\">[<\/span><span class=\"nu0\">0<\/span>\u00a0<span class=\"nu0\">1<\/span>\u00a0<span class=\"nu0\">2<\/span>\u00a0<span class=\"nu0\">3<\/span>\u00a0<span class=\"nu0\">4<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">The same function can be used to provide a lower value so that the array starts from other numbers than 0:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>np.<span class=\"me1\">arange<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">12<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">This will return the output as:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"br0\">[<\/span>\u00a0<span class=\"nu0\">4<\/span>\u00a0\u00a0<span class=\"nu0\">5<\/span>\u00a0\u00a0<span class=\"nu0\">6<\/span>\u00a0\u00a0<span class=\"nu0\">7<\/span>\u00a0\u00a0<span class=\"nu0\">8<\/span>\u00a0\u00a0<span class=\"nu0\">9<\/span>\u00a0<span class=\"nu0\">10<\/span>\u00a0<span class=\"nu0\">11<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p class=\"Normal1\"><span lang=\"UZ-CYR\">The numbers need not be continuous, they can skip a fix step like:<\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>np.<span class=\"me1\">arange<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">14<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">2<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p>This will return the output as:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"br0\">[<\/span>\u00a0<span class=\"nu0\">4<\/span>\u00a0<span class=\"nu0\">6<\/span>\u00a0<span class=\"nu0\">8<\/span>\u00a0<span class=\"nu0\">10<\/span>\u00a0<span class=\"nu0\">12<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p>We can also get the numbers in a decreasing order with a negative skip value:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>np.<span class=\"me1\">arange<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">14<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">4<\/span><span class=\"sy0\">,<\/span>\u00a0&#8211;<span class=\"nu0\">1<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p>This will return the output as:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"br0\">[<\/span><span class=\"nu0\">14<\/span>\u00a0<span class=\"nu0\">13<\/span>\u00a0<span class=\"nu0\">12<\/span>\u00a0<span class=\"nu0\">11<\/span>\u00a0<span class=\"nu0\">10<\/span>\u00a0<span class=\"nu0\">9<\/span>\u00a0<span class=\"nu0\">8<\/span>\u00a0<span class=\"nu0\">7<\/span>\u00a0<span class=\"nu0\">6<\/span>\u00a0<span class=\"nu0\">5<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p>It is possible to fund n numbers between x and y with equal space with linspace method, here is the code snippet for the same:<span id=\"ezoic-pub-ad-placeholder-140\" class=\"ezoic-adpicker-ad\"><\/span><\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\">np.<span class=\"me1\">linspace<\/span><span class=\"br0\">(<\/span>start<span class=\"sy0\">=<\/span><span class=\"nu0\">10<\/span><span class=\"sy0\">,<\/span>\u00a0stop<span class=\"sy0\">=<\/span><span class=\"nu0\">70<\/span><span class=\"sy0\">,<\/span>\u00a0num<span class=\"sy0\">=<\/span><span class=\"nu0\">10<\/span><span class=\"sy0\">,<\/span>\u00a0dtype<span class=\"sy0\">=<\/span><span class=\"kw2\">int<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p>This will return the output as:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw3\">array<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">10<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">16<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">23<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">30<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">36<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">43<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">50<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">56<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">63<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">70<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p>Please note that the output items are not equally spaced. NumPy does its best to do so but you need not rely on it as it does the rounding off.<\/p>\n<p>Finally, let us look at how we can generate a set of random sequence with NumPy which is one of the most used function for testing purposes. We will pass a range of numbers to NumPy which will be used as an initial and final point for the random numbers:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"kw1\">print<\/span><span class=\"br0\">(<\/span>np.<span class=\"kw3\">random<\/span>.<span class=\"me1\">randint<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span>\u00a0<span class=\"nu0\">10<\/span><span class=\"sy0\">,<\/span>\u00a0size<span class=\"sy0\">=<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">2<\/span><span class=\"sy0\">,<\/span><span class=\"nu0\">2<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><\/div>\n<\/div>\n<p>The above snippet creates a 2 by 2 dimensional NumPy array which will contain random numbers between 0 and 10. Here is the sample output:<\/p>\n<div class=\"codecolorer-container python default\">\n<div class=\"python codecolorer\"><span class=\"br0\">[<\/span><span class=\"br0\">[<\/span><span class=\"nu0\">0<\/span>\u00a0<span class=\"nu0\">4<\/span><span class=\"br0\">]<\/span><br \/>\n<span class=\"br0\">[<\/span><span class=\"nu0\">8<\/span>\u00a0<span class=\"nu0\">3<\/span><span class=\"br0\">]<\/span><span class=\"br0\">]<\/span><\/div>\n<\/div>\n<p>Please note as the numbers are random, the output can differ even between the 2 runs on the same machine.<\/p>\n<h4>Conclusion<\/h4>\n<p>In this lesson, we looked at various aspects of this computing library which we can use with Python to compute simple as well as complex mathematical problems which can arise in various use-cases The NumPy is one of the most important computation library when it comes to data engineering and calculating numerical dat, definitely a skill we need to have under our belt.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/python_numpy_tutorial\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson on Python\u00a0NumPy\u00a0library, we will look at how this library allows us to manage powerful N-dimensional array objects with sophisticated functions present to manipulate and operate over these arrays. To make this lesson complete, we will cover the following sections: What is Python NumPy package? NumPy arrays Different operations which can be done &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/01\/12\/python-numpy-tutorial-linux-hint\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python NumPy Tutorial \u2013 Linux Hint&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8081","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=8081"}],"version-history":[{"count":2,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8081\/revisions"}],"predecessor-version":[{"id":8540,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/8081\/revisions\/8540"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=8081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=8081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=8081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}