Python NumPy for Data Analysis
Arrays and indexing, vectorised operations, broadcasting, descriptive statistics with the axis argument, random numbers for simulation, and common mistakes, with small runnable examples.
9 programs
Choose the best training program according to your career.
Improve the performance of your business with our innovative Industrial Training Program.
Read More →
Our instructor-led training classes are designed to make you an expert.
Read More →
Real-life case studies are a great way to demonstrate your practical knowledge.
Read More →
Explore blogs, latest technologies and industry updates with practical examples.
Read More →Browse all available training programs and choose the right one for your career.
Read More →Know more about who we are and what we stand for.
Discover our mission to equip individuals with essential IT skills and industry expertise.
Read More →Learn, practice and plan your career — bilkul free.
Book a free video counselling session and pick the right course for you.
Read More →Download the NumPy for data analysis guide PDF from Uncodemy: arrays, vectorisation, broadcasting and statistics with runnable examples.
This page offers Uncodemy's NumPy data analysis guide as a PDF download. NumPy provides fast arrays and vectorised mathematics, and it sits underneath pandas and many other analysis libraries. The guide covers arrays, indexing, vectorised operations, broadcasting, statistics and random numbers, with small examples you can run as they are.
Download the guide below. Each download is its own file, and everything in it is also explained on this page.
Arrays and indexing, vectorised operations, broadcasting, descriptive statistics with the axis argument, random numbers for simulation, and common mistakes, with small runnable examples.
A short form asks for your details before the download starts. Your details are used only to share study material and course information.
NumPy supplies the ndarray, a grid of values of one type stored compactly in memory. Because operations run over whole arrays in optimised compiled code, they are far faster than Python loops, and the code is shorter and easier to read. Almost every Python data library is built on NumPy arrays, including pandas, SciPy and scikit-learn.
As an analyst you may use NumPy directly for numeric calculations, simulation and statistics, and you will meet it indirectly every time you use pandas. Understanding it helps you write faster code and understand the behaviour of the tools built on top of it.
Arrays can be built from lists or generated with helper functions.
import numpy as np
a = np.array([12, 15, 11, 18, 20, 14])
zeros = np.zeros(3)
steps = np.arange(0, 10, 2) # 0, 2, 4, 6, 8
grid = np.linspace(0, 1, 5) # 5 evenly spaced values
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape, a.dtype, matrix.shape)
Arithmetic applies to every element at once; there is no need for a loop.
prices = np.array([100.0, 250.0, 80.0, 400.0])
qty = np.array([3, 1, 5, 2])
revenue = prices * qty # element by element
print(revenue) # [300. 250. 400. 800.]
print(revenue.sum(), revenue.mean())
print((revenue > 300)) # boolean array
print(revenue[revenue > 300]) # boolean filtering
| Goal | Syntax | Meaning |
|---|---|---|
| One element | a[2] | Third element (indexing starts at 0) |
| A range | a[1:4] | Elements 1, 2 and 3 |
| From the end | a[-2:] | The last two elements |
| A row of a matrix | m[0, :] | First row, all columns |
| A column of a matrix | m[:, 1] | All rows, second column |
| By condition | a[a > 15] | Elements greater than 15 |
NumPy includes the descriptive statistics that analysts use every day: mean, median, standard deviation, variance, minimum, maximum and percentiles. For two-dimensional arrays you can specify an axis so that the calculation runs down columns or across rows.
Remember that the default standard deviation in NumPy is the population version; set ddof to 1 for the sample standard deviation, which is what most analysis uses. It is a small detail that explains why two tools sometimes disagree in the third decimal place.
Column-wise and row-wise summaries of a small matrix of monthly sales for three products.
sales = np.array([[120, 135, 150],
[ 90, 85, 100],
[200, 210, 190]]) # rows = products, columns = months
print(sales.sum(axis=0)) # total per month
print(sales.mean(axis=1)) # average per product
print(np.median(sales), np.percentile(sales, 90))
print(sales.std(ddof=1)) # sample standard deviation
Broadcasting lets NumPy combine arrays of different shapes by stretching the smaller one along the missing dimension, without copying data. Adding a single number to an array is the simple case. A more useful case is subtracting each column's mean from a matrix to centre the data, or dividing each row by its total to get shares.
The rule is that dimensions are compared from the right and must be equal or one of them must be one. When shapes do not fit you receive a clear error that tells you the shapes, which is the signal to reshape or add an axis.
Centre each column and turn each row into percentage shares.
centered = sales - sales.mean(axis=0) # subtract column means
shares = sales / sales.sum(axis=1, keepdims=True) # row shares
print(centered.round(1))
print((shares * 100).round(1))
A seeded generator gives repeatable results, useful for testing ideas such as a simple simulation of daily orders.
rng = np.random.default_rng(seed=42)
daily_orders = rng.poisson(lam=20, size=30) # 30 days, average 20 orders
print(daily_orders.mean(), daily_orders.std(ddof=1))
In day-to-day analysis you will usually work in pandas, but a few tasks are cleaner in plain NumPy. Simulations with random numbers, matrix calculations, fast element-wise functions and anything involving arrays of more than two dimensions are examples.
It is also useful for checking a calculation independently: compute the same total with NumPy and with pandas and confirm they agree. Treat NumPy as the engine under the hood that you can open when needed.
| Function | What it does |
|---|---|
| np.where(cond, a, b) | Choose a or b element by element |
| np.cumsum(x) | Running total |
| np.diff(x) | Difference between consecutive elements |
| np.unique(x, return_counts=True) | Distinct values and how often each appears |
| np.corrcoef(x, y) | Correlation matrix of two variables |
| np.round(x, 2) | Round to a number of decimals |
Want to use NumPy and pandas the way analysts do at work? Explore Uncodemy's Data Analytics course and learn from industry trainers.