

{"id":79317,"date":"2020-07-25T14:43:03","date_gmt":"2020-07-25T09:13:03","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=79317"},"modified":"2021-05-09T13:13:35","modified_gmt":"2021-05-09T07:43:35","slug":"numpy-copy-view","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/","title":{"rendered":"NumPy Copies and Views  &#8211; Copy Vs View in NumPy"},"content":{"rendered":"<p>NumPy consists of different methods to duplicate an original array. The two main functions for this duplication are copy and view. The duplication of the array means an array assignment.<\/p>\n<p>When we duplicate the original array, the changes made in the new array may or may not reflect. The duplicate array may use the same location or may be at a new memory location.<\/p>\n<h2>Copy or Deep copy in NumPy<\/h2>\n<p>It returns a copy of the original array stored at a new location. The copy doesn\u2019t share data or memory with the original array. The modifications are not reflected. The copy function is also known as <strong>deep<\/strong> copy.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr = np.array([20,30,50,70])\r\na= arr.copy()\r\n#changing a value in original array\r\narr[0] = 100\r\n \r\nprint(arr)\r\nprint(a)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[100 30 50 70]<br \/>\n[20 30 50 70]<\/div>\n<p>Changes made in the original array are not reflected in the copy.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr = np.array([20,30,50,70])\r\na= arr.copy()\r\n#changing a value in copy array\r\na[0] = 5\r\n \r\nprint(arr)\r\nprint(a)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[20 30 50 70]<br \/>\n[ 5 30 50 70]<\/div>\n<p>Changes made in copy are not reflected in the original array<\/p>\n<h2>View or Shallow copy in NumPy<\/h2>\n<p>It returns a view of the original array stored at the existing location. The view doesn\u2019t have its own data or memory but uses the original array. The modifications reflect in both. We also call the view function <strong>shallow<\/strong> copy.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr = np.array([20,30,50,70])\r\na= arr.view()\r\n#changing a value in original array\r\narr[0] = 100\r\n \r\nprint(arr)\r\nprint(a)\r\n \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[100 30 50 70]<br \/>\n[100 30 50 70]<\/div>\n<p>Changes made in the original array reflect in the view.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr = np.array([20,30,50,70])\r\na= arr.view()\r\n#changing a value in the view \r\na[0] = 80\r\n \r\nprint(arr)\r\nprint(a)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[80 30 50 70]<br \/>\n[80 30 50 70]<\/div>\n<p>Changes made in the view reflected in the original array.<\/p>\n<h2>No copy in NumPy<\/h2>\n<p>When we make assignments to an array it does not create a copy of the array. It actually accesses the original array through its id(). The id() element is equivalent to pointers in c \/c++ language. They just point to the original array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np \r\n \r\n#declaring an array \r\narr1 = np.arange(10) \r\nprint(arr1)\r\n \r\n#applying id() function\r\nprint (id(arr1))\r\n \r\n#assign the array to another variable \r\narr2 = arr1\r\nprint (arr2) \r\n \r\n#we print the id() for arr2\r\nprint( id(arr2)  )\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[0 1 2 3 4 5 6 7 8 9]<br \/>\n139825053003648<br \/>\n[0 1 2 3 4 5 6 7 8 9]<br \/>\n139825053003648<\/div>\n<h2>NumPy View Creation<\/h2>\n<p>We can create a view via two methods. We can either create it by slicing the original array or changing its data type.<\/p>\n<h3>NumPy Slice view<\/h3>\n<p>In this type of view creation, we perform slicing of the original array. We can then address the view by offsets, strides, and counts of the original array.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr= np.arange(10)\r\nprint(arr)\r\n \r\n#slicing of original array to create a view\r\nv=arr[1:10:2]\r\nprint(v)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[0 1 2 3 4 5 6 7 8 9]<br \/>\n[1 3 5 7 9]<\/div>\n<h3>NumPy dtype view<\/h3>\n<p>In this case we change the data type of the original array and generate a view.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\narr= np.arange(10,dtype='int32')\r\nprint(arr)\r\n \r\n#chanding datatype of original array to create a view\r\nv=arr.view('int16')\r\nprint(v)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[0 1 2 3 4 5 6 7 8 9]<br \/>\n[0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0]<\/div>\n<h2>Checking if NumPy Array owns its data<\/h2>\n<p>We know that copy owns its data while the view does not. We can check this with the base the attribute that returns a none value if array owns its data.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import numpy as np\r\n \r\narr = np.array([59,87,64])\r\n \r\na = arr.copy()\r\nb = arr.view()\r\n \r\nprint(a.base)\r\nprint(b.base)\r\n \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">None<br \/>\n[59 87 64]<\/div>\n<h2>Difference between NumPy Copy Vs View<\/h2>\n<p>The main highlight difference between a copy and view it in its memory location. The copy of an array is a new array. The view, on the other hand, is just a view of the original array.<\/p>\n<p>We store the copy at a new memory location. A copy returns the data stored at the new location. The changes made in the copy data does not reflect in the original array. The copy of an array owns its own data.<\/p>\n<p>The view is a view of the original array at the same memory location. A view returns the data stored at the original memory location. The changes made in the original data are reflected in the view and vice versa. The view of an array does not own its own data.<\/p>\n<h2>Summary<\/h2>\n<p>NumPy copy and view functions are very useful for array duplication. The difference in their properties helps utilize the duplication in a variety of ways. The changes in the original array can be monitored depending on the type of duplication function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy consists of different methods to duplicate an original array. The two main functions for this duplication are copy and view. The duplication of the array means an array assignment. When we duplicate the&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":79793,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22401],"tags":[22758,22760,22762,22761,22759],"class_list":["post-79317","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-numpy-copies","tag-numpy-copy","tag-numpy-copy-vs-view","tag-numpy-view","tag-numpy-views"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>NumPy Copies and Views - Copy Vs View in NumPy - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn NumPy Copy and View - Deep Copy, shallow copy and No copy in NumPy, NumPy view creation and types with examples, NumPy View vs Copy\" \/>\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-copy-view\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy Copies and Views - Copy Vs View in NumPy - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn NumPy Copy and View - Deep Copy, shallow copy and No copy in NumPy, NumPy view creation and types with examples, NumPy View vs Copy\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/\" \/>\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-25T09:13:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:43:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-copy-view.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=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NumPy Copies and Views - Copy Vs View in NumPy - DataFlair","description":"Learn NumPy Copy and View - Deep Copy, shallow copy and No copy in NumPy, NumPy view creation and types with examples, NumPy View vs Copy","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-copy-view\/","og_locale":"en_US","og_type":"article","og_title":"NumPy Copies and Views - Copy Vs View in NumPy - DataFlair","og_description":"Learn NumPy Copy and View - Deep Copy, shallow copy and No copy in NumPy, NumPy view creation and types with examples, NumPy View vs Copy","og_url":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-07-25T09:13:03+00:00","article_modified_time":"2021-05-09T07:43:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-copy-view.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":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"NumPy Copies and Views &#8211; Copy Vs View in NumPy","datePublished":"2020-07-25T09:13:03+00:00","dateModified":"2021-05-09T07:43:35+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/"},"wordCount":557,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-copy-view.jpg","keywords":["numpy copies","numpy copy","numpy copy vs view","numpy view","numpy views"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/numpy-copy-view\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/","url":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/","name":"NumPy Copies and Views - Copy Vs View in NumPy - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-copy-view.jpg","datePublished":"2020-07-25T09:13:03+00:00","dateModified":"2021-05-09T07:43:35+00:00","description":"Learn NumPy Copy and View - Deep Copy, shallow copy and No copy in NumPy, NumPy view creation and types with examples, NumPy View vs Copy","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/numpy-copy-view\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-copy-view.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/07\/NumPy-copy-view.jpg","width":1200,"height":628,"caption":"NumPy copy and View"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/numpy-copy-view\/#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 Copies and Views &#8211; Copy Vs View in NumPy"}]},{"@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\/79317","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=79317"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79317\/revisions"}],"predecessor-version":[{"id":93067,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/79317\/revisions\/93067"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/79793"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=79317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=79317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=79317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}