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

  • Basic Python Knowledge (Variables, Data Types, Syntax)
  • Familiarity with the Basics of the NumPy Library (Role in Numerical Computing, Basic Array Operations)

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:

  • Imports the NumPy library with the alias ‘np’ for easier usage
  • Creates a 2D NumPy array ‘myar’ with shape (3, 4) and integer values
  • Prints the original 2D array
  • Reshapes the 2D array into a 1D array ‘myar1’ with 12 elements
  • Prints the reshaped 1D array
  • Creates a new NumPy array ‘myar’ with floating-point values
  • Creates another NumPy array ‘myar’ with integer values
  • Prints the integer array and its data type
  • Creates an integer NumPy array ‘myar’ with 12 elements
  • Reshapes the 1D array into a 2D array ‘myar1’ with shape (3, 4)
  • Prints the reshaped 2D array
  • Prints size, item size, shape and number of dimensions of the reshaped array

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.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *