AppDividend
Latest Code Tutorials

# NumPy Array Attributes Example

NumPy array has attributes like what is the shape of the array, what is the dimension of the array. It has attribute that modify the array size. It resizes the array. We can also find the bytes of the items  NumPy provides a high-performance multidimensional array and necessary tools to calculate with and manipulate these arrays.

SciPy builds on this and offers a vast number of methods that operate on numpy arrays and that re useful for different types of scientific and engineering applications.

## NumPy Array Attributes Example

Content Overview

If we need to know what is the shape of the numpy array, then we can use the ndarray.shape() attribute.

The shape array attribute returns the tuple consisting of array dimensions.

It can also be used to resize the array. Let’s see the example in Python Jupyter Notebook.

Write the following code inside the cell to import the NumPy library.

`import numpy as np`

Then we need to create a NumPy array from Python List and output its shape. See the below code.

```app_list = [18, 0, 21, 30, 46]
np_app_list = np.array(app_list)
np_app_list.shape```

Here, we have defined one list and then convert that list into the numpy array and then figure out print it’s shape.

See the below output. You can run the cell using Ctrl + Enter key.

The above-defined array is a single dimension array. We can define the multidimensional array using the following code.

```app_list = [[18, 0, 21], [30, 46, 21], [19, 21, 18]]
np_app_list = np.array(app_list)
np_app_list.shape```

Here, we have defined the 3*3 dimensional Array.

See the output.

## NumPy Shape To Resize Array

In the following example, we will see how we can use the NumPy shape attribute to resize the array.

```app_list = [[18, 0, 21], [30, 46, 21]]
np_app_list = np.array(app_list)
np_app_list.shape = (3, 2)
np_app_list```

Now, we have defined a 2*3 array, and now we have changed its shape to the 3*2 array.

See the output below.

So, we have used the shape attribute to change the shape of the array which means it will turn rows into columns and columns into rows.

## numpy.itemsize

The itemsize attribute returns the length of each element of an array in bytes. See the below example.

```np_app_list = np.array([18, 0, 21], dtype=np.int8)
np_app_list.itemsize```

Here, when we are creating a numpy array, we have passed the second argument which is dtype which means the items datatype, and it is int8. That means the bytes of each character is 1.

See the below output.

If we take float32, then it will return the in the output.

## numpy.ndim

The ndim attribute returns the dimension of a numpy array. See the following example.

```npdata = np.array([[21, 19], [18, 21]])
npdata.ndim```

See the output.

Finally, NumPy Array Attributes Tutorial With Example is over.

This site uses Akismet to reduce spam. Learn how your comment data is processed.