Hey there! 👋 Welcome to your friendly guide to NumPy - the superhero of numerical computing in Python! Whether you're just starting out with data science or looking to brush up on your NumPy skills, you're in the right place.
I created this guide while learning NumPy myself, so trust me - it's written with beginners in mind! Inside, you'll find clear examples and explanations of the most commonly used NumPy functions in Machine Learning, including:
- 🎲 Creating and playing with arrays (the building blocks of NumPy)
- 🔄 Reshaping and manipulating data like a pro
- 🎯 Picking out exactly the data you need (indexing & slicing)
- 🧮 Doing cool math stuff with arrays
- 📊 Statistical operations made easy
- ✅ Checking and filtering your data
- 🛠️ Essential tools for Machine Learning
📌 Future Plans:
- Optimizing Performance
- Real-World Applications
❗NOTE:
- This guide covers the most important parts of NumPy, but there's always more to explore in the NumPy Docs
- Some examples were created with help from AI tools (because why not use all available resources!)
- Some functions are explained in other files later in the repository. Sorry about that, will fix it.
Let's dive in and make NumPy fun!
All the best
This covers the following topics:
- Creating and Initializing Arrays
np.array()
→ Create arrays from lists/tuplesnp.zeros()
→ Create an array filled with zerosnp.ones()
→ Create an array filled with onesnp.full()
→ Create an array with a constant valuenp.eye()
→ Identity matrix (useful in linear algebra)np.linspace()
→ Generate evenly spaced numbersnp.arange()
→ Generate numbers with a step sizenp.random.rand()
→ Random samples from uniform distribution (0,1)np.random.randn()
→ Random samples from normal distributionnp.random.randint()
→ Random integers in a given range
- Data Manipulation
np.copy()
→ Create a duplicate of an arraynp.reshape()
→ Reshape arrays (e.g., from 1D to 2D)np.flatten()
→ Convert multi-dimensional arrays to 1Dnp.ravel()
→ Flatten an array (like flatten(), but returns a view when possible)np.transpose()
→ Transpose an arraynp.hstack()
→ Horizontally stack arraysnp.vstack()
→ Vertically stack arraysnp.concatenate()
→ Concatenate multiple arraysnp.split()
→ Split an array into subarrays
- Indexing and Slicing
np.where()
→ Conditional indexing (e.g., np.where(arr > 0, 1, 0))np.take()
→ Select elements at specific indicesnp.argsort()
→ Get indices that would sort an arraynp.argmax()
→ Get index of max valuenp.argmin()
→ Get index of min value
- Mathematical Operations
np.add()
,np.subtract()
,np.multiply()
,np.divide()
→ Element-wise operationsnp.power()
→ Element-wise exponentiationnp.exp()
→ Exponential functionnp.log()
→ Natural logarithmnp.log10()
→ Base-10 logarithmnp.sqrt()
→ Square rootnp.sin()
,np.cos()
,np.tan()
→ Trigonometric functionsnp.dot()
→ Dot product (used in linear algebra)np.matmul()
→ Matrix multiplicationnp.linalg.inv()
→ Matrix inversenp.linalg.det()
→ Determinant of a matrixnp.linalg.eig()
→ Eigenvalues and eigenvectorsnp.linalg.svd()
→ Singular Value Decomposition (SVD)
- Statistical and Aggregation Functions
np.sum()
→ Sum of elementsnp.mean()
→ Mean of elementsnp.median()
→ Median of elementsnp.var()
→ Variancenp.std()
→ Standard deviationnp.corrcoef()
→ Pearson correlation coefficientnp.percentile()
→ Compute percentile of datanp.histogram()
→ Compute histogram of an array
- Boolean and Comparison Operations
np.all()
→ Check if all elements satisfy a conditionnp.any()
→ Check if any element satisfies a conditionnp.isfinite()
→ Check for finite valuesnp.isinf()
→ Check for infinite valuesnp.isnan()
→ Check for NaNs
- Utility Functions for ML
np.clip()
→ Clip values within a specified rangenp.unique()
→ Find unique values in an arraynp.bincount()
→ Count occurrences of valuesnp.meshgrid()
→ Generate coordinate grids (useful in visualization)
- Random Number
np.random.rand()
→ Uniform distribution (0 to 1)np.random.randn()
→ Standard normal distribution (mean=0, std=1)np.random.randint()
→ Random integersnp.random.choice()
→ Sampling from an arraynp.random.seed(42)
→ Why setting a seed is important for MLnp.random.permutation()
→ Shuffling arrays (important for dataset splitting)np.random.shuffle()
→ In-place shufflingnp.random.normal()
→ Generating synthetic datanp.random.uniform()
→ Anoth
If you want to follow a path for learning through this repo, the following would be best:
numpy_for_noobs/
│── 📜 README.md # Project introduction & guide
│
├── 🔰 basics/ # Fundamental NumPy concepts
│ ├── 📂 01_creating_arrays.ipynb # Creating and initializing arrays
│ ├── 📂 02_order_in_numpy.md # Memory order (C vs. F order)
│ ├── 📂 03_indexing_slicing.ipynb # Accessing elements in arrays
│ ├── 📂 04_mathematical_operations.ipynb # Basic math operations
│ ├── 📂 05_boolean_comparison.ipynb # Boolean and comparison operations
│
├── ⚡ operations/ # Array manipulations & computations
│ ├── 📂 06_vector_and_matrix.ipynb # Vector and matrix operations
│ ├── 📂 07_data_manipulation.ipynb # Transforming and reshaping arrays
│ ├── 📂 08_statistical_functions.ipynb # Aggregation & statistical functions
│ ├── 📂 09_random_numbers.ipynb # Generating random numbers
│
├── 🚀 advanced/ # Advanced NumPy techniques
│ ├── 📂 10_linear_algebra.ipynb # Linear algebra operations
│ ├── 📂 11_utility_functions_ml.ipynb # NumPy utilities for ML
│ ├── 📂 12_optimizing_performance.ipynb(to be added?) # Performance optimization
│
└── 🌍 extra/ # Additional resources
├── 📂 13_C-Order_and_F-Order.md # Explanation of memory layouts
├── 📂 14_real_world_applications.ipynb(to be added?) # NumPy in real-world ML tasks
- Open the notebook in Google Colab.
- Run the code cells sequentially to understand the concepts and see the output.
- Experiment with the code by modifying values and observing the results.
- Python 3.x
- NumPy library
Sagar Sharma