Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
56 views

NumPy Array Copy Vs View

The document discusses the difference between copying and viewing NumPy arrays. A copy creates a new array, so changes to one do not affect the other. A view does not own the data, so changes to one affect the other. The base attribute can check if an array owns its data, with None indicating a copy and the original for a view.

Uploaded by

ahmed salem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

NumPy Array Copy Vs View

The document discusses the difference between copying and viewing NumPy arrays. A copy creates a new array, so changes to one do not affect the other. A view does not own the data, so changes to one affect the other. The base attribute can check if an array owns its data, with None indicating a copy and the original for a view.

Uploaded by

ahmed salem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

 Tutorials  Exercises  Get Certified  Services  Bootcamps Spaces Sign Up Log in

Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO   

NumPy Tutorial ADVERTISEMENT

NumPy HOME
NumPy Intro
NumPy Getting Started

NumPy Array Copy vs View


NumPy Creating Arrays
NumPy Array Indexing
NumPy Array Slicing
NumPy Data Types ❮ Previous Next ❯
NumPy Copy vs View
NumPy Array Shape
NumPy Array Reshape
NumPy Array Iterating
The Difference Between Copy and View
NumPy Array Join
The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the
NumPy Array Split original array.
NumPy Array Search
NumPy Array Sort The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original
NumPy Array Filter array will not affect the copy.

The view does not own the data and any changes made to the view will affect the original array, and any changes made to the
NumPy Random original array will affect the view.
Random Intro
Data Distribution
Random Permutation
COPY:
Seaborn Module
Normal Distribution
Binomial Distribution
Example Get your own Python Server

Make a copy, change the original array, and display both arrays:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])


x = arr.copy()
arr[0] = 42

print(arr)
print(x)

Try it Yourself »
COLOR PICKER

The copy SHOULD NOT be affected by the changes made to the original array.



VIEW:

Example
Make a view, change the original array, and display both arrays:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])


x = arr.view()
arr[0] = 42

print(arr)
print(x)

Try it Yourself »

The view SHOULD be affected by the changes made to the original array.

Make Changes in the VIEW:


ADVERTISEMENT

Example
Make a view, change the view, and display both arrays:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])


x = arr.view()
x[0] = 31

print(arr)
print(x)

Try it Yourself »

The original array SHOULD be affected by the changes made to the view.

ADVERTISEMENT

Check if Array Owns its Data


As mentioned above, copies owns the data, and views does not own the data, but how can we check this?

Every NumPy array has the attribute base that returns None if the array owns the data.

Otherwise, the base attribute refers to the original object.

Example
Print the value of the base attribute to check if an array owns it's data or not:

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

x = arr.copy()
y = arr.view()

print(x.base)
print(y.base)

Try it Yourself »

The copy returns None .


The view returns the original array.

Test Yourself With Exercises

Exercise:
Use the correct method to make a copy of the array.

arr = np.array([1, 2, 3, 4, 5])

x = arr.

Submit Answer »

Start the Exercise

❮ Previous Log in to track progress Next ❯

ADVERTISEMENT

ADVERTISEMENT

Spaces Upgrade Newsletter Get Certified Report Error

Top Tutorials Top References Top Examples Get Certified


HTML Tutorial HTML Reference HTML Examples HTML Certificate
CSS Tutorial CSS Reference CSS Examples CSS Certificate
JavaScript Tutorial JavaScript Reference JavaScript Examples JavaScript Certificate
How To Tutorial SQL Reference How To Examples Front End Certificate
SQL Tutorial Python Reference SQL Examples SQL Certificate
Python Tutorial W3.CSS Reference Python Examples Python Certificate
W3.CSS Tutorial Bootstrap Reference W3.CSS Examples PHP Certificate
Bootstrap Tutorial PHP Reference Bootstrap Examples jQuery Certificate
PHP Tutorial HTML Colors PHP Examples Java Certificate
Java Tutorial Java Reference Java Examples C++ Certificate
C++ Tutorial Angular Reference XML Examples C# Certificate
jQuery Tutorial jQuery Reference jQuery Examples XML Certificate

FORUM | ABOUT

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.


W3Schools is Powered by W3.CSS.

You might also like