{"id":18728,"date":"2022-11-01T19:13:01","date_gmt":"2022-11-01T13:43:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/"},"modified":"2022-11-01T19:13:01","modified_gmt":"2022-11-01T13:43:01","slug":"solved-extracting-the-elements-matching-the-filter","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/","title":{"rendered":"[Solved] Extracting the elements matching the filter"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-28522318\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"28522318\" data-parentid=\"28522264\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I&#8217;m basing this answer on a very similar answer I wrote just a few hours ago.<\/p>\n<pre class=\"lang-py prettyprint-override\"><code>#from sklearn.feature_extraction.image import extract_patches  # similar to numpy's stride_tricks\nfrom numpy.lib.stride_tricks import as_strided\n\ndata = np.array([[1, 1 , 0 , 0 , 0 , 0 , 1 , 0],\n                 [1, 1 , 1 , 0 , 0 , 1 , 1 , 0],\n                 [1, 1 , 1 , 1 , 1 , 0 , 0 , 0],\n                 [0, 0 , 1 , 1 , 1 , 0 , 0 , 0],\n                 [0, 0 , 1 , 1 , 1 , 1 , 0 , 1],\n                 [1, 1 , 0 , 0 , 0 , 1 , 1 , 1],\n                 [1, 1 , 0 , 0 , 0 , 1 , 1 , 1]])\npatches = as_strided(data, shape=(data.shape[0]-2,data.shape[1]-2, 3, 3), strides=data.strides*2)\ndual = patches.all(axis=-1).all(axis=-1)\npatches[dual==False] = 0\npatches[dual] = 1\n<\/code><\/pre>\n<p>The result (in <code>data<\/code>) is as you&#8217;ve shown.<\/p>\n<p>The way this works is as follows:<br \/>\nthe function <code>extract_patches<\/code> generates <em>a view<\/em> into the array <code>data<\/code>, which means it is <em>not<\/em> a copy of the actual data but uses the <em>strides<\/em> to generate a seemingly different array. In this case, it will generate all possible 3&#215;3 submatrices (which can be overlapping) of the array <code>data<\/code>. Then, by writing <code>patches.all(axis=-1).all(axis=-1)<\/code>, you are first checking if the elements in the rows of the submatrices are all True (or equivalent to True in a boolean sense, so not 0, empty lists, empty dictionaries, empty tuples and a few other special cases), thereby collapsing one of the axes of this array and then with the second <code>.all(axis=-1)<\/code> the columns are being checked to see if they&#8217;re all True.<\/p>\n<p>Small example of this paragraph to clarify visually:<\/p>\n<pre class=\"lang-py prettyprint-override\"><code>&gt;&gt;&gt; A = np.array([\n... [1, 1, 0],        # -&gt; along this row, not all elements are 1: so `all` along the last axis will return False\n... [1, 1, 1],        # -&gt; along this row, all elements are 1: so `all` along the last axis (axis=-1) will return True\n... [1, 1, 1]])\n&gt;&gt;&gt; A.all(axis=-1)\narray([False,  True,  True], dtype=bool)  # so it is done along the last axis, along the rows\n&gt;&gt;&gt; A.all(axis=-1).all(axis=-1)\nFalse\n<\/code><\/pre>\n<p>So this array <code>dual<\/code> now has a &#8220;1&#8221; (True actually) for every 3&#215;3 submatrix that was full of ones. Those submatrices are overlapping however, so you first want to set the patches all to 0 whenevery any of these 3&#215;3 submatrices was not all ones (that&#8217;s the one-to-last line in the first code block: <code>patches[dual==False] = 0<\/code>) and <em>then<\/em> you can apply the ones again in each 3&#215;3 submatrix that originally had all ones.<br \/>\nThe alternatives are to correlate with a <code>kernel = np.ones((3,3))<\/code> or to take multiple bitwise operations into account (like in the other answer), but that last method becomes very difficult to write when the array&#8217;s dimensions grow beyond simply <code>(2,2)<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Extracting the elements matching the filter <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I&#8217;m basing this answer on a very similar answer I wrote just a few hours ago. #from sklearn.feature_extraction.image import extract_patches # similar to numpy&#8217;s stride_tricks from numpy.lib.stride_tricks import as_strided data = np.array([[1, 1 , 0 , 0 , 0 , 0 , 1 , 0], [1, 1 , 1 , 0 , 0 , &#8230; <a title=\"[Solved] Extracting the elements matching the filter\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/\" aria-label=\"More on [Solved] Extracting the elements matching the filter\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[581,2825,1699],"class_list":["post-18728","post","type-post","status-publish","format-standard","hentry","category-solved","tag-numpy","tag-scikit-image","tag-scipy"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Extracting the elements matching the filter - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Extracting the elements matching the filter - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I&#8217;m basing this answer on a very similar answer I wrote just a few hours ago. #from sklearn.feature_extraction.image import extract_patches # similar to numpy&#039;s stride_tricks from numpy.lib.stride_tricks import as_strided data = np.array([[1, 1 , 0 , 0 , 0 , 0 , 1 , 0], [1, 1 , 1 , 0 , 0 , ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T13:43:01+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Extracting the elements matching the filter\",\"datePublished\":\"2022-11-01T13:43:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/\"},\"wordCount\":275,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"numpy\",\"scikit-image\",\"scipy\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/\",\"name\":\"[Solved] Extracting the elements matching the filter - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-01T13:43:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Extracting the elements matching the filter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Extracting the elements matching the filter - JassWeb","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:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Extracting the elements matching the filter - JassWeb","og_description":"[ad_1] I&#8217;m basing this answer on a very similar answer I wrote just a few hours ago. #from sklearn.feature_extraction.image import extract_patches # similar to numpy's stride_tricks from numpy.lib.stride_tricks import as_strided data = np.array([[1, 1 , 0 , 0 , 0 , 0 , 1 , 0], [1, 1 , 1 , 0 , 0 , ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T13:43:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Extracting the elements matching the filter","datePublished":"2022-11-01T13:43:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/"},"wordCount":275,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["numpy","scikit-image","scipy"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/","url":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/","name":"[Solved] Extracting the elements matching the filter - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T13:43:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-extracting-the-elements-matching-the-filter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Extracting the elements matching the filter"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=18728"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18728\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}