# NumPy Array Attributes

NumPy is a library in Python that provides a high-performance multidimensional array objects, along with that it provides the tools for working with these arrays. An array is a central data structure of the NumPy library, and it is used for representing vectors, matrices, and higher-dimensional datasets.

## Introduction

To begin using NumPy, you first need to import the library. Once you have imported NumPy, you can start creating arrays. Here’s how you can create a simple one-dimensional (1D) array like this,

```import numpy as np

# Creating a one-dimensional NumPy array
arr = np.array([11, 12, 13, 14])

print(arr)
```

Output:

```[11 12 13 14]
```

Similarly, you can create a two-dimensional (2D) array or a three-dimensional (3D) array by passing a list of lists or a list of lists of lists, respectively, to the `np.array` function.

## Exploring NumPy Array Attributes

### NumPy Array Attribute – shape

• Shape: This attribute returns a tuple with each index having the number of corresponding elements in a dimension. For example, for a 1D Array, it will give a tuple with one value only i.e.
```import numpy as np

# Creating a one-dimensional NumPy array
arr = np.array([11, 12, 13, 14])

print(arr.shape)
```

Output:

```(4,)
```

For a 2D NumPy Array shape attribute will return a tuple with 2 values. For example,

```import numpy as np

# Creating a 2D NumPy array
arr = np.array([[11, 12, 13, 14],
[11, 12, 13, 14],
[11, 12, 13, 14]])

print(arr.shape)
```

Output:

```(3, 4)
```

First value in tuple is 3 i.e. Number of rows in the NumPy Array. Whereas, second value in NumPy Array is 4 i.e. number of columns in NumPy Array

### NumPy Array Attribute – size

• Size: This gives you the total number of elements in the array.
```import numpy as np

# Creating a one-dimensional NumPy array
arr = np.array([11, 12, 13, 14])

print(arr.size)
```

Output:

```4
```

For 2D NumPy Array:

```import numpy as np

# Creating a 2D NumPy array
arr = np.array([[11, 12, 13, 14],
[11, 12, 13, 14],
[11, 12, 13, 14]])

print(arr.size)
```

Output:

```12
```

For 2D NumPy Array it will return total numbers of elements in array including all rows and columns.

### NumPy Array Attribute – dtype

• Data type (dtype): It tells you the data type of the elements in the array.
```import numpy as np

# Creating a one-dimensional NumPy array
arr = np.array([11, 12, 13, 14])

print(arr.dtype)
```

Output:

```int64
```

Example 2:

Here, in this example NumPy Array contains float values, so the dtype will give `float64`

```import numpy as np

# Creating a one-dimensional NumPy array
arr = np.array([11, 12.1, 13.2, 14])

print(arr.dtype)
```

Output:

```float64
```

### NumPy Array Attribute – ndim

• ndim: This stands for the number of dimensions of the array.

For a 2D NumPy Array it will return 2 and for a 3D NumPy Array it will return 3. For example,

```import numpy as np

# Creating a 2D NumPy array
arr = np.array([[11, 12, 13, 14],
[11, 12, 13, 14],
[11, 12, 13, 14]])

print(arr.ndim)
```

Output:

```2
```

## Working with Different Array Dimensions

You can create arrays with various dimensions as needed:

• For a 1D array, it represents a simple list or vector of values.
• A 2D array is akin to a matrix or a list of lists.
• A 3D array can represent a volume or a list of matrices.

Here’s an example of creating and accessing the attributes of a 2D array, which we dicussed above:

```import numpy as np

# Creating a 2D NumPy array
arr = np.array([[11, 12, 13, 14],
[11, 12, 13, 14],
[11, 12, 13, 14]])

print(arr)

print('Shape:', arr.shape)  # Output: Shape: (3, 4)
print('Size:', arr.size)    # Output: Size: 12
print('Data type:', arr.dtype)  # Output: Data type: int64
print('Dimensions:', arr.ndim)  # Output: Dimensions: 2
```

Output:

```[[11 12 13 14]
[11 12 13 14]
[11 12 13 14]]
Shape: (3, 4)
Size: 12
Data type: int64
Dimensions: 2
```

In the above example, we used different attributes of NumPy Array on a 2D Array.

## Summary

Understanding how to manipulate NumPy Arrays is useful for tasks in machine learning, where you often deal with large and multidimensional datasets.

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

Scroll to Top