0% found this document useful (0 votes)
13 views

NumPy_Array_Attributes

Uploaded by

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

NumPy_Array_Attributes

Uploaded by

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

Numpy Array Attributes

Attributes of Numpy arrays or ndarrays are properties or characteristics


associated with these arrays that provide information about their structure
and content. These attributes offer insights into the array's dimensions, data
type, size, and memory usage etc.
Following are the main attributes of ndArrays:

1. ndarray.shape
2. ndarray.ndim
3. numpy.itemsize
4. numpy.flags

1. ndarray.shape: This array property provides a tuple containing the


dimensions of the array and can also be utilized for resizing the array.

Example Code 1:

import numpy as np
a = np.array([[4,5,6],[7,8,9]])
print(a.shape)

Output
(2,3)

The code creates a 2x3 array named a, and prints its shape, which is (2,
3), indicating 2 rows and 3 columns.

Example Code 2:

# this resizes the ndarray


import numpy as np

a = np.array([[4,5,6],[7,8,9]])
a.shape = (3,2)
print(a)

Output
[[4 5]
[6 7]
[8 9]]

1|Page
This code snippet creates a 2x3 array a, then resizes it to a 3x2 array
using the shape property. Finally, it prints the resized array.

NumPy offers a reshape function for resizing arrays as well.

Example Code:

import numpy as np
a = np.array([[4,5,6],[7,8,9]])
b = a.reshape(3,2)
print(b)

Output
[[4 5]
[6 7]
[8 9]]

The code creates a NumPy array a with shape (2,3), then reshapes it to
a new array b with shape (3,2), and finally prints the reshaped array b.

2. ndarray.ndim: This array attributes provides the number of dimensions


(axes) in the array.

Example Code:

import numpy as np

# Create a 1-dimensional array


arr_1d = np.array([1, 2, 3])
print(arr_1d.ndim)

# Create a 3-dimensional array


arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d.ndim)

Output
1
3

In the code arr_1d is a 1-dimensional array, and arr_3d is a 3-


dimensional array. The ndim attribute is used to query the number of
dimensions of each array.

2|Page
3. numpy.itemsize: This array attributes provides the size of each element
in the array, measured in bytes.

# dtype of array is float32 (4 bytes)


import numpy as np
x = np.array([1,2,3,4,5], dtype = np.float32)
print(x.itemsize)

Output
4

Array x contains 32-bit floating-point numbers, so each element


occupies 4 bytes.

4. numpy.flags: This function returns the current values of the attributes of


the ndarray object.

Sr.No. Attribute & Description

C_CONTIGUOUS (C) - The data is organized in a single, C-style


1
contiguous segment.

F_CONTIGUOUS (F) - The data is organized in a single, Fortran-


2
style contiguous segment.

OWNDATA (O) - The array either owns the memory it uses or


3
borrows it from another object.

WRITEABLE (W) - The data area is writable; setting to False locks


4
the data, making it read-only.

ALIGNED (A) - Both data and elements are appropriately aligned


5
for the hardware.
UPDATEIFCOPY (U) - This array serves as a copy of another array;
6
deallocation updates the base array with its contents.

3|Page
Example Code:

import numpy as np
x = np.array([3,4,5,6,7])
print(x.flags)

Output
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False

The code creates a NumPy array 'x' with elements [3,4,5,6,7] and prints
its flags indicating memory layout and properties.

4|Page

You might also like