{"id":4790,"date":"2020-09-10T16:59:41","date_gmt":"2020-09-10T11:29:41","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4790"},"modified":"2020-09-10T16:59:44","modified_gmt":"2020-09-10T11:29:44","slug":"getting-started-with-numpy","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/getting-started-with-numpy\/","title":{"rendered":"Getting Started with NumPy"},"content":{"rendered":"\n<p>NumPy stands for&nbsp;<strong>Numerical Python<\/strong>. It is a tool for mathematical computing and data preparation in Python. It can be utilized to perform several mathematical operations on arrays such as trigonometric, statistical, and algebraic routines. This library provides many useful features including handling n-dimensional arrays, broadcasting, performing operations, data generation, etc., thus, it\u2019s the fundamental package for scientific computing with Python. It also provides a large collection of high-level mathematical functions to operate on arrays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why use NumPy..?<\/strong><\/h2>\n\n\n\n<p>It is incredibly fast, as it has bindings to C libraries. In Python, we have lists that serve the purpose of arrays, but they are slow to process. Python list has fewer properties than the NumPy array. So using Numpy is more suggestible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why is NumPy Faster Than Lists..?<\/strong><\/h2>\n\n\n\n<p>NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently. This behavior is called locality of reference in computer science. This is the main reason why NumPy is faster than lists. Also, it is optimized to work with the latest <a href=\"https:\/\/computersciencewiki.org\/index.php\/Architecture_of_the_central_processing_unit_(CPU)\" rel=\"nofollow noopener\" target=\"_blank\">CPU architectures<\/a>.<\/p>\n\n\n\n<p>Numpy is surprisingly compact, fast, and easy to use, so let\u2019s dive into the installation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Installation of NumPy<\/strong><\/h2>\n\n\n\n<p>If you have Python and pip already installed on a system, then the installation of NumPy is very easy.<\/p>\n\n\n\n<p>Install it using this command:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>C:\\Users\\<em>Your Name<\/em>&gt;pip install numpyif<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>If you have anaconda already installed in your machine then it is easy to install this package with conda run:<\/p>\n\n\n\n<p>Type this command in Jupyter notebook:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>conda install -c anaconda numpy<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Once NumPy is installed, import it in your applications<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Import NumPy<\/strong><\/h2>\n\n\n\n<p>We can import Numpy into our application by using import keyword<\/p>\n\n\n\n<p>NumPy is usually imported under the np alias.<\/p>\n\n\n\n<p>What is an <strong>alias:<\/strong> In Python alias are an alternate name for referring to the same thing.<\/p>\n\n\n\n<p>Create an alias with the \u2018as\u2019 keyword while importing:<\/p>\n\n\n\n<p>import numpy as np<\/p>\n\n\n\n<p>Now the NumPy package can be referred to as np instead of numpy<\/p>\n\n\n\n<p>Numpy has many different built-in functions and capabilities. We will focus on some of the most important aspects in Numpy Now let\u2019s discuss the most important concept in numpy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>NumPy Arrays<\/strong><\/h2>\n\n\n\n<p>So, what are arrays?<\/p>\n\n\n\n<p>An array is a data structure consisting of a collection of elements, each identified by at least one array index or key.<\/p>\n\n\n\n<p>NumPy is used to work with arrays. The array object in NumPy is called ndarray.<\/p>\n\n\n\n<p>We can create a NumPy ndarray object by using the array() function.<\/p>\n\n\n\n<p>The pre-built function np.array is the correct way to create an array.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np&nbsp;\narr = np.array([1, 2, 3, 4, 5])\nprint(arr)\nprint(type(arr)).<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>[1 2 3 4 5]<br>&lt;class \u2018numpy.ndarray\u2019&gt;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>2-D Arrays<\/strong><\/p>\n\n\n\n<p>An array of arrays is known as 2D array. The two dimensional (2D) array is also known as matrix. A matrix can be represented as a table of rows and columns. Before we discuss more about two Dimensional arrays let&#8217;s have a look at the following example.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>import numpy as np<br>arr = np.array([[1, 2, 3], [4, 5, 6]])<br>print(arr)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>[[1 2 3]<br>[4 5 6]]<\/p>\n\n\n\n<p><strong>3-D Arrays<\/strong><\/p>\n\n\n\n<p>A 3D array is a multi-dimensional array(array of arrays). A 3D array is a collection of 2D arrays . It is specified by using three subscripts&nbsp;<\/p>\n\n\n\n<p>Block size, Row size and Column size<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<p>Create a 3-D array with two 2-D arrays, both containing two arrays with the values 1,2,3 and 4,5,6:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\narr = np.array([[[1, 2],[4, 5]],[[1, 2],[4, 5]]])\nprint(arr)<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>[[[1 2][4 5]]<br>[[1 2][4 5]]]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Check Number of Dimensions?<\/p>\n\n\n\n<p>NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Check how many dimensions the arrays have:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\na = np.array(42)\nb = np.array([1, 2, 3])\nc = np.array([[1, 2], [4, 5]])\nd = np.array([[[1, 2], [4, 5]], [[1, 2], [4, 5]]])\n\nprint(a.ndim)&nbsp;\nprint(b.ndim)&nbsp;\nprint(c.ndim)&nbsp;\nprint(d.ndim)\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<br>1<br>2<br>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>NumPy Array Indexing<\/strong><\/h2>\n\n\n\n<p><strong>Access Array Elements<\/strong><\/p>\n\n\n\n<p>Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1, etc.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\na = np.array([1,2,3,4])\nprint(arr[2])\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>3<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Access 2-D Arrays<\/strong><\/h2>\n\n\n\n<p>To access elements from 2-D arrays we can use comma-separated integers representing the dimension and the index of the element.<\/p>\n\n\n\n<p>The first element indicates the row and the second element indicates the column<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Access the 2nd element on 1st dim:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\narr = np.array([[1,2,3], [6,7,8]])\nprint(arr[0,2])\nprint(arr[1,2])\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>3<br>8<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Negative Indexing<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\narr = np.array([[1,2,3,4,5], [6,7,8,9,10]])\nprint('Last element from 2nd dim: ', arr[1, -1])\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Last element from 2nd dim:&nbsp; 9<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Built-In Methods<\/strong><\/h2>\n\n\n\n<p>Numpy allows us to use many built-in methods for generating arrays. Let\u2019s examine the most used from those, as well as their purpose:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>np.arange( ) \u2013 array of arranged values from low to high value<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>np.zeros( ) \u2013 array of zeros with specified shape<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>np.ones( ) \u2013 similarly to zeros, array of ones with specified shape<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>np.linspace( ) \u2013 array of linearly spaced numbers, with specified size<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Arange<\/strong><\/h2>\n\n\n\n<p>Numpy\u2019s arange function will return evenly spaced values within a given interval. Works similarly to <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-are-python-functions\/\">Python\u2019s range() function<\/a>. The only required parameter is \u2018stop\u2019, while all the other parameters are optional:<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\narr=np.arange(start=1, stop=10)\nprint(arr)\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>array([1, 2, 3, 4, 5, 6, 7, 8, 9])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Zeros and ones<\/strong><\/h2>\n\n\n\n<p>Numpy provides functions that are able to create arrays of 1\u2019s and 0\u2019s. The required parameter for these functions is \u2018shape\u2019.<\/p>\n\n\n\n<p><strong>Example for Zeros:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr=np.zeros(shape=5)\narr\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>array([0., 0., 0., 0., 0.])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example for Ones:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr=np.ones(3)\narr<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>array([1., 1., 1.])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>linspace<\/strong><\/h2>\n\n\n\n<p>Numpy\u2019s linspace function will return evenly spaced numbers over a specified interval. Required parameters for this functions are \u2018start\u2019 and \u2018stop\u2019.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr=np.linspace(start=1, stop=100, num=5)\narr\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>array([&nbsp; 1.&nbsp; ,&nbsp; 25.75,&nbsp; 50.5 ,&nbsp; 75.25, 100.&nbsp; ])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Random Numbers in NumPy<\/strong><\/h2>\n\n\n\n<p>Numpy allows you to use various functions to produce arrays with random values. To access these functions, first we have to access the \u2018random\u2019 function itself. This is done using \u2018np.random\u2019, after which we specify which function we need. Here is a list of the most used random functions and their purpose:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>np.random.rand() \u2013 produce random values in the given shape from 0 to 1<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>np.random.randn() \u2013 produce random values with a \u2018standard normal\u2019 distribution, from -1 to 1<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>np.random.randint() \u2013 produce random numbers from low to high, specified as a parameter<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Rand<\/strong><\/h3>\n\n\n\n<p>The rand function uses only one parameter which is the \u2018shape\u2019 of the output. You need to specify the output format you need, whether it is one or two-dimensional array. If there is no argument passed to the function, it returns a single value. Otherwise, it produces several numbers as specified<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr=np.random.rand(2)\narr<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>array([0.66988993, 0.34697049])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Randn<\/strong><\/h3>\n\n\n\n<p>The randn function is similar to the rand function, except it produces a number with standard normal distribution. What this means, is that it generates number with distribution of 1 and mean of 0, i.e. value from -1 to +1 by default:<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr=np.random.randn(2)\narr<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>array([1.35689157, -0.36031165])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Randint<\/strong><\/h3>\n\n\n\n<p>Randint is used to generate whole random numbers, ranging between low(inclusive) and high(exclusive) value. Specifying a parameter like \u2018(1, 100)\u2019 will create random values from 1 to 99.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr=np.random.randint(1,10,5) \/\/ (start,stop,number of elements )\narr<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>array([7, 3, 9, 8, 1])<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Reshape<\/strong><\/h3>\n\n\n\n<p>This method allows you to transform a one-dimensional array to more dimensional, or the other way around. Reshape will not affect your data values. Let\u2019s check out this code:<\/p>\n\n\n\n<p>arr=np.arange(16)<\/p>\n\n\n\n<p>array([7, 3, 9, 8, 1])<\/p>\n\n\n\n<p>Now we can reshape this<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\">ar=arr.reshape(4,4)\narray([[ 0,&nbsp; 1,&nbsp; 2,&nbsp; 3],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ 4,&nbsp; 5,&nbsp; 6,&nbsp; 7],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[ 8,&nbsp; 9, 10, 11],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[12, 13, 14, 15]])<\/pre>\n<\/div><\/div>\n\n\n\n<p>The array named \u2018arr\u2019 is now reshaped into a 4 by 4 matrix and by this, we can specify the number of rows and the number of columns. The key thing to notice is that the array still has all 16 elements.<\/p>\n\n\n\n<p>These are all the basic functionalities off NumPy. We will discuss the rest of functionalities in another article<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy stands for&nbsp;Numerical Python. It is a tool for mathematical computing and data preparation in Python. It can be utilized to perform several mathematical operations on arrays such as trigonometric, statistical, and algebraic routines. This library provides many useful features including handling n-dimensional arrays, broadcasting, performing operations, data generation, etc., thus, it\u2019s the fundamental package [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4816,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[500],"tags":[1344,1345],"class_list":["post-4790","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science-using-python-tutorials","tag-numerical-python","tag-numpy"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4790"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4790\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/4816"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}