{"id":14352,"date":"2021-03-29T16:31:20","date_gmt":"2021-03-29T16:31:20","guid":{"rendered":"https:\/\/www.askpython.com\/?p=14352"},"modified":"2021-03-29T16:31:21","modified_gmt":"2021-03-29T16:31:21","slug":"numpy-set-operations","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/numpy-set-operations","title":{"rendered":"NumPy Set Operations to know!"},"content":{"rendered":"\n<p>Hello, readers! In this article, we will learn the universal NumPy Set Operations in Python. So, let us get started! \ud83d\ude42<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Useful Numpy set operations <\/h2>\n\n\n\n<p>We&#8217;re going over 5 useful numpy set operations in this article. <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>numpy.unique(array)<\/code><\/li><li><code>numpy.union1d(array,array)<\/code><\/li><li><code>numpy.intersect1d(array,array,assume_unique)<\/code><\/li><li><code>np.setdiff1d(arr1, arr2, assume_unique=True)<\/code><\/li><li><code>np.setxor1d(arr1, arr2, assume_unique=True)<\/code><\/li><\/ol>\n\n\n\n<p>Let&#8217;s check these operations individually. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-2\"><\/span><span id=\"eh-1\"><\/span>1. Unique values from a NumPy Array<\/h3>\n\n\n\n<p>This numpy set operation helps us find unique values from the set of array elements in Python. The <code>numpy.unique()<\/code> function skips all the duplicate values and represents only the unique elements from the Array<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.unique(array)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>In this example, we have used unique() function to select and display the unique elements from the set of array. Thus, it skips the duplicate value 30 and selects it only once.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr = np.array(&#x5B;30,60,90,30,100])\ndata = np.unique(arr)\nprint(data)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B; 30  60  90 100]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-3\"><\/span><span id=\"eh-2\"><\/span>2. Set union operation on NumPy Array<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-module\" class=\"rank-math-link\">NumPy<\/a> offers us with universal <code>union1d()<\/code> function that performs UNION operation on both the arrays. <\/p>\n\n\n\n<p>That is, it clubs the values from both the arrays and represents them. This process completely neglects the duplicate values and includes only a single occurrence of the duplicate element in the UNION set of arrays.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.union1d(array,array)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr1 = np.array(&#x5B;30,60,90,30,100])\narr2 = np.array(&#x5B;1,2,3,60,30])\n\ndata = np.union1d(arr1,arr2)\n\nprint(data)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;  1   2   3  30  60  90 100]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span id=\"eh-4\"><\/span><span id=\"eh-3\"><\/span>3. Set Intersection operation on NumPy array<\/h3>\n\n\n\n<p>The <code>intersect1d() function<\/code> enables us to perform INTERSECTION operation on the arrays. That is, it selects and represents the common elements from both the arrays.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.intersect1d(array,array,assume_unique)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li>assume_unique: If set to TRUE, it includes the duplicate values for intersection operation. Setting it to FALSE, would result in the neglection of duplicate values for intersection operation.<\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Here, as we have set <code>assume_unique<\/code> to TRUE, the intersection operation has been performed including the duplicate values i.e. it selects the common values from both the arrays including the duplicates of those common elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr1 = np.array(&#x5B;30,60,90,30,100])\narr2 = np.array(&#x5B;1,2,3,60,30])\n\ndata = np.intersect1d(arr1, arr2, assume_unique=True)\n\nprint(data)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;30 30 60]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Finding uncommon values with NumPy Array<\/h2>\n\n\n\n<p>With <code>setdiff1d()<\/code> function, we can find and represent all the elements from the 1st array that are not present in the 2nd array according to the parameters passed to the function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr1 = np.array(&#x5B;30,60,90,30,100])\narr2 = np.array(&#x5B;1,2,3,60,30])\n\ndata = np.setdiff1d(arr1, arr2, assume_unique=True)\n\nprint(data)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B; 90 100]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><span id=\"eh-6\"><\/span><span id=\"eh-5\"><\/span>5. Symmetric Differences<\/h2>\n\n\n\n<p>With <code>setxor1d()<\/code> function, we can calculate the symmetric differences between the array elements. That is, it selects and represents all the elements that are not common in both the arrays. Thus, it omits all the common values from the arrays and represents the distinct values with respect to both the arrays.<\/p>\n\n\n\n<p><strong>Example:<\/strong> <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\narr1 = np.array(&#x5B;30,60,90,30,100])\narr2 = np.array(&#x5B;1,2,3,60,30])\n\ndata = np.setxor1d(arr1, arr2, assume_unique=True)\n\nprint(data)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;  1   2   3  90  100]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. For more such posts related to Python programming, Stay tuned with us.<\/p>\n\n\n\n<p>Till then, Happy Learning!! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, readers! In this article, we will learn the universal NumPy Set Operations in Python. So, let us get started! \ud83d\ude42 Useful Numpy set operations We&#8217;re going over 5 useful numpy set operations in this article. numpy.unique(array) numpy.union1d(array,array) numpy.intersect1d(array,array,assume_unique) np.setdiff1d(arr1, arr2, assume_unique=True) np.setxor1d(arr1, arr2, assume_unique=True) Let&#8217;s check these operations individually. 1. Unique values from a [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":14359,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93,1],"tags":[],"class_list":["post-14352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=14352"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/14352\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/14359"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=14352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=14352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=14352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}