Site icon DataFlair

Python Program on Array Attributes

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In this article, we will explore a Python program that delves into the attributes of arrays using the NumPy library. NumPy is a powerful library for numerical and mathematical operations in Python, and it provides an array object that supports various attributes for efficient array manipulation. The program demonstrates different attributes such as shape, size, data type, and dimensionality, providing insights into the structure and characteristics of arrays in NumPy.

Prerequisites

Topic Explanation

This program initiates by importing the NumPy library as ‘np’ and constructing arrays with diverse shapes and data types. It proceeds to illustrate several essential attributes of NumPy arrays, encompassing ‘reshape()’ for modifying array shape, ‘dtype’ for specifying data types, ‘size’ for element count, ‘itemsize’ for byte size per element, ‘shape’ for array dimensions, and ‘ndim’ for the number of dimensions.

By demonstrating these attributes, the program offers a practical exploration of NumPy’s versatility and its role in efficient data manipulation. Readers gain insights into key functionalities for working with arrays, enhancing their comprehension of NumPy as a fundamental library for numerical computing and data analysis in Python.

Code:

# Importing the NumPy library with the alias 'np'
import numpy as np

# Creating a 2D NumPy array 'myar' with shape (3, 4)
myar = np.array([[1, 2, 3, 1], [4, 5, 6, 2], [7, 8, 9, 5]])

# Printing the original array
print("Original Array:")
print(myar)

# Reshaping the array to a 1D array 'myar1' with 12 elements
myar1 = myar.reshape(12)

# Printing the reshaped array
print("\nReshaped Array:")
print(myar1)

# Creating a NumPy array 'myar' with floating-point values
myar = np.array([1.2, 2.4, 3.5, 4.5, 5.7, 6.8, 7.9, 8.3, 9.6, 10.7])

# Creating another NumPy array 'myar' with integer values
myar = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

# Printing the array and its data type
print("\nArray with Integer Values:")
print(myar)
print("Data type of array:", myar.dtype)

# Creating a NumPy array 'myar' with integer values
myar = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

# Reshaping the array to a 2D array 'myar1' with shape (3, 4)
myar1 = myar.reshape(3, 4)

# Printing the reshaped array
print("\nReshaped 2D Array:")
print(myar1)

# Printing the size, item size, shape, and number of dimensions of the array
print("\nArray Information:")
print("Size of array:", myar.size)
print("Item Size of array:", myar.itemsize)
print("Shape of array:", myar.shape)
print("Number of dimensions:", myar.ndim)

Output:

Original Array:
[[1 2 3 1]
[4 5 6 2]
[7 8 9 5]]

Reshaped Array:
[1 2 3 1 4 5 6 2 7 8 9 5]

Array with Integer Values:
[1 2 3 4 5 6 7 8 9]
Data type of array: int64

Reshaped 2D Array:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

Array Information:
Size of array: 12
Item Size of array: 8
Shape of array: (12,)
Number of dimensions: 1

Code Explanantion:

Summary

In summary, this Python program serves as an invaluable resource for readers seeking to delve into the intricacies of array attributes using the powerful NumPy library. It not only offers practical guidance but also illuminates the significance of understanding array structure and characteristics. These insights form the bedrock for proficient numerical computing, data analysis, and scientific research in Python. As readers grasp the capabilities of NumPy arrays, they gain a robust foundation for tackling complex data-centric challenges across a myriad of domains, from machine learning to scientific simulations.

Exit mobile version