{"id":841,"date":"2023-09-17T14:34:41","date_gmt":"2023-09-17T06:34:41","guid":{"rendered":"https:\/\/datascihubs.com\/?p=841"},"modified":"2023-09-17T14:34:42","modified_gmt":"2023-09-17T06:34:42","slug":"useful-indexing-techniques-in-numpy","status":"publish","type":"post","link":"https:\/\/datascihubs.com\/index.php\/2023\/09\/17\/useful-indexing-techniques-in-numpy\/","title":{"rendered":"Useful Indexing Techniques In NumPy"},"content":{"rendered":"\n<p class=\"has-text-align-justify\">When playing with data, many times we only want to access specific elements or subsets of data. Indexing in NumPy refers to the process of accessing specific elements or subsets of elements within a NumPy array. NumPy offers various indexing techniques to work with arrays efficiently.  <\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1498\" height=\"998\" src=\"https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-4494642.jpeg?resize=1498%2C998&#038;ssl=1\" alt=\"magnifying glass on book\" class=\"wp-image-843\" srcset=\"https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-4494642.jpeg?w=1880&amp;ssl=1 1880w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-4494642.jpeg?resize=300%2C200&amp;ssl=1 300w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-4494642.jpeg?resize=1024%2C682&amp;ssl=1 1024w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-4494642.jpeg?resize=768%2C512&amp;ssl=1 768w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-4494642.jpeg?resize=1536%2C1024&amp;ssl=1 1536w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-4494642.jpeg?resize=1320%2C880&amp;ssl=1 1320w, https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/pexels-photo-4494642.jpeg?resize=600%2C400&amp;ssl=1 600w\" sizes=\"auto, (max-width: 1498px) 100vw, 1498px\" \/><figcaption class=\"wp-element-caption\"><sup>Photo by Nothing Ahead on <a href=\"https:\/\/www.pexels.com\/photo\/magnifying-glass-on-book-4494642\/\" rel=\"nofollow\">Pexels.com<\/a><\/sup><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Table of Contents<\/h3>\n\n\n\n<div class=\"wp-block-aioseo-table-of-contents\"><ul><li><a href=\"#aioseo-1-basic-indexing-numpy-arrays-can-be-indexed-using-square-brackets-and-integer-indices-similar-to-python-lists-the-indexing-starts-from-0\">Basic Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Multidimensional Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Boolean Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Fancy Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Integer Array Indexing<\/a><\/li><li><a href=\"#aioseo-multidimensional-indexing\">Slicing with Steps<\/a><\/li><\/ul><\/div>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-1-basic-indexing-numpy-arrays-can-be-indexed-using-square-brackets-and-integer-indices-similar-to-python-lists-the-indexing-starts-from-0\">Basic Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">NumPy arrays can be indexed using square brackets and integer indices, similar to Python lists. The indexing starts from 0.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;0, 1, 2, 3, 4])\n\n# Accessing a single element\nelement = arr&#91;2]  # Returns 2\n\n# Slicing to get a subset of elements\nsubset = arr&#91;1:4]  # Returns &#91;1, 2, 3]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Multidimensional Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">NumPy supports indexing in multiple dimensions for multidimensional arrays (e.g., matrices). You can use comma-separated indices or separate brackets for each dimension.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\nmatrix = np.array(&#91;&#91;1, 2, 3], &#91;4, 5, 6], &#91;7, 8, 9]])\n\n# Accessing a single element\nelement = matrix&#91;1, 2]  # Returns 6\n\n# Slicing rows and columns\nrow = matrix&#91;0]  # Returns &#91;1, 2, 3]\ncol = matrix&#91;:, 1]  # Returns &#91;2, 5, 8]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Boolean Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">You can use boolean arrays to filter elements based on a condition. This is particularly useful for selecting elements that satisfy specific criteria.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;1, 2, 3, 4, 5])\n\n# Boolean indexing to select elements greater than 3\nfiltered = arr&#91;arr &gt; 3]  # Returns &#91;4, 5]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Fancy Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">NumPy allows you to use arrays of integers or other sequences as indices to access or modify multiple elements simultaneously.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;10, 20, 30, 40, 50])\n\n# Using an array of indices\nindices = np.array(&#91;0, 2, 4])\nselected = arr&#91;indices]  # Returns &#91;10, 30, 50]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Integer Array Indexing<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">NumPy also provides advanced indexing techniques like integer array indexing, which allows you to create arbitrary arrays using the data from another array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;&#91;1, 2], &#91;3, 4], &#91;5, 6]])\n\n# Integer array indexing to select specific elements\nselected = arr&#91;&#91;0, 1], &#91;1, 0]]  # Returns &#91;2, 3]<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"aioseo-multidimensional-indexing\">Slicing with Steps<\/h5>\n\n\n\n<p class=\"has-text-align-justify\">You can use slicing with step values to access elements at regular intervals.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\narr = np.array(&#91;0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n\n# Slicing with a step of 2\nsubset = arr&#91;1:8:2]  # Returns &#91;1, 3, 5, 7]<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">These indexing techniques provide great flexibility for accessing and manipulating data within NumPy arrays. Depending on your data analysis or computation needs, you can choose the most appropriate method to extract the information you require from the array.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When playing with data, many times we only want to access specific elements or subsets of data. Indexing in NumPy refers to the process of accessing specific elements or subsets of elements within a NumPy array. NumPy offers various indexing techniques to work with arrays efficiently. Table of Contents Basic Indexing NumPy arrays can be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":845,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[28,89,86,47,87,88],"class_list":["post-841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-information","tag-data-analysis","tag-np","tag-numpy","tag-python","tag-python-for-data-science","tag-python-library"],"blocksy_meta":[],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/datascihubs.com\/wp-content\/uploads\/2023\/09\/Indexing-in-NumPy.png?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/comments?post=841"}],"version-history":[{"count":1,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/841\/revisions"}],"predecessor-version":[{"id":844,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/posts\/841\/revisions\/844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/media\/845"}],"wp:attachment":[{"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/media?parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/categories?post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datascihubs.com\/index.php\/wp-json\/wp\/v2\/tags?post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}