Learn NUMPY with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
1
NumPy Array Operations Example
import numpy as np
# Create arrays
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
# Array arithmetic
sum_arr = arr1 + arr2
print('Sum:', sum_arr)
# Statistical operations
print('Mean of arr1:', np.mean(arr1))
print('Standard deviation of arr2:', np.std(arr2))
# Multi-dimensional arrays
matrix = np.array([[1,2],[3,4]])
print('Matrix:
', matrix)
print('Transpose:
', matrix.T)
A minimal NumPy example demonstrating array creation, arithmetic, and basic statistics.
2
NumPy Array Indexing and Slicing
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print('Original array:', arr)
# Indexing
print('Element at index 2:', arr[2])
# Slicing
print('Elements from index 1 to 3:', arr[1:4])
print('Every second element:', arr[::2])
Demonstrates indexing and slicing of NumPy arrays.
3
NumPy Random Number Generation
import numpy as np
# Random integers
rand_ints = np.random.randint(0, 10, size=5)
print('Random integers:', rand_ints)
# Random floats
rand_floats = np.random.rand(3,3)
print('Random floats 3x3:\n', rand_floats)
Generating random numbers and arrays using NumPy.
4
NumPy Array Reshaping
import numpy as np
arr = np.arange(12)
print('Original array:', arr)
matrix = arr.reshape((3,4))
print('Reshaped 3x4 matrix:\n', matrix)
flattened = matrix.flatten()
print('Flattened array:', flattened)
Changing the shape of arrays.
5
NumPy Mathematical Functions
import numpy as np
arr = np.array([1,4,9,16])
print('Square root:', np.sqrt(arr))
print('Exponential:', np.exp(arr))
print('Natural log:', np.log(arr))
Applying mathematical functions on arrays.
6
NumPy Boolean Indexing
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
print('Original array:', arr)
mask = arr > 20
print('Boolean mask:', mask)
print('Filtered array:', arr[mask])
Filtering arrays using boolean conditions.
7
NumPy Stacking Arrays
import numpy as np
arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
vstacked = np.vstack((arr1, arr2))
hstacked = np.hstack((arr1, arr2))
print('Vertical stack:\n', vstacked)
print('Horizontal stack:', hstacked)
Combining arrays using vertical and horizontal stacking.
8
NumPy Linear Algebra Operations
import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
# Matrix multiplication
C = np.dot(A, B)
print('Matrix multiplication:\n', C)
# Determinant
print('Determinant of A:', np.linalg.det(A))
# Inverse
print('Inverse of A:\n', np.linalg.inv(A))
Matrix multiplication, determinant, and inverse using NumPy.
9
NumPy Statistical Analysis
import numpy as np
arr = np.array([1,2,3,4,5])
arr2 = np.array([5,4,3,2,1])
print('Mean:', np.mean(arr))
print('Median:', np.median(arr))
print('Std deviation:', np.std(arr))
print('Correlation:', np.corrcoef(arr, arr2))
Compute mean, median, standard deviation, and correlation.
10
NumPy Broadcasting Example
import numpy as np
arr = np.array([[1,2,3],[4,5,6]])
scalar = 10
# Add scalar to array
print('Add scalar:\n', arr + scalar)
# Subtract row vector
row_vec = np.array([1,0,1])
print('Subtract row vector:\n', arr - row_vec)
Demonstrates broadcasting operations between arrays of different shapes.