By Ram Sir

calculate NumPy Mastery Course

Dive deep into NumPy, Python's essential library for scientific computing, with theory, hands-on code, and expert tips from Ram Sir.

Start Learning NumPy
person

About Your Instructor

Ram Sir is a Python data science educator with 2+ years of experience in scientific computing and analytics. He has guided thousands in mastering Python, NumPy, and data-driven problem solving.

NumPy Core Concepts

view_module

Arrays & Creation

  • Understanding ndarrays
  • Array creation methods
  • Data types (dtype)
repeat

Array Operations

  • Arithmetic & broadcasting
  • Shape manipulation
  • Aggregation functions
filter_alt

Indexing & Slicing

  • Basic & advanced indexing
  • Boolean filters
  • Fancy indexing
functions

Mathematical Functions

  • Universal functions (ufuncs)
  • Statistical ops
  • Linear algebra
extension

Advanced Topics

  • Random numbers
  • Broadcasting tricks
  • Masked arrays
Arrays
Operations
Indexing
Math
Advanced

view_module NumPy Arrays & Creation

NumPy's core is the ndarray, a fast, space-efficient multidimensional array. Arrays are created in many ways:

code Basic Array Creation

import numpy as np

a = np.array([1, 2, 3])          # 1D array
b = np.zeros((2, 3))             # 2x3 zero array
c = np.ones(5)                   # 1D array of ones
d = np.eye(3)                    # 3x3 identity matrix
e = np.arange(0, 10, 2)          # Even numbers [0,2,4,6,8]
f = np.linspace(0, 1, 5)         # 5 evenly spaced numbers from 0 to 1
g = np.full((2,2), 7)            # 2x2 array filled with 7

code Array Attributes

a.shape         # Dimensions (tuple)
a.dtype         # Data type
a.size          # Number of elements
a.ndim          # Number of dimensions
a.itemsize      # Size of one item (bytes)

sticky_note_2 Key Notes

NumPy arrays are homogeneous and vastly faster than native Python lists for large data.

repeat Array Operations

NumPy supports vectorized operations, broadcasting, and aggregation:

code Arithmetic & Broadcasting

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b           # [5, 7, 9]
d = a * 2           # [2, 4, 6]
e = a ** 2          # [1, 4, 9]
f = a + 10          # [11, 12, 13]

code Aggregation Functions

arr = np.array([[1,2,3],[4,5,6]])
arr.sum()           # 21
arr.mean(axis=0)    # [2.5, 3.5, 4.5]
arr.min()           # 1
arr.max(axis=1)     # [3, 6]
arr.std()           # Standard deviation

code Shape Manipulation

arr.reshape((3,2))      # Change shape
arr.T                   # Transpose
arr.flatten()           # 1D copy
arr.ravel()             # 1D view (if possible)

sticky_note_2 Good to Know

Broadcasting allows arithmetic between differently-shaped arrays, e.g., adding a vector to every row of a matrix.

filter_alt Indexing & Slicing

Access and manipulate data efficiently:

code Basic Indexing/Slicing

a = np.array([10, 20, 30, 40, 50])
a[1]         # 20
a[1:4]       # [20, 30, 40]
a[-1]        # 50

mat = np.array([[1,2,3],[4,5,6]])
mat[0,2]     # 3
mat[:,1]     # [2,5]

code Boolean Indexing

a = np.array([10, 20, 30, 40, 50])
mask = a > 25
a[mask]      # [30, 40, 50]

code Fancy Indexing

a = np.array([10, 20, 30, 40])
idx = [0, 2]
a[idx]       # [10, 30]

sticky_note_2 Good to Know

Slicing returns views (not copies) when possible. Changing a slice may affect the original array!

functions Mathematical & Statistical Functions

NumPy provides many universal functions (ufuncs) and mathematical/statistical tools:

code Universal Functions (ufuncs)

a = np.array([0, np.pi/2, np.pi])
np.sin(a)           # [0., 1., 0.]
np.exp(a)           # [1., 4.81, 23.1...]
np.log(a+1)         # [0., 1.18, 1.54]

code Statistical Functions

a = np.array([1, 2, 3, 4, 5])
np.median(a)        # 3.0
np.percentile(a, 80) # 4.2
np.corrcoef(a, a)   # Correlation matrix

code Linear Algebra

mat = np.array([[1,2],[3,4]])
np.linalg.inv(mat)          # Inverse
np.dot(mat, mat)            # Matrix multiplication
np.linalg.eig(mat)          # Eigenvalues/vectors

sticky_note_2 Key Notes

NumPy's rich set of mathematical/statistical functions make it ideal for scientific computing and analytics.

extension Advanced NumPy Features

Go further with NumPy's advanced features:

code Random Numbers

np.random.seed(42)
np.random.rand(2,3)         # 2x3 array, uniform [0,1)
np.random.randn(4)          # 4 samples, standard normal
np.random.randint(0, 10, 5) # 5 random ints [0,10)

code Broadcasting Tricks

a = np.arange(3)          # [0,1,2]
b = np.arange(3)[:,np.newaxis] # [[0],[1],[2]]
a + b   # 3x3 grid: pairwise sums

code Masked Arrays

import numpy.ma as ma
a = np.array([1, 2, -99, 4])
masked = ma.masked_values(a, -99)
masked.mean()      # Ignores -99

sticky_note_2 Good to Know

Explore np.save, np.load for saving arrays, and np.vectorize for custom vectorized operations.

NumPy Playground

Try Python NumPy Code

Results will appear here...

NumPy Function Reference

Explore the most important NumPy functions with examples:

add_box

Array Creation

  • np.array(): Make ndarray from list/tuple
  • np.zeros(), np.ones(), np.full()
  • np.arange(), np.linspace()
a = np.zeros((3,2))
b = np.linspace(0,1,5)
edit_attributes

Array Manipulation

  • reshape(), flatten(), transpose()
  • concatenate(), vstack(), hstack()
x = np.arange(6).reshape(2,3)
y = x.flatten()
filter_list

Selection & Filtering

  • where(), nonzero(), argmax(), argmin()
  • Boolean/fancy indexing
a = np.array([1,2,3,4])
idx = np.where(a%2==0)
calculate

Math & Stats

  • sum(), mean(), median()
  • std(), var(), corrcoef()
x = np.random.randn(5)
m = np.mean(x)
functions

Linear Algebra

  • dot(): Matrix multiplication
  • linalg.inv(), linalg.eig()
A = np.eye(2)
B = np.linalg.inv(A)
casino

Random Numbers

  • np.random.rand(), randn()
  • np.random.randint()
  • np.random.seed()
np.random.seed(0)
data = np.random.randint(0,10,4)