NumPy

NumPy (Numerical Python) is the fundamental library for numerical computing in Python. It is widely used in data science, machine learning, scientific research, statistics, and any task that involves large-scale numerical data.

NumPy introduces powerful data structures like arrays, along with efficient mathematical operations. It is faster and more memory-efficient than Python lists, making it essential for high-performance computing.

1. What Is NumPy

NumPy is a Python library that provides:

  • A high-performance multidimensional array object (ndarray)
  • Mathematical functions to operate on arrays
  • Tools for linear algebra, random numbers, reshaping, and vector operations

NumPy is faster because it internally uses optimized C code, making operations highly efficient.

Installing NumPy

pip install numpy

Then import it:

import numpy as np

2. NumPy Arrays vs Python Lists

NumPy arrays are different from Python lists:

FeaturePython ListNumPy Array
SpeedSlowFast
Memory UsageHighLow
Supports Vectorized Ops           NoYes
TypeMixedSame data type
Best ForGeneral use              Numerical computing       

Example of vectorized computation:

import numpy as np

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

Output:

[2 4 6]

Lists cannot perform arithmetic like this.

3. Creating NumPy Arrays

From Python lists

import numpy as np

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

Multi-dimensional array

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

Functions to create arrays

FunctionPurpose
np.zeros(n)Array of zeros
np.ones(n)Array of ones
np.arange(start, stop, step)Range of numbers
np.linspace(start, stop, num)          Evenly spaced numbers       
np.eye(n)Identity matrix

Examples:

np.zeros(5)
np.ones((2, 3))
np.arange(0, 10, 2)
np.linspace(1, 5, 5)
np.eye(3)

4. Array Attributes

NumPy provides useful attributes:

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

print(arr.ndim)      # number of dimensions
print(arr.shape)     # rows, columns
print(arr.size)      # total elements
print(arr.dtype)     # data type

5. Indexing and Slicing

Indexing

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

Slicing

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

2-D slicing

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

print(arr[1, 2])      # row 1, col 2
print(arr[:, 1])      # all rows, col 1

6. Array Operations

NumPy supports fast mathematical operations on arrays without loops.

Element-wise operations

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

print(a + b)
print(a * b)
print(a ** 2)
print(a / 2)

7. Statistical Functions

NumPy provides built-in statistical operations:

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

print(arr.mean())
print(arr.max())
print(arr.min())
print(arr.sum())

8. Reshaping Arrays

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

Reshape changes the structure without modifying data.

9. Concatenation and Stacking

Concatenation

np.concatenate((a, b))

Vertical stacking

np.vstack((a, b))

Horizontal stacking

np.hstack((a, b))

10. Matrix Operations

NumPy supports linear algebra.

Matrix multiplication

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

print(np.dot(a, b))

Transpose

print(a.T)

11. Random Number Generation

NumPy has a random module:

np.random.rand(3)
np.random.randint(1, 10, 5)

12. Boolean Filtering

Powerful for selecting elements based on conditions.

arr = np.array([10, 20, 30, 40, 50])
print(arr[arr > 25])

13. Reading and Writing Data (Basic)

Save array:

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

Load array:

np.load("data.npy")