保留正文与补采附件。源页面当前未重新核验;具体限制见交付清单。

Revisit consent button We value your privacy We use cookies to enhance your browsing experience, serve personalised ads or content, and analyse our traffic. By clicking "Accept All", you consent to our use of cookies. Customise Reject All Accept All Customise Consent Preferences We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below. The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... Show more NecessaryAlways Active Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

Functional Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

Analytics Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

Performance Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Advertisement Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

Uncategorised Other uncategorised cookies are those that are being analysed and have not been classified into a category as yet. No cookies to display. Reject All Save My Preferences Accept All Toggle SearchToggle Mobile Navigation Publish AI, ML & data-science insights to a global community of data professionals. Sign inSubmit an Article LatestEditor's PicksNewsletterShipAI


Write For TDS LinkedInXToggle Search Data Science

Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

NumPy tricks you didn’t know you needed (until now) Ibrahim Salami October 21, 20259 min read I’ve been learning data analytics for a year now. So far, I can consider myself confident in SQL and Power BI. The transition to Python has been quite exciting. I’ve been exposed to some neat and smarter approaches to data analysis. After brushing up on my skills on the Python fundamentals, the ideal next step was to start learning about some of the Python libraries for data analysis. NumPy is one of them. Being a math nerd, naturally, I’d enjoy exploring this Python library. This library is designed for individuals who want to perform mathematical computations using Python, from basic mathematics and algebra to advanced concepts like calculus. NumPy can pretty much do it all. In this article, I wanted to introduce you to some NumPy functions I’ve been playing around with. Whether you’re a data scientist, financial analyst, or research nerd, these functions would help you out a lot. Without further ado, let’s get to it. Learn this step by step with the interactive AI and Data Scientist roadmap.

Sample Dataset (used throughout)

Before diving in, I’ll define a small dataset that will anchor all examples: python

import numpy as np
temps = np.array([30, 32, 29, 35, 36, 33, 31])

Using this small temperature dataset, I’ll be sharing 7 functions that make array operations effortless.

1. np.where()— The Vectorized If-Else

Before I define what this function is, here’s a quick showcase of the function python

arr = np.array([10, 15, 20, 25, 30])
indices = np.where(arr > 20)
print(indices)

Output: (array([3, 4]),)

np.where is a condition-based function. When a condition is specified, it outputs the index/indices where that condition is true.
For instance, in the example above, an array is specified, and I have declared a np.where function that retrieves records where the array element is greater than 20. The output is array([3, 4]), because that’s the location/indices where that condition is true — that will be 25 and 30.

Conditional selection/replacement

It’s also useful when you’re trying to define a custom output for the outputs that meet your condition. This is used a lot in data analysis. For instance: python

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
result = np.where(arr % 2 == 0, ‘even’, ‘odd’)
print(result)

Output: [‘odd’ ‘even’ ‘odd’ ‘even’ ‘odd’] The example above tries to retrieve even numbers. After retrieving, a condition selection/replacement function is called that adds a custom name to our conditions. If the condition is true, it’s replaced by even, and if the condition is false, it’s replaced by odd.
Alright, let’s apply this to our small dataset. Problem: Replace all temperatures above 35°C with 35 (cap extreme readings). In real-world data, especially from sensors, weather stations, or user inputs, outliers are quite common — sudden spikes or unrealistic values that aren’t realistic. For example, a temperature sensor might momentarily glitch and record 42°C when the actual temperature was 35°C. Leaving such anomalies in your data can:

Let’s fix that python

adjusted = np.where(temps > 35, 35, temps)

Output: array([30, 32, 29, 35, 35, 33, 31]) Looks much better now. With just a few lines of code, we managed to fix unrealistic outliers in our dataset.

2. np.clip() — Keep Values in Range

In many practical datasets, values can spill outside the expected range, probably due to measurement noise, user error, or scaling mismatches. For instance:

These “out-of-bounds” values may seem minor, but they can:

np.clip() neatly solves this problem by constraining all elements of an array within a specified minimum and maximum range. It’s kinda like setting boundaries in your dataset. Example:
Problem: Ensure all readings stay within [28, 35] range. python

clipped = np.clip(temps, 28, 35)
clipped

Output: array([30, 32, 29, 35, 35, 33, 31]) Here’s what it does:

Sure, this could also be done with np.where() like so
temps = np.where(temps < 28, 28, np.where(temps > 35, 35, temps))
But I’d rather go with np.clip() cos it’s much cleaner and faster.

