Write a python program by using NumPy module
Description
The provided Python code demonstrates the use of the NumPy library for sorting operations within arrays. It imports the NumPy library, creates a 3x3 array named 'a' filled with integer values, and showcases two sorting operations: column-wise sorting using 'np.sort(a, axis=0)' and row-wise sorting using 'np.sort(a, axis=1).'
Code
program11a.py
import numpy as np
a = np.array([[10, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Sorting along the columns")
print(np.sort(a, axis=0))
print("Sorting along the rows")
print(np.sort(a, axis=1))
datatype = np.dtype([('name', 'S10'), ('mark', int)])
array = np.array([('mlbp', 315), ('bet', 100)], dtype=datatype)
print("Sorting the structured array by the 'name' field")
print(np.sort(array, order='name'))
Explanation of above code
- The code initiates by importing the NumPy library, a fundamental tool for numerical and array operations in Python, with the standard alias
np.
- It proceeds to create a 3x3 NumPy array named
a,
filled with integer values. This array is utilized for sorting demonstrations. - The first sorting operation focuses on sorting the elements along the columns (axis 0) using the
np.sort(a, axis=0)
function. This function sortsa
along the specified axis and returns a sorted array. The sorted array is then displayed, demonstrating column-wise sorting. - The second sorting operation similarly employs the
np.sort
function to sort the elements along the rows (axis 1) usingnp.sort(a, axis=1).
The resultant sorted array is showcased, illustrating the sorting along rows. - The code takes a significant step by defining a structured data type using the
np.dtype()
function. This structured data type, termeddatatype,
defines two fields:name,
characterized by a string data type with a maximum length of 10 characters (S10
), andmark,
using an integer data type. - Subsequently, a structured NumPy array, named
array,
is instantiated to contain two records. Each record holds values forname
andmark.
- The code demonstrates structured array sorting by sorting
array
based on thename
field usingnp.sort(array, order=
name).
This command sortsarray
based on the specified field and prints the sorted structured array. - In summary, this code exemplifies the application of NumPy for sorting elements within arrays, including both column-wise and row-wise sorting. Additionally, it demonstrates structured array sorting based on specific fields. NumPy is an essential library for numerical and data manipulation tasks, extensively used in scientific and data analysis domains.