1

In Numpy we can make a new array object that looks at same data from another array object using view method.

Which will make a shallow object of an array. the new array will have the same data from a existing one (actually reference to it), so the data part of the new array will be just a reference to the data of a existing one!(we can resize the new one without changing the old array shape but they will still have the same data even if the old array elements were modified).

For example:

>>>import numpy as np
>>>a = np.array([1,2,3,4])
>>>c = a.view()
>>>c
array([1,2,3,4])

c is a shallow copy of a (they reference to the same data but not to the same object):

>>>a is c
False

However, if we tested c.base to a it give a True:

>>>c.base is a
True

knowing that both are ndarry objects:

>>>type(a)
<class 'numpy.ndarray'>
>>> type(c)
<class 'numpy.ndarray'>

if we tested the type of both a and c we find this:

>>> type(a.base)
<class 'NoneType'>
>>> type(c.base)
<class 'numpy.ndarray'>

My questions are:
-What is .base attribute exactly (is it the data part in ndarray object)?
-Why a and c have different types of their .base?

2

1 Answer 1

3

According to numpy doc:

array.base is base object if memory is from some other object.

If object owns its memory, base is None, but if an object is sharing memory with another one (for example is a view of it), the base would be the owner object of that memory. In your example, c.base is the object that owns the memory, which would be a.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! so flags.owndata will be just a : return isinstance(c.base,NoneType) if there was a Nonetype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.