How does NumPy ndarrary.view(…) work internallyWhat are the advantages of NumPy over regular Python lists?How can the Euclidean distance be calculated with NumPy?How to print the full NumPy array, without truncation?Dump a NumPy array into a csv fileHow do I get indices of N maximum values in a NumPy array?Simple Digit Recognition OCR in OpenCV-Pythonnumpy np.array versus np.matrix (performance)What does -1 mean in numpy reshape?What is the purpose of meshgrid in Python / NumPy?How to work “if np.array([False]):”
Why do guitarists wave their guitars?
Will TSA allow me to carry a Continuous Positive Airway Pressure (CPAP) device?
Working in the USA for living expenses only; allowed on VWP?
I wrote a scene that the majority of my readers loved. How do I get back to that place while writing my new book?
What does left arrow <- mean outside a do block?
What's the most polite way to tell a manager "shut up and let me work"?
Traffic law UK, pedestrians
Does the growth of home value benefit from compound interest?
What are the words for people who cause trouble believing they know better?
Old black and white movie: glowing black rocks slowly turn you into stone upon touch
Is the decompression of compressed and encrypted data without decryption also theoretically impossible?
How bad would a partial hash leak be, realistically?
what is triplication on fpga?
Linux tr to convert vertical text to horizontal
Credit card offering 0.5 miles for every cent rounded up. Too good to be true?
Vector-valued ParametricNDSolve, solving for a combination
What is the right way to float a home lab?
Accidentally renamed tar.gz file to a non tar.gz file, will my file be messed up
X-shaped crossword
Why were the Night's Watch required to be celibate?
PhD student with mental health issues and bad performance
What's the correct term for a waitress in the Middle Ages?
How to decline physical affection from a child whose parents are pressuring them?
How were concentration and extermination camp guards recruited?
How does NumPy ndarrary.view(…) work internally
What are the advantages of NumPy over regular Python lists?How can the Euclidean distance be calculated with NumPy?How to print the full NumPy array, without truncation?Dump a NumPy array into a csv fileHow do I get indices of N maximum values in a NumPy array?Simple Digit Recognition OCR in OpenCV-Pythonnumpy np.array versus np.matrix (performance)What does -1 mean in numpy reshape?What is the purpose of meshgrid in Python / NumPy?How to work “if np.array([False]):”
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I've used the following code to create a view but i want to understand how this view works under the hood,
>>> x = np.array([(1, 2)], dtype=np.int8)
>>> y = x.view(dtype=np.int16)
where to find the source code for ndarray.view(...), I searched through the GitHub code repository but couldn't find it
numpy numpy-ndarray
add a comment |
I've used the following code to create a view but i want to understand how this view works under the hood,
>>> x = np.array([(1, 2)], dtype=np.int8)
>>> y = x.view(dtype=np.int16)
where to find the source code for ndarray.view(...), I searched through the GitHub code repository but couldn't find it
numpy numpy-ndarray
1
github.com/numpy/numpy/blob/…
– Brad Solomon
Mar 24 at 14:28
add a comment |
I've used the following code to create a view but i want to understand how this view works under the hood,
>>> x = np.array([(1, 2)], dtype=np.int8)
>>> y = x.view(dtype=np.int16)
where to find the source code for ndarray.view(...), I searched through the GitHub code repository but couldn't find it
numpy numpy-ndarray
I've used the following code to create a view but i want to understand how this view works under the hood,
>>> x = np.array([(1, 2)], dtype=np.int8)
>>> y = x.view(dtype=np.int16)
where to find the source code for ndarray.view(...), I searched through the GitHub code repository but couldn't find it
numpy numpy-ndarray
numpy numpy-ndarray
edited Mar 24 at 14:36
Brad Solomon
15.9k84198
15.9k84198
asked Mar 24 at 13:09
ManoranjanManoranjan
14910
14910
1
github.com/numpy/numpy/blob/…
– Brad Solomon
Mar 24 at 14:28
add a comment |
1
github.com/numpy/numpy/blob/…
– Brad Solomon
Mar 24 at 14:28
1
1
github.com/numpy/numpy/blob/…
– Brad Solomon
Mar 24 at 14:28
github.com/numpy/numpy/blob/…
– Brad Solomon
Mar 24 at 14:28
add a comment |
1 Answer
1
active
oldest
votes
What are you most interested in - the coding mechanics, or how int8 values are repsented as int16?
view creates a new array, with its own shape and dtype, but sharing the data buffer with the source. Most of the code that you'll see has to do with creating that new array, with little to nothing about the specific dtypes.
There are actually two versions of int16, big-ended and little.
In [194]: np.array([(1,2)],np.int8).view('<i2')
Out[194]: array([[513]], dtype=int16)
In [195]: np.array([(1,2)],np.int8).view('>i2')
Out[195]: array([[258]], dtype=int16)
np.int8 is a single byte which can represent values up to 256. The values we see depend on how the 2 bytes are combined into 1 number.
In [197]: 2*256+1
Out[197]: 513
In [198]: 1*256+2
Out[198]: 258
My guess is that you won't see this level of detail in the numpy C code. It's performed by the C compiler.
Hi @hpaulj, thanks for the reply, basically i am trying to understand the mechanics behind numpy view(...) in general. just like array broadcasting is powered by multi iterators behind the scene, so is this view(...) also backed-up by some iterators or something else, I wanted to see some piece of source code to understand it or some blogs/forums that talks about the internals of how view(...) works under the cover.
– Manoranjan
Mar 24 at 18:40
how to get the view that can result the array having alternate elements from the original array, for ex:- array1 = np.array([1,2,3,4,5]) and array2 = array1.view() this gives me all the elements from the original array array1, but how to create a view having only alternate items form the original array, like [1,3,5], is there anyway to achieve using just numpy view(...) ??
– Manoranjan
Mar 24 at 18:44
1
Theviewmethod isn't the only way to create aview:) Basic indexing, ie. with a slice, also creates a view:arr[::2]. The view method, as shown in its docs, has two purposes, changingdtypeand changing subclass,type.
– hpaulj
Mar 24 at 19:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324126%2fhow-does-numpy-ndarrary-view-work-internally%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
What are you most interested in - the coding mechanics, or how int8 values are repsented as int16?
view creates a new array, with its own shape and dtype, but sharing the data buffer with the source. Most of the code that you'll see has to do with creating that new array, with little to nothing about the specific dtypes.
There are actually two versions of int16, big-ended and little.
In [194]: np.array([(1,2)],np.int8).view('<i2')
Out[194]: array([[513]], dtype=int16)
In [195]: np.array([(1,2)],np.int8).view('>i2')
Out[195]: array([[258]], dtype=int16)
np.int8 is a single byte which can represent values up to 256. The values we see depend on how the 2 bytes are combined into 1 number.
In [197]: 2*256+1
Out[197]: 513
In [198]: 1*256+2
Out[198]: 258
My guess is that you won't see this level of detail in the numpy C code. It's performed by the C compiler.
Hi @hpaulj, thanks for the reply, basically i am trying to understand the mechanics behind numpy view(...) in general. just like array broadcasting is powered by multi iterators behind the scene, so is this view(...) also backed-up by some iterators or something else, I wanted to see some piece of source code to understand it or some blogs/forums that talks about the internals of how view(...) works under the cover.
– Manoranjan
Mar 24 at 18:40
how to get the view that can result the array having alternate elements from the original array, for ex:- array1 = np.array([1,2,3,4,5]) and array2 = array1.view() this gives me all the elements from the original array array1, but how to create a view having only alternate items form the original array, like [1,3,5], is there anyway to achieve using just numpy view(...) ??
– Manoranjan
Mar 24 at 18:44
1
Theviewmethod isn't the only way to create aview:) Basic indexing, ie. with a slice, also creates a view:arr[::2]. The view method, as shown in its docs, has two purposes, changingdtypeand changing subclass,type.
– hpaulj
Mar 24 at 19:58
add a comment |
What are you most interested in - the coding mechanics, or how int8 values are repsented as int16?
view creates a new array, with its own shape and dtype, but sharing the data buffer with the source. Most of the code that you'll see has to do with creating that new array, with little to nothing about the specific dtypes.
There are actually two versions of int16, big-ended and little.
In [194]: np.array([(1,2)],np.int8).view('<i2')
Out[194]: array([[513]], dtype=int16)
In [195]: np.array([(1,2)],np.int8).view('>i2')
Out[195]: array([[258]], dtype=int16)
np.int8 is a single byte which can represent values up to 256. The values we see depend on how the 2 bytes are combined into 1 number.
In [197]: 2*256+1
Out[197]: 513
In [198]: 1*256+2
Out[198]: 258
My guess is that you won't see this level of detail in the numpy C code. It's performed by the C compiler.
Hi @hpaulj, thanks for the reply, basically i am trying to understand the mechanics behind numpy view(...) in general. just like array broadcasting is powered by multi iterators behind the scene, so is this view(...) also backed-up by some iterators or something else, I wanted to see some piece of source code to understand it or some blogs/forums that talks about the internals of how view(...) works under the cover.
– Manoranjan
Mar 24 at 18:40
how to get the view that can result the array having alternate elements from the original array, for ex:- array1 = np.array([1,2,3,4,5]) and array2 = array1.view() this gives me all the elements from the original array array1, but how to create a view having only alternate items form the original array, like [1,3,5], is there anyway to achieve using just numpy view(...) ??
– Manoranjan
Mar 24 at 18:44
1
Theviewmethod isn't the only way to create aview:) Basic indexing, ie. with a slice, also creates a view:arr[::2]. The view method, as shown in its docs, has two purposes, changingdtypeand changing subclass,type.
– hpaulj
Mar 24 at 19:58
add a comment |
What are you most interested in - the coding mechanics, or how int8 values are repsented as int16?
view creates a new array, with its own shape and dtype, but sharing the data buffer with the source. Most of the code that you'll see has to do with creating that new array, with little to nothing about the specific dtypes.
There are actually two versions of int16, big-ended and little.
In [194]: np.array([(1,2)],np.int8).view('<i2')
Out[194]: array([[513]], dtype=int16)
In [195]: np.array([(1,2)],np.int8).view('>i2')
Out[195]: array([[258]], dtype=int16)
np.int8 is a single byte which can represent values up to 256. The values we see depend on how the 2 bytes are combined into 1 number.
In [197]: 2*256+1
Out[197]: 513
In [198]: 1*256+2
Out[198]: 258
My guess is that you won't see this level of detail in the numpy C code. It's performed by the C compiler.
What are you most interested in - the coding mechanics, or how int8 values are repsented as int16?
view creates a new array, with its own shape and dtype, but sharing the data buffer with the source. Most of the code that you'll see has to do with creating that new array, with little to nothing about the specific dtypes.
There are actually two versions of int16, big-ended and little.
In [194]: np.array([(1,2)],np.int8).view('<i2')
Out[194]: array([[513]], dtype=int16)
In [195]: np.array([(1,2)],np.int8).view('>i2')
Out[195]: array([[258]], dtype=int16)
np.int8 is a single byte which can represent values up to 256. The values we see depend on how the 2 bytes are combined into 1 number.
In [197]: 2*256+1
Out[197]: 513
In [198]: 1*256+2
Out[198]: 258
My guess is that you won't see this level of detail in the numpy C code. It's performed by the C compiler.
edited Mar 24 at 16:59
answered Mar 24 at 15:56
hpauljhpaulj
121k790166
121k790166
Hi @hpaulj, thanks for the reply, basically i am trying to understand the mechanics behind numpy view(...) in general. just like array broadcasting is powered by multi iterators behind the scene, so is this view(...) also backed-up by some iterators or something else, I wanted to see some piece of source code to understand it or some blogs/forums that talks about the internals of how view(...) works under the cover.
– Manoranjan
Mar 24 at 18:40
how to get the view that can result the array having alternate elements from the original array, for ex:- array1 = np.array([1,2,3,4,5]) and array2 = array1.view() this gives me all the elements from the original array array1, but how to create a view having only alternate items form the original array, like [1,3,5], is there anyway to achieve using just numpy view(...) ??
– Manoranjan
Mar 24 at 18:44
1
Theviewmethod isn't the only way to create aview:) Basic indexing, ie. with a slice, also creates a view:arr[::2]. The view method, as shown in its docs, has two purposes, changingdtypeand changing subclass,type.
– hpaulj
Mar 24 at 19:58
add a comment |
Hi @hpaulj, thanks for the reply, basically i am trying to understand the mechanics behind numpy view(...) in general. just like array broadcasting is powered by multi iterators behind the scene, so is this view(...) also backed-up by some iterators or something else, I wanted to see some piece of source code to understand it or some blogs/forums that talks about the internals of how view(...) works under the cover.
– Manoranjan
Mar 24 at 18:40
how to get the view that can result the array having alternate elements from the original array, for ex:- array1 = np.array([1,2,3,4,5]) and array2 = array1.view() this gives me all the elements from the original array array1, but how to create a view having only alternate items form the original array, like [1,3,5], is there anyway to achieve using just numpy view(...) ??
– Manoranjan
Mar 24 at 18:44
1
Theviewmethod isn't the only way to create aview:) Basic indexing, ie. with a slice, also creates a view:arr[::2]. The view method, as shown in its docs, has two purposes, changingdtypeand changing subclass,type.
– hpaulj
Mar 24 at 19:58
Hi @hpaulj, thanks for the reply, basically i am trying to understand the mechanics behind numpy view(...) in general. just like array broadcasting is powered by multi iterators behind the scene, so is this view(...) also backed-up by some iterators or something else, I wanted to see some piece of source code to understand it or some blogs/forums that talks about the internals of how view(...) works under the cover.
– Manoranjan
Mar 24 at 18:40
Hi @hpaulj, thanks for the reply, basically i am trying to understand the mechanics behind numpy view(...) in general. just like array broadcasting is powered by multi iterators behind the scene, so is this view(...) also backed-up by some iterators or something else, I wanted to see some piece of source code to understand it or some blogs/forums that talks about the internals of how view(...) works under the cover.
– Manoranjan
Mar 24 at 18:40
how to get the view that can result the array having alternate elements from the original array, for ex:- array1 = np.array([1,2,3,4,5]) and array2 = array1.view() this gives me all the elements from the original array array1, but how to create a view having only alternate items form the original array, like [1,3,5], is there anyway to achieve using just numpy view(...) ??
– Manoranjan
Mar 24 at 18:44
how to get the view that can result the array having alternate elements from the original array, for ex:- array1 = np.array([1,2,3,4,5]) and array2 = array1.view() this gives me all the elements from the original array array1, but how to create a view having only alternate items form the original array, like [1,3,5], is there anyway to achieve using just numpy view(...) ??
– Manoranjan
Mar 24 at 18:44
1
1
The
view method isn't the only way to create a view :) Basic indexing, ie. with a slice, also creates a view: arr[::2]. The view method, as shown in its docs, has two purposes, changing dtype and changing subclass, type.– hpaulj
Mar 24 at 19:58
The
view method isn't the only way to create a view :) Basic indexing, ie. with a slice, also creates a view: arr[::2]. The view method, as shown in its docs, has two purposes, changing dtype and changing subclass, type.– hpaulj
Mar 24 at 19:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324126%2fhow-does-numpy-ndarrary-view-work-internally%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
github.com/numpy/numpy/blob/…
– Brad Solomon
Mar 24 at 14:28