DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Data Analytics Using Python
  • Power BI Embedded Analytics — Part 1.1: Power BI Authoring Data Federation
  • Power BI Embedded Analytics — Part 1: Introduction and Power BI Authoring Overview
  • Essential Python Libraries: Introduction to NumPy and Pandas

Trending

  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • Power BI Embedded Analytics — Part 2: Power BI Embedded Overview
  • Performance Optimization Techniques for Snowflake on AWS
  1. DZone
  2. Data Engineering
  3. Data
  4. Python - 5 Sets of Useful Numpy Unary Functions - Data Analytics

Python - 5 Sets of Useful Numpy Unary Functions - Data Analytics

In this post, you will learn about some of the 5 most popular or useful set of unary universal functions (ufuncs) provided by Python Numpy library.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Sep. 15, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
4.3K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, you will learn about some of the 5 most popular or useful set of unary universal functions (ufuncs) provided by Python Numpy library. As data scientists, it will be useful to learn these unary functions by heart as it will help in performing arithmetic operations on sequential-like objects. These functions can also be termed as vectorized wrapper functions which are used to perform element-wise operations.

The following represents different set of popular functions:

  • Basic arithmetic operations
  • Summary statistics
  • Sorting
  • Minimum/maximum
  • Array equality

Basic Arithmetic Operations

The following are some of the unary functions whichc an be used to perform arithmetic operations:

  • add, subtract, multiply, divide, exp
  • add.reduce
  • sum

Here is the sample code demonstrating the usage of the above functions:

Java
 




x
13


 
1
import numpy as np
2
#
3
# Create an array of 5 numbers between 5 and 10
4
#
5
arr = np.linspace(5, 10, 5)
6
#
7
# Print array
8
#
9
print(arr)
10
#
11
# Perform arithmetic operations
12
#
13
np.add(arr, 1), np.subtract(arr, 1), np.multiply(arr, 2), np.divide(arr, 2)


Here is how the output would look like:


Fig 1. Numpy Unary Arithmetic Functions


In case, you want to add all the numbers in a row or column and get the output as matrix, functions such as add.reduce or sum is used with axis. In the code sample given below, a 2 x 5 matrix is reduced by rows (axis=1) and columns (axis=0)


Fig 2. Numpy Unary functions to sum rows and columns


Summary Statistics

The following are some of the methods which can be used for calculating summary statistics:

  • mean, median: Finds the mean and median of the data sample respectively.
  • std: Finds the standard deviation of the data
  • var: Finds the variance of the data

Here is the code demonstrating the usage of above functions with SKlearn IRIS dataset.

Java
 




xxxxxxxxxx
1


 
1
import numpy as np
2
from sklearn import datasets
3

           
4
iris = datasets.load_iris()
5

           
6
np.mean(iris.data[:,0]), np.std(iris.data[:,0]), np.var(iris.data[:,0]), np.median(iris.data[:,0])


Here is how the output will look like:


Fig 2. Numpy unary functions to fund summary statistics


Sorting

The following represents the Numpy unary functions which can be used for sorting the array:

  • sort: Sort the data
  • argsort: Finds the indices of the sorted data

Here is the code demonstrating the usage of above functions with SKlearn IRIS dataset:

Java
 




xxxxxxxxxx
1
10


 
1
import numpy as np
2
from sklearn import datasets
3

           
4
iris = datasets.load_iris()
5

           
6
iris.data[0:10, 0]
7

           
8
np.sort(iris.data[0:10, 0])
9

           
10
np.argsort(iris.data[0:10, 0])


Here is how the output will look like:


Fig 3. Numpy unary functions for sorting


Finding Maximum/Minimum

The following are some of the methods which can be used for finding maximum and minimum value from a data array:

  • min: Finds the minimum of the array
  • max: Finds the maximum of the array
  • argmin: Finds the index of the minimum of the array
  • argmax: Finds the index of the maximum of the array

Here is the code demonstrating the usage of above functions with SKlearn IRIS dataset:

Java
 




xxxxxxxxxx
1
10


 
1
import numpy as np
2
from sklearn import datasets
3

           
4
iris = datasets.load_iris()
5

           
6
iris.data[0:10, 0]
7

           
8
np.min(iris.data[0:10, 0]), np.argmin(iris.data[0:10, 0])
9

           
10
np.max(iris.data[0:10, 0]), np.argmax(iris.data[0:10, 0])


Here is how the output will look like:

Fig 4. Numpy unary functions for finding minimum and maximum
Data (computing) NumPy Python (language) Analytics

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Data Analytics Using Python
  • Power BI Embedded Analytics — Part 1.1: Power BI Authoring Data Federation
  • Power BI Embedded Analytics — Part 1: Introduction and Power BI Authoring Overview
  • Essential Python Libraries: Introduction to NumPy and Pandas

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!