top of page

Everything About NumPy in Python: The Complete Beginner-to-Advanced Guide

  • Writer: Nhung Nguyen
    Nhung Nguyen
  • Jul 11
  • 4 min read


Introduction

If you plan to work with data analysis, machine learning, artificial intelligence, scientific computing, finance, or engineering, one Python library stands above the rest:

NumPy.

NumPy (Numerical Python) is the foundation of nearly every data science library in Python. Libraries such as Pandas, SciPy, Scikit-learn, TensorFlow, PyTorch, and OpenCV all rely on NumPy for efficient numerical computation.

Whether you're learning Python for the first time or preparing for a data science career, mastering NumPy is one of the best investments you can make.

In this guide, we'll cover everything you need to know about NumPy—from installation to advanced concepts—with practical examples.

What is NumPy?

NumPy is an open-source Python library designed for:

  • Numerical computation

  • Multi-dimensional arrays

  • Matrix operations

  • Mathematical functions

  • Linear algebra

  • Statistics

  • Random number generation

Unlike Python lists, NumPy arrays are optimized for speed and memory efficiency.

Why Do We Need NumPy?

Consider adding two Python lists.

a = [1,2,3]
b = [4,5,6]

result = []

for i in range(len(a)):
    result.append(a[i] + b[i])

print(result)

Output

[5,7,9]

With NumPy:

import numpy as np

a = np.array([1,2,3])
b = np.array([4,5,6])

print(a + b)

Output

[5 7 9]

Much cleaner—and significantly faster.

Installing NumPy

Using pip

pip install numpy

Using Anaconda

conda install numpy

Verify installation

import numpy as np

print(np.__version__)

Creating Arrays

From a List

import numpy as np

arr = np.array([1,2,3,4])
print(arr)

Output

[1 2 3 4]

Two-Dimensional Array

arr = np.array([
    [1,2,3],
    [4,5,6]
])

Result

[[1 2 3]
 [4 5 6]]

Three-Dimensional Array

arr = np.array([
    [[1,2],[3,4]],
    [[5,6],[7,8]]
])

Array Properties

arr = np.array([[1,2],[3,4]])

Shape

arr.shape

Output

(2,2)

Dimensions

arr.ndim

Output

2

Size

arr.size

Output

4

Data Type

arr.dtype

Output

int64

Special Arrays

Zeros

np.zeros((3,4))
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

Ones

np.ones((2,3))

Identity Matrix

np.eye(4)

Produces

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

Full

np.full((3,3), 9)

Empty

np.empty((2,2))

Creates an array without initializing its values.

Creating Number Sequences

arange()

np.arange(0,10)
0 1 2 3 4 5 6 7 8 9

Step

np.arange(0,20,2)

linspace()

np.linspace(0,1,5)

Output

0.
0.25
0.5
0.75
1.

Useful for plotting graphs.

Reshaping Arrays

arr = np.arange(12)
[0 1 2 3 4 5 6 7 8 9 10 11]

Reshape

arr.reshape(3,4)

Output

0 1 2 3
4 5 6 7
8 9 10 11

Flatten Arrays

arr.flatten()

or

arr.ravel()

Both convert multidimensional arrays into one dimension.

Array Indexing

arr = np.array([10,20,30,40])
arr[0]
10

Negative index

arr[-1]
40

Slicing

arr[1:3]

Output

20 30

Two dimensions

arr = np.array([
    [1,2,3],
    [4,5,6]
])

arr[:,1]

Output

2
5

Boolean Indexing

arr = np.array([10,20,30,40])

arr[arr > 20]

Output

30 40

Mathematical Operations

Addition

a + b

Subtraction

a - b

Multiplication

a * b

Division

a / b

Power

a ** 2

Square root

np.sqrt(a)

Logarithm

np.log(a)

Exponential

np.exp(a)

Statistical Functions

Mean

np.mean(arr)

Median

np.median(arr)

Maximum

np.max(arr)

Minimum

np.min(arr)

Standard Deviation

np.std(arr)

Variance

np.var(arr)

Sum

np.sum(arr)

Matrix Operations

Matrix multiplication

A @ B

or

np.dot(A,B)

Transpose

A.T

Inverse

np.linalg.inv(A)

Determinant

np.linalg.det(A)

Eigenvalues

np.linalg.eig(A)

Broadcasting

One of NumPy's most powerful features.

a = np.array([1,2,3])

a + 10

Output

11
12
13

Instead of manually looping, NumPy automatically broadcasts the scalar across every element.

Random Numbers

Random decimal

np.random.rand(5)

Random integers

np.random.randint(1,100,10)

Normal distribution

np.random.randn(1000)

Random seed

np.random.seed(42)

Ensures reproducible results.

Sorting

np.sort(arr)

Descending

np.sort(arr)[::-1]

Searching

Largest value index

np.argmax(arr)

Smallest value index

np.argmin(arr)

Joining Arrays

np.concatenate((a,b))

Stack vertically

np.vstack((a,b))

Stack horizontally

np.hstack((a,b))

Splitting Arrays

np.split(arr,3)

Saving Arrays

Save

np.save("data.npy", arr)

Load

np.load("data.npy")

Useful NumPy Functions

Function

Purpose

np.array()

Create array

np.arange()

Generate range

np.linspace()

Evenly spaced values

np.reshape()

Change shape

np.zeros()

Zero array

np.ones()

Ones array

np.eye()

Identity matrix

np.mean()

Average

np.sum()

Sum

np.max()

Maximum

np.min()

Minimum

np.std()

Standard deviation

Matrix multiplication

np.sqrt()

Square root

np.log()

Logarithm

np.exp()

Exponential

np.random.rand()

Random floats

np.random.randint()

Random integers

np.concatenate()

Join arrays

NumPy vs Python Lists

Feature

Python List

NumPy Array

Speed

Slow

Very Fast

Memory Usage

High

Low

Mathematical Operations

Limited

Extensive

Matrix Operations

No

Yes

Broadcasting

No

Yes

Scientific Computing

No

Yes

Common Applications of NumPy

NumPy is used in almost every scientific and data-driven field, including:

  • Data Science

  • Machine Learning

  • Artificial Intelligence

  • Deep Learning

  • Financial Modeling

  • Quantitative Trading

  • Image Processing

  • Computer Vision

  • Robotics

  • Signal Processing

  • Scientific Research

  • Statistics

  • Engineering Simulation

Tips for Learning NumPy

  • Practice creating arrays with different dimensions.

  • Understand the difference between lists and arrays.

  • Learn indexing and slicing thoroughly.

  • Master broadcasting—it eliminates many explicit loops.

  • Explore the numpy.linalg module for linear algebra.

  • Use vectorized operations instead of Python loops whenever possible.

  • Combine NumPy with Pandas and Matplotlib to build complete data analysis workflows.

Conclusion

NumPy is the cornerstone of Python's scientific computing ecosystem. By replacing slow Python loops with highly optimized, vectorized operations, it enables developers and data scientists to process large datasets efficiently while writing concise and readable code.

Whether you're building machine learning models, analyzing financial data, processing images, or conducting scientific research, a solid understanding of NumPy will make your programs faster, more scalable, and easier to maintain. Once you're comfortable with NumPy, you'll find it much easier to learn libraries such as Pandas, SciPy, Scikit-learn, TensorFlow, and PyTorch, all of which build upon its powerful array structures.

Investing time in mastering NumPy is one of the most valuable steps you can take on your Python journey, laying the groundwork for advanced analytics, automation, and AI development.


Resources : Internet

Recent Posts

See All

Comments


bottom of page