Skip to content
AI & MLbeginner

What is NumPy?

Updated Nov 30, 20263 min readBased on official documentation
What is NumPy?

In short

NumPy is a Python library for fast numerical work. It adds a multidimensional array object, which stores many numbers in one structure, along with routines that run math on whole arrays at once. Data scientists and machine learning engineers use it as a base for many other tools.

What NumPy does

NumPy is short for Numerical Python. It is the basic package for scientific computing in Python. Its main feature is the array, a grid of values that all share one type, such as whole numbers or decimals. An array can have one dimension, like a list, or several, like a table or a stack of tables.

Python has its own lists, but lists are slow when you work with large amounts of numbers. NumPy arrays store their values compactly, and NumPy runs most operations in fast compiled code. You write a short expression, and the library handles the loop over every element for you.

How it works

The central object is the ndarray, usually called just an array. Every array has a shape, which tells you the size along each dimension, and a data type, which tells you what each value is. You create arrays from Python lists or ask NumPy to build them, for example filled with zeros or with evenly spaced numbers.

Once you have an array, you can apply math to it directly. Adding two arrays adds them element by element. Multiplying an array by a number scales every value. This style is called vectorized code, and it replaces many explicit loops.

NumPy also ships with a large set of routines. They cover sorting, reshaping, selecting values by condition, reading and writing files, Fourier transforms, basic linear algebra, simple statistics and random number generation. The official documentation describes these in its reference guide, which lists every function with its parameters.

A simple example

The following short script builds an array of five numbers, scales it, and computes a few summary values:

import numpy as np

prices = np.array([12.5, 8.0, 15.25, 9.75, 11.0])
discounted = prices * 0.9

print(discounted)
print(discounted.mean())
print(discounted.max())
print(prices[prices > 10])

The first line imports the library under the short name np, which is the convention most people follow. The multiplication applies to every element at once. The last line selects only the prices above 10, using a condition written directly on the array. You do not need a loop for any of these steps.

When to use it

Use NumPy when you have numbers in bulk and need to transform them quickly. Typical cases include cleaning sensor readings, preparing matrices for a model, or running simulations. Many other Python libraries for data analysis and machine learning accept NumPy arrays as their input format or build on them internally, so learning the array model pays off across the ecosystem.

NumPy is less suited to data with mixed types and labels, such as a spreadsheet with text columns and dates. For that kind of table, a library built on top of NumPy is often a better fit. Knowing the basics of arrays still helps, because those tools rely on the same ideas.

Common mistakes

Beginners often mix up Python lists and NumPy arrays. The + operator joins two Python lists, but it adds two NumPy arrays element by element. Check the type of your data when results look unexpected.

Another frequent problem is the data type. An array holds one type throughout, so if you create it from a mix of integers and decimals, NumPy picks a single type for all values, and some information may be lost. Set the dtype argument when the type matters.

Finally, remember that many operations return a new array rather than changing the original. If you expect the original to change, assign the result to a variable.

Learning path

The project offers three main resources. The Absolute Beginner's Guide introduces the core ideas and links to more tutorials. The user guide explains key concepts with background information. The reference guide describes each function in detail, including how methods work and which parameters they accept. Start with the beginner guide, then move to the reference when you need exact details.

NumPy is also a good stepping stone to other topics. If you later train models, you may meet ideas such as a neural network or gradient descent, and the array operations you practise here are the same ones those methods use.

FAQ

Is NumPy part of the Python standard library?

No. NumPy is a separate package that you install on top of Python. Once installed, you import it in your code with import numpy as np.

What is the difference between a NumPy array and a Python list?

A NumPy array holds values of one data type and supports element-wise math on the whole array. A Python list can mix types and does not do math across all its items by default.

Do I need to know linear algebra to use NumPy?

No. You can use NumPy for basic tasks like sorting and averaging without advanced math. Linear algebra routines are available when you need them.

Where can I find the full list of NumPy functions?

The reference guide in the official documentation lists every function, module and object, with descriptions of their parameters.

Sources

This page is based on the official documentation below. Read it for full detail.

NumPy — official documentation ↗

NumPy in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.