NumPy Introduction

NumPy Tutorial

NumPy Introduction

NumPy stands for Numerical Python. It is one of the most important and fundamental libraries in Python, especially used for numerical computing and scientific calculations. Many popular Python libraries like Pandas, Matplotlib, SciPy, Scikit-learn, and TensorFlow are built on top of NumPy.

If you want to work with data science, machine learning, AI, statistics, or engineering, learning NumPy is essential.


 Why NumPy Was Created?

Python’s built-in data structures like lists are flexible but:

  • They are slow for large numerical data

  • They consume more memory

  • Mathematical operations require loops

NumPy solves these problems by providing:

  • Fast execution (written in C internally)

  • Efficient memory usage

  • Vectorized operations (no loops required)


 What Is NumPy?

NumPy is a Python library that provides:

  • A powerful N-dimensional array object (ndarray)

  • Mathematical functions

  • Linear algebra operations

  • Random number generation

  • Tools for working with matrices and vectors


 Core Data Structure: ndarray

The heart of NumPy is the ndarray (N-Dimensional Array).

Key Characteristics:

  • Stores elements of same data type

  • Fixed size (cannot grow like lists)

  • Faster than Python lists

  • Supports multi-dimensional data (1D, 2D, 3D…)

Example:


 

Output:

[10 20 30 40]
<class 'numpy.ndarray'>

Dimensions in NumPy Arrays

1D Array (Vector)

2D Array (Matrix)

3D Array (Tensor)


Python List vs NumPy Array

FeaturePython ListNumPy Array
SpeedSlowerFaster
MemoryHighLow
Data TypeMixedSame
Vector Operations NoYes
Scientific UseLimitedPowerful

Example:


 

Output:

[1, 2, 3, 1, 2, 3]
[2 4 6]

 Vectorization (Major Advantage)

NumPy allows vectorized operations, meaning calculations happen element-wise without loops.

Output:

[11 12 13 14]

This makes code:

  • Shorter

  • Faster

  • Easier to read


NumPy Mathematical Capabilities

NumPy supports:

  • Arithmetic operations (+ - * /)

  • Trigonometric functions (sin, cos, tan)

  • Statistical functions (mean, median, std)

  • Linear algebra (dot, transpose, inverse)

  • Random numbers

Example:


 


Broadcasting in NumPy

Broadcasting allows NumPy to perform operations on arrays of different shapes.

Output:

[6 7 8]

NumPy Installation

Install NumPy using pip:

pip install numpy

Check version:

import numpy as np
print(np.__version__)

 Where Is NumPy Used?

NumPy is widely used in:

  • Data Science

  • Machine Learning & AI

  • Financial Analysis

  • Scientific Research

  •  Image Processing

  • Signal Processing


 Summary

  • NumPy is a fast, efficient numerical library

  • Its core object is ndarray

  • Supports multi-dimensional arrays

  • Enables vectorized operations

  • Backbone of Python’s data ecosystem

You may also like...