

{"id":79324,"date":"2020-07-16T19:02:22","date_gmt":"2020-07-16T13:32:22","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=79324"},"modified":"2021-05-09T13:13:43","modified_gmt":"2021-05-09T07:43:43","slug":"numpy-ndarray","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/","title":{"rendered":"NumPy ndarray &#8211; NumPy N-Dimensional Array"},"content":{"rendered":"<p>NumPy contains the ndarray object. It is one of the most significant features of the NumPy library for performing logical and mathematical operations. It provides an abundance of functions and methods for performing operations on the array elements. NumPy arrays are grids that contain homogenous values.<\/p>\n<h2>NumPy N-dimensional Array (NumPy ndarray)<\/h2>\n<p>Arrays in NumPy are a group of elements, mostly homogenous numbers of the same type and size. Element indexing is in the form of tuples of positive integers depending on the number of dimensions.<\/p>\n<p>The values of the tuple determine the shape of the array. An array class in NumPy is ndarray. We can initialize the array elements in a range of ways.<\/p>\n<p>The data type of the individual array is given by a separate data-type object (dtype). Accessing and manipulation of array elements is by indexing and slicing the array. For this purpose, we use the methods and functions of the ndarray.<\/p>\n<p>Different ndarrays can also share the content. The changes then reflect in the corresponding array. An ndarray can be a \u201cview\u201d to another ndarray, and the data it refers to is held by the \u201cbase\u201d array.<\/p>\n<p>A basic array along with its data type is given as:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">X = np.array([[0,1,2],[3,4,5]], dtype='int16')\r\nprint(X)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[0 1 2]<br \/>\n[3 4 5]]<\/div>\n<h2>Internal Memory Layout of ndarray in NumPy<\/h2>\n<p>An instance of the ndarray class is given a continuous segment in the memory. The memory allocation along with the indexing scheme then maps N-integers to an element of the array block. The range of index variation is given by the shape of the array and vice-versa.<\/p>\n<p>The data-type object is used to define how many bytes each item will take. It also defines how the bytes will be inferred. For example, each int16 item has a size of 16 bits, I .e.16\/8= 2bytes.<\/p>\n<p>There are two key concepts determining the memory: dimensions and strides. Strides describe the number of bytes of each step in all the dimensions of the array when traversing through it. We can calculate the strides of an array using:<br \/>\n<strong>array.strides()<\/strong><\/p>\n<p>We can use the concept of strides to increase the execution speed.<\/p>\n<h2>NumPy Array attributes<\/h2>\n<p>The array attributes give information related to the array. Accessing array through its attributes helps to give an insight into its properties. Changes in attributes can be made of the elements, without new creations.<\/p>\n<p><strong>1. ndarray.flags-<\/strong> It provides information about memory layout<br \/>\n<strong>2. ndarray.shape-<\/strong> Provides array dimensions<br \/>\n<strong>3. ndarray.strides-<\/strong> Determines step size while traversing the arrays<br \/>\n<strong>4. ndarray.ndim-<\/strong> Number of array dimensions<br \/>\n<strong>5. ndarray.data-<\/strong> Points the starting position of array<br \/>\n<strong>6. ndarray.size-<\/strong> Number of array elements<br \/>\n<strong>7. ndarray.itemsize-<\/strong> Size of individual array elements in bytes<br \/>\n<strong>8. ndarray.base-<\/strong> Provides the base object, if it is a view<br \/>\n<strong>9. ndarray.nbytes-<\/strong> Provides the total bytes consumed by the array<br \/>\n<strong>10. ndarray.T-<\/strong> It gives the array transpose<br \/>\n<strong>11. ndarray.real-<\/strong> Separates the real part<br \/>\n<strong>12. ndarray.imag-<\/strong> Separates the imaginary<\/p>\n<h2>NumPy Indexing and Slicing<\/h2>\n<p>These are two very important concepts. It is useful when we want to work with sub-arrays.<\/p>\n<p>Indexing starts with zero as the first index. We can retrieve element values by its index value. For 2 or more dimensional arrays, we have to specify 2 or more indices. Indexing can be of two types<\/p>\n<h3>1. Integer array indexing in NumPy<\/h3>\n<p>We pass lists for indexing in all the dimensions. Then one to one mapping occurs for the creation of a new array.<\/p>\n<h3>2. Boolean array indexing in NumPy<\/h3>\n<p>In this type of indexing, we carry out a condition check. If the boolean condition satisfies we create an array of those elements.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=([1,2,5,6,7])\r\narr[3]\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">6<\/div>\n<p><strong>Slicing<\/strong> is similar to indexing, but it retrieves a string of values. The range is defined by the starting and ending indices. It is similar to lists in Python. The arrays can also be sliced. The arrays can be single or multidimensional. We can specify slices for all the dimensions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr=([1,2,5,6,7])\r\narr[2:5]\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[5, 6, 7]<\/div>\n<h2>Basic Operations in NumPy<\/h2>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Basic-Operations-of-NumPy.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79379\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Basic-Operations-of-NumPy.jpg\" alt=\"Basic Operations of NumPy\" width=\"566\" height=\"430\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Basic-Operations-of-NumPy.jpg 566w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Basic-Operations-of-NumPy-300x228.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Basic-Operations-of-NumPy-150x114.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/Basic-Operations-of-NumPy-520x395.jpg 520w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/a><\/p>\n<h3>1. NumPy Arithmetic Operations<\/h3>\n<p>We can perform arithmetic operations on the array to do an element-wise operation to create a new array. We use +=, -=, *= operators, to manipulate the existing array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\n \r\na = np.array([1, 2, 3, 4])\r\n \r\n# add 5 to every element\r\nprint ( a+5)\r\n \r\n# subtract 2 from each element\r\nprint ( a-2)\r\n \r\n# multiply each element by 5 \r\nprint (a*10)\r\n \r\n# divide each element by 2\r\nprint ( a\/2)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[6 7 8 9]<br \/>\n[-1 0 1 2]<br \/>\n[10 20 30 40]<br \/>\n[0.5 1. 1.5 2. ]<\/div>\n<h3>2. NumPy Unary Operators<\/h3>\n<p>There are many unary operators available to perform sum, min, max, etc. We can perform these functions row or column-wise.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">arr = np.array([[1,5, 12],\r\n                [2,32, 20],\r\n                [3, 40, 13]])\r\n \r\n \r\nprint(arr.max(axis = 1))\r\n \r\nprint (arr.min(axis = 0))\r\n \r\nprint ( arr.sum())\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[12 32 40]<br \/>\n[ 1 5 12]<br \/>\n128<\/div>\n<h3>3. NumPy Binary Operators<\/h3>\n<p>We can use all the arithmetic operators on the arrays and generate anew array. These operators work on two arrays and return a new array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\n \r\na = np.array([[1, 2],\r\n            [3, 4]])\r\nb = np.array([[4, 3],\r\n            [2, 1]])\r\n \r\nprint (a + b)\r\nprint (a*b)\r\nprint (a.dot(b))\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[[5 5]<br \/>\n[5 5]][[4 6]<br \/>\n[6 4]]<br \/>\n[[ 8 5]<br \/>\n[20 13]]<\/div>\n<h2>NumPy Universal Functions<\/h2>\n<p>NumPy provides many mathematical functions. These are applicable elements wise on the arrays. This includes trigonometric, exponential, and root functions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\n \r\na = np.array([0, np.pi\/2, np.pi])\r\nprint ( np.sin(a))\r\n \r\n \r\na = np.array([0, 1, 2, 3])\r\nprint ( np.exp(a))\r\n \r\n \r\nprint ( np.sqrt(a))\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[0.0000000e+00 1.0000000e+00 1.2246468e-16]<br \/>\n[ 1. 2.71828183 7.3890561 20.08553692]<br \/>\n[0. 1. 1.41421356 1.73205081]<\/div>\n<h2>Summary<\/h2>\n<p>NumPy ndarray object is the most basic concept of the NumPy library. To use the advanced features of NumPy, it is necessary to have a complete understanding of the ndarray object. The functions and methods in NumPy are all based on arrays which are instances of the ndarray class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy contains the ndarray object. It is one of the most significant features of the NumPy library for performing logical and mathematical operations. It provides an abundance of functions and methods for performing operations&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":79378,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22401],"tags":[22638,9174],"class_list":["post-79324","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-ndarray-in-numpy","tag-numpy-ndarray"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>NumPy ndarray - NumPy N-Dimensional Array - DataFlair<\/title>\n<meta name=\"description\" content=\"NumPy ndarray - NumPy N-Dimensional array - What is NumPy ndarray, its internal memory layout, Array attributes, Indexing &amp; Slicing, basic numpy operations\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy ndarray - NumPy N-Dimensional Array - DataFlair\" \/>\n<meta property=\"og:description\" content=\"NumPy ndarray - NumPy N-Dimensional array - What is NumPy ndarray, its internal memory layout, Array attributes, Indexing &amp; Slicing, basic numpy operations\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-16T13:32:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:43:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-NdArray.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NumPy ndarray - NumPy N-Dimensional Array - DataFlair","description":"NumPy ndarray - NumPy N-Dimensional array - What is NumPy ndarray, its internal memory layout, Array attributes, Indexing & Slicing, basic numpy operations","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/","og_locale":"en_US","og_type":"article","og_title":"NumPy ndarray - NumPy N-Dimensional Array - DataFlair","og_description":"NumPy ndarray - NumPy N-Dimensional array - What is NumPy ndarray, its internal memory layout, Array attributes, Indexing & Slicing, basic numpy operations","og_url":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-07-16T13:32:22+00:00","article_modified_time":"2021-05-09T07:43:43+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-NdArray.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"NumPy ndarray &#8211; NumPy N-Dimensional Array","datePublished":"2020-07-16T13:32:22+00:00","dateModified":"2021-05-09T07:43:43+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/"},"wordCount":807,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-NdArray.jpg","keywords":["ndarray in NumPy","NumPy ndarray"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/numpy-ndarray\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/","url":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/","name":"NumPy ndarray - NumPy N-Dimensional Array - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-NdArray.jpg","datePublished":"2020-07-16T13:32:22+00:00","dateModified":"2021-05-09T07:43:43+00:00","description":"NumPy ndarray - NumPy N-Dimensional array - What is NumPy ndarray, its internal memory layout, Array attributes, Indexing & Slicing, basic numpy operations","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/numpy-ndarray\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-NdArray.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-NdArray.jpg","width":1200,"height":628,"caption":"NumPy NdArray"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/numpy-ndarray\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"NumPy Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/numpy\/"},{"@type":"ListItem","position":3,"name":"NumPy ndarray &#8211; NumPy N-Dimensional Array"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=79324"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79324\/revisions"}],"predecessor-version":[{"id":93078,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79324\/revisions\/93078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/79378"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=79324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=79324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=79324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}