-
Notifications
You must be signed in to change notification settings - Fork 4.3k
added nbytes.md #7836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
added nbytes.md #7836
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
73f61f2
added nbytes.md
Ananya44444 e7a8293
Merge branch 'main' into documentation
Ananya44444 fa10ee1
Revise .nbytes documentation for clarity and detail
mamtawardhani 6c5a766
Enhance `.nbytes` description with documentation link
mamtawardhani 21b7ac6
Apply suggestion from @avdhoottt
avdhoottt 1a72706
Apply suggestion from @avdhoottt
avdhoottt 1b38112
Apply suggestion from @avdhoottt
avdhoottt 4a375e6
Merge branch 'main' into documentation
avdhoottt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| --- | ||
| Title: '.nbytes' | ||
| Description: 'Returns the total number of bytes consumed by the elements of the array.' | ||
| Subjects: | ||
| - 'Code Foundations' | ||
| - 'Computer Science' | ||
| Tags: | ||
| - 'Arrays' | ||
| - 'Attributes' | ||
| - 'Memory' | ||
| - 'NumPy' | ||
| CatalogContent: | ||
| - 'learn-python-3' | ||
| - 'paths/computer-science' | ||
| --- | ||
|
|
||
| The **`.nbytes`** attribute returns the total number of bytes consumed by the elements of a [NumPy array](https://www.codecademy.com/resources/docs/numpy/ndarray). This value is calculated as the product of the number of elements in the array (given by `.size`) and the number of bytes per element (given by `.itemsize`). | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```pseudo | ||
| ndarray.nbytes | ||
| ``` | ||
|
|
||
| **Parameters:** | ||
|
|
||
| The `.nbytes` attribute takes no parameters. | ||
|
|
||
| **Return value:** | ||
|
|
||
| Returns an integer representing the total number of bytes consumed by the array elements. | ||
|
|
||
| ## Example | ||
|
|
||
| The following example creates a one-dimensional NumPy array `arr` with 12 elements. The `.nbytes` attribute reports the total bytes used by all array elements. On a 64-bit system where the default integer type (`int64`) uses 8 bytes per element, | ||
|
|
||
| $12 \text{ elements} \times 8 \text{ bytes}/\text{element} = 96 \text{ bytes}$: | ||
avdhoottt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```py | ||
| # Import NumPy | ||
| import numpy as np | ||
|
|
||
| # Create a NumPy array with 12 elements (default type is usually int64, or 8 bytes per item) | ||
| arr = np.arange(12) | ||
|
|
||
| # Use the '.nbytes' attribute | ||
| total_bytes_nbytes = arr.nbytes | ||
|
|
||
| print(f"Array: {arr}") | ||
| print(f"Bytes per element (.itemsize): {arr.itemsize}") | ||
| print(f"Total number of elements (.size): {arr.size}") | ||
| print(f"Total bytes consumed (.nbytes): {total_bytes_nbytes}") | ||
| ``` | ||
|
|
||
| The result will be similar to the following (the value of `arr.itemsize` might vary based on system architecture): | ||
|
|
||
| ```shell | ||
| Array: [ 0 1 2 3 4 5 6 7 8 9 10 11] | ||
| Bytes per element (.itemsize): 8 | ||
| Total number of elements (.size): 12 | ||
| Total bytes consumed (.nbytes): 96 | ||
| ``` | ||
|
|
||
| ## Codebyte Example | ||
|
|
||
| The example below demonstrates a two-dimensional NumPy array `arr` with a specified data type (`float32`). Since `float32` uses 4 bytes per element and the array contains $2 \times 3 = 6$ elements, the total memory consumed is $6 \times 4 = 24$ bytes: | ||
|
|
||
| ```codebyte/python | ||
| import numpy as np | ||
|
|
||
| # Create a 2x3 array of type float32 (4 bytes per element) | ||
| arr = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32) | ||
|
|
||
| print(f"Array shape: {arr.shape}") | ||
| print(f"Array data type: {arr.dtype}") | ||
| print(f"Bytes per element (.itemsize): {arr.itemsize}") | ||
| print(f"Bytes consumed by elements (.nbytes): {arr.nbytes}") | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.