{"id":905,"date":"2024-02-23T13:24:38","date_gmt":"2024-02-23T13:24:38","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=905"},"modified":"2024-02-23T13:24:40","modified_gmt":"2024-02-23T13:24:40","slug":"numpy-filter-array","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/23\/numpy-filter-array\/","title":{"rendered":"NumPy\u00a0Filter Array"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Filtering Arrays<\/h2>\n\n\n\n<p>Getting some elements out of an existing array and creating a new array out of them is called&nbsp;<em>filtering<\/em>.<\/p>\n\n\n\n<p>In NumPy, you filter an array using a&nbsp;<em>boolean index list<\/em>.<\/p>\n\n\n\n<p>A&nbsp;<em>boolean index list<\/em>&nbsp;is a list of booleans corresponding to indexes in the array.<\/p>\n\n\n\n<p>If the value at an index is&nbsp;<code>True<\/code>&nbsp;that element is contained in the filtered array, if the value at that index is&nbsp;<code>False<\/code>&nbsp;that element is excluded from the filtered array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Create an array from the elements on index 0 and 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;41,&nbsp;42,&nbsp;43,&nbsp;44])<br><br>x = &#91;True,&nbsp;False,&nbsp;True,&nbsp;False]<br><br>newarr = arr&#91;x]<br><br>print(newarr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_filter1\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<p>The example above will return&nbsp;<code>[41, 43]<\/code>, why?<\/p>\n\n\n\n<p>Because the new array contains only the values where the filter array had the value&nbsp;<code>True<\/code>, in this case, index 0 and 2.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the Filter Array<\/h2>\n\n\n\n<p>In the example above we hard-coded the&nbsp;<code>True<\/code>&nbsp;and&nbsp;<code>False<\/code>&nbsp;values, but the common use is to create a filter array based on conditions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Create a filter array that will return only values higher than 42:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;41,&nbsp;42,&nbsp;43,&nbsp;44])<br><br># Create an empty list<br>filter_arr = &#91;]<br><br># go through each element in arr<br>for&nbsp;element&nbsp;in&nbsp;arr:<br>&nbsp;&nbsp;# if the element is higher than 42, set the value to True, otherwise False:<br>&nbsp;&nbsp;if&nbsp;element &gt;&nbsp;42:<br>&nbsp;&nbsp;&nbsp;&nbsp;filter_arr.append(True)<br>&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp; filter_arr.append(False)<br><br>newarr = arr&#91;filter_arr]<br><br>print(filter_arr)<br>print(newarr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_filter2\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Create a filter array that will return only even elements from the original array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6,&nbsp;7])<br><br># Create an empty list<br>filter_arr = &#91;]<br><br># go through each element in arr<br>for&nbsp;element&nbsp;in&nbsp;arr:<br>&nbsp;&nbsp;# if the element is completely divisble by 2, set the value to True, otherwise False<br>&nbsp;&nbsp;if&nbsp;element %&nbsp;2&nbsp;==&nbsp;0:<br>&nbsp;&nbsp;&nbsp;&nbsp;filter_arr.append(True)<br>&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp; filter_arr.append(False)<br><br>newarr = arr&#91;filter_arr]<br><br>print(filter_arr)<br>print(newarr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_filter3\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Creating Filter Directly From Array<\/h2>\n\n\n\n<p>The above example is quite a common task in NumPy and NumPy provides a nice way to tackle it.<\/p>\n\n\n\n<p>We can directly substitute the array instead of the iterable variable in our condition and it will work just as we expect it to.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Create a filter array that will return only values higher than 42:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;numpy&nbsp;as&nbsp;np<br><br>arr = np.array(&#91;41,&nbsp;42,&nbsp;43,&nbsp;44])<br><br>filter_arr = arr&nbsp;&gt;&nbsp;42<br><br>newarr = arr&#91;filter_arr]<br><br>print(filter_arr)<br>print(newarr)<\/code><\/pre>\n\n\n\n<p><a target=\"_blank\" href=\"https:\/\/www.w3schools.com\/python\/numpy\/trypython.asp?filename=demo_numpy_array_filter4\" rel=\"noreferrer noopener\">Try it Yourself \u00bb<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Create a filter array that will return only even elements from the original array:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import\u00a0numpy\u00a0as\u00a0np\n\narr = np.array(&#91;1,\u00a02,\u00a03,\u00a04,\u00a05,\u00a06,\u00a07])\n\nfilter_arr = arr\u00a0%\u00a02\u00a0==\u00a00\n\nnewarr = arr&#91;filter_arr]\n\nprint(filter_arr)\nprint(newarr)\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Filtering Arrays Getting some elements out of an existing array and creating a new array out of them is called&nbsp;filtering. In NumPy, you filter an array using a&nbsp;boolean index list. A&nbsp;boolean index list&nbsp;is a list of booleans corresponding to indexes in the array. If the value at an index is&nbsp;True&nbsp;that element is contained in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-905","post","type-post","status-publish","format-standard","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=905"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/905\/revisions"}],"predecessor-version":[{"id":906,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/905\/revisions\/906"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}