3. np.ptp() — Find Your Data’s Range in One Line

np.ptp() (peak-to-peak) basically shows you the difference between the maximum and minimum elements. It’s basically:
np.ptp(a) == np.max(a) — np.min(a) But in one clean, expressive function. Here’s how it works python

arr = np.array([[1, 5, 2],
[8, 3, 7]])
# Calculate peak-to-peak range of the entire array
range_all = np.ptp(arr)
print(f”Peak-to-peak range of the entire array: {range_all}”)

So that’ll be our maximum value (8) — minimum value(1)

Output: Peak-to-peak range of the entire array: 7 So why is this useful? Similar to averages, understanding how much your data varies is often as important. In weather data, for instance, it shows you how stable or volatile the conditions were. Instead of separately calling max() and min(), or manually subtracting. np.ptp() makes it concise, readable, and vectorised, especially useful when you’re computing ranges across multiple rows or columns. Now, let’s apply this to our dataset. Problem: How much did the temperature vary this week? python

temps = np.array([30, 32, 29, 35, 36, 33, 31])
np.ptp(temps)

Output: np.int64(7) This tells us the temperature fluctuated by 7°C over the period, from 29°C to 36°C.

4. np.diff() — Detect Daily Changes

np.diff() is the quickest way to measure momentum, growth, or decline across time. It basically calculates differences between elements in an array. To paint a picture for you, if your dataset were a journey, np.ptp() tells you how far you’ve travelled overall, while np.diff() tells you how far you moved between each stop. Essentially:
np.diff([a1, a2, a3, …]) = [a2 — a1, a3 — a2, …] Let’s apply this to our dataset. Let’s look at our temperature data again:

python

temps = np.array([30, 32, 29, 35, 36, 33, 31])
daily_change = np.diff(temps)
print(daily_change)

Output: [ 2 -3 6 1 -3 -2] In the real world, np.diff() is used for

5. np.gradient() — Capture Smooth Trends and Slopes

To be honest, when I first came across this, i found it hard to grasp. But essentially, np.gradient() computes the numerical gradient (a smooth estimate of change or slope) across your data. It’s similar to np.diff(), however, np.gradient() works even if your x-values are unevenly spaced (e.g., irregular timestamps). It provides a smoother signal, making trends easier to interpret visually. For instance: python

time = np.array([0, 1, 2, 4, 7])
temp = np.array([30, 32, 34, 35, 36])
np.gradient(temp, time)

Output: array([2. , 2. , 1.5 , 0.43333333, 0.33333333]) Let’s break this down a bit. Normally, np.gradient() assumes the x-values (your index positions) are evenly spaced — like 0, 1, 2, 3, 4, etc. But in the example above, the time array isn’t evenly spaced: notice the jumps are 1, 1, 2, 3. That means temperature readings weren’t taken every hour. By passing time as the second argument, we’re essentially telling NumPy to use the actual time gaps when calculating how fast temperature changes. To explain the output above. It’s saying between 0–2 hours, the temperature rose quickly (about 2°C per hour), and between 2–7 hours, the rise slowed down to around 0.3–1°C per hour. Let’s apply this to our dataset. Problem: Estimate the rate of temperature change (like slope). python

temps = np.array([30, 32, 29, 35, 36, 33, 31])
grad = np.gradient(temps)
np.round(grad, 2)

Output: array([ 2. , -0.5, 1.5, 3.5, -1. , -2.5, -2. ]) You can read it like:

So this tells a story of the week’s temperature. Let’s visualise this real quick with matplotlib. Notice how easy it is to interpret the visualisation. That’s what makes np.gradient() so useful.

6. np.percentile() – Spot Outliers or Thresholds

This is one of my favourite functions. np.percentile() helps you retrieve chunks or slices of your data. Numpy defines it well.

numpy.percentile computes the q-th percentile of data along a specified axis , where q is a percentage between 0 and 100. In np.percentile(), usually there’s a threshold to meet (which is 100%). You can then walk backwards and check the percentage of records that fall below this threshold. Let’s try this out with sales records Let’s say your monthly sales target is $60,000. You can use np.percentile() to understand how often and how strongly you’re hitting or missing that target. python

import numpy as np
sales = np.array([45, 50, 52, 48, 60, 62, 58, 70, 72, 66, 63, 80])
np.percentile(sales, [25, 50, 75, 90])

Output: [51.0 61.0 67.5 73.0] To break this down:

So now you can say:
“We hit or exceeded our $60k target in roughly half of all months.” This can also be visualised using a KPI card. That’s pretty powerful. That’s KPI storytelling with data. Let’s apply that to our temperature dataset. python

import numpy as np
temps = np.array([30, 32, 29, 35, 36, 33, 31])
np.percentile(temps, [25, 50, 75])

Output: [30.5 32. 34.5] Here’s what it means:

7. np.unique() — Quickly Find Unique Values and Their Counts

This function is perfect for cleaning, summarising, or categorising data. np.unique() finds all the unique elements in your array. It can also check how often those elements appear in your array. For instance, let’s say you have a list of product categories from your store: python

import numpy as np
products = np.array([
‘Shoes’, ‘Bags’, ‘Bags’, ‘Hats’,
‘Shoes’, ‘Shoes’, ‘Belts’, ‘Hats’
])
np.unique(products)

Output: array([‘Bags’, ‘Belts’, ‘Hats’, ‘Shoes’], dtype=’ <U5') You can take things further by counting the number of times they appear using the return_counts property: python

np.unique(products, return_counts=True)

Output: (array([‘Bags’, ‘Belts’, ‘Hats’, ‘Shoes’], dtype=’ <U5'), array([2, 1, 2, 3])). Let’s apply that to my temperature dataset. Currently, there aren’t any duplicates, so we are just gonna get our same input back. python

import numpy as np
temps = np.array([30, 32, 29, 35, 36, 33, 31])
np.unique(temps)

Output: array([29, 30, 31, 32, 33, 35, 36]) Notice how the figures are organised accordingly, too — in ascending order. You can also ask NumPy to count how many times each value appears: python

np.unique(temps, return_counts=True)

Output: (array([29, 30, 31, 32, 33, 35, 36]), array([1, 1, 1, 1, 1, 1, 1]))

Wrapping up

So far, these are the functions I stumbled upon. And I find them to be pretty helpful in data analysis. The beauty of NumPy is that the more you play with it, the more you uncover these tiny one-liners that replace pages of code. So next time you’re wrangling data or debugging a messy dataset, stay away from Pandas for a bit, and try dropping in one of these functions. Thanks for reading!

Register Now to Continue Reading

Unlock Towards Data Science with a Free Account

All articles on Towards Data Science are available to registered users. Create a free account today to access this article and thousands more. Register for freeSign in to your account


Written By

Ibrahim Salami

See all from Ibrahim Salami Data AnalyticsData VisualizationNumpyProgrammingStatistics Share This Article

Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program. Write for TDS

Related Articles

Visualizing household income from government sources - A guided walkthrough

Data Visualization Using python code which can be applied to data from your own dataset for generating your own charts and gifs James Koh, PhD March 7, 202413 min read

Pandas made easy (the guide - I)

Data Science With many examples of some of the most used functions and methods Gonçalo Guimarães Gomes October 6, 20209 min read

A/B Testing - A complete guide to statistical testing

Machine Learning Optimizing web marketing strategies through statistical testing Francesco Casalegno February 17, 20219 min read

Machine Learning on AWS SageMaker

Machine Learning How to predict employee salary using linear regression Mahsa Mir July 13, 20206 min read

Unlock the Secrets of Causal Inference with a Master Class in Directed Acyclic Graphs

Data Science A step-by-step explanation of Directed Acyclic Graphs from the basics through to more advanced aspects Graham Harrison April 6, 202338 min read

Understanding LLMs from Scratch Using Middle School Math

Artificial Intelligence In this article, we talk about how LLMs work, from scratch - assuming only that you know how to add and multiply two numbers. The article... Rohit Patel October 19, 202443 min read

Twitter analysis of the current political situation in Belarus

Data Science When independent sociological research is forbidden, Twitter and Python come in handy to analyze real political preferences. Daria Minsky July 29, 202011 min read

Embeddings + Knowledge Graphs: The Ultimate Tools for RAG Systems

Deep Learning The advent of large language models (LLMs) , trained on vast amounts of text data, has been one of the most significant breakthroughs in... Anthony Alcaraz November 14, 202311 min read

Using Causal Graphs to answer causal questions

Data Science Causal AI, exploring the integration of causal reasoning into machine learning Ryan OSullivan January 31, 20249 min read

Your home for data science and AI. The world's leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals. Subscribe to Our NewsletterWrite For TDS•About•Advertise•Privacy Policy•Terms of Use © Insight Media Group, LLC 2026•The New Stack•roadmap.sh•Creator Network

Recommended Articles

Close