Site icon DataFlair

Python Program on NumPy Mathematical Function

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

In this article, we will explore a Python program that showcases various functions provided by the NumPy module for array manipulation. NumPy is a powerful library for numerical and mathematical operations in Python, and it offers a wide range of functions to efficiently handle arrays. The program focuses on array sorting using NumPy’s sorting functions and demonstrates the usage of sort() and max().

Prerequisites

Topic Explanation

This program commences by importing the NumPy library as ‘np’ and crafting an array. It proceeds to offer an instructive exploration of diverse techniques for sorting arrays within the NumPy framework, encompassing the use of ‘np.sort()’ and the ‘sort()’ method.

Furthermore, the program delves into the task of identifying the maximum value within an array, demonstrating the application of the ‘max()’ function. By elucidating these array manipulation methods, readers acquire practical proficiency in efficiently handling and analyzing data using NumPy arrays, crucial for diverse applications in data science, analytics, and numerical computing.

Code:

# Importing the numpy module
import numpy as np

# Creating a NumPy array named 'myar' with some random values
myar = np.array([90, 60, 12, 90, 34, 6])

# Sorting the 'myar' array and assigning the sorted array to 'newar'
newar = np.sort(myar)

# Sorting the 'myar' array in-place using the sort() method
myar.sort()

# Printing the maximum value of the sorted 'myar' array
print(myar.max())

# Printing the original 'myar' array after sorting it in-place
print("Old array ", myar)

# Printing the 'newar' array, which is a sorted copy of 'myar'
print("New array ", newar)

Output:

90
Old array [ 6 12 34 60 90 90]
New array [ 6 12 34 60 90 90]

Code Explanation:

Summary

In summary, this Python program serves as an invaluable resource for readers seeking proficiency in leveraging NumPy’s array manipulation capabilities. By providing practical insights into the utilization of NumPy functions for sorting arrays and determining maximum values, it empowers individuals to enhance their data manipulation and analysis skills.

These functions play a pivotal role in streamlining numerical computing tasks, making them an indispensable tool in fields such as data science, engineering, and scientific research, where efficient data handling is essential for achieving meaningful insights and solutions.

Exit mobile version