2.1 NumPy Arrays and Operations

Loading content...

2.1 NumPy Arrays and Operations

Module 2.1 — NumPy Arrays and Operations ðŸ”Ē ðŸĪ– Module 2.1 â€Ē Python for Machine Learning ðŸ”Ē NumPy Arrays and Operations The backbone of every ML model — learn to store, manipulate, and operate on data at lightning speed with NumPy arrays. 🏗ïļ The Analogy — Toolkit Upgrade Imagine you need to dig 100 holes in your garden. ðŸŒą You could use a spoon (Python lists) — it works, but it's painfully slow. Or you could use a power drill (NumPy) — same holes, 50x faster! ⚡ Python lists are general-purpose containers — they can hold anything (numbers, strings, cats ðŸą). But when you need to do math on thousands of numbers (which is ALL of Machine Learning), they're like using a spoon to dig holes. NumPy is purpose-built for fast number crunching. 🚀 NumPy (Numerical Python) is a Python library that provides fast, efficient multi-dimensional array objects and mathematical functions to operate on them. It's the foundation of nearly every ML and data science library. ðŸ§Ū ⚡ NumPy is up to 50x faster than Python lists for numerical operations 📊 Every ML dataset is stored as a NumPy array 🧠 Neural network weights, image pixels, audio waves — all NumPy arrays 🐍 Libraries like Pandas, Scikit-learn, TensorFlow all use NumPy internally ðŸ’ū Uses less memory than Python lists Python lists store each item as a separate object scattered in memory. NumPy stores numbers in a contiguous block of memory (like seats in a row), so the CPU can process them all in one sweep. This is called vectorization. 🏎ïļ âœ… True or False — Activity 1 NumPy arrays are slower than Python lists for mathematical operations. Before we dive in, here are the terms you'll hear constantly. 📝 Don't memorize — just skim now and refer back as needed! A container that holds numbers in a structured grid The dimensions of an array (rows × columns) The data type of elements in the array Number of dimensions (1D, 2D, 3D...) A 1D array — a single row or column of numbers A 2D array — rows and columns (like a spreadsheet) A 3D+ array — like a stack of matrices Changing the shape of an array without changing data Applying an operation to all elements at once (no loops) NumPy's way of handling arrays with different shapes in operations 🧠 Quick Check — Activity 2 A 2D array (rows and columns) is commonly called a: Think of creating a NumPy array like organizing items on a shelf. ðŸ“Ķ You can arrange them by hand (np.array()), fill a shelf with zeros (np.zeros()), or create a numbered sequence (np.arange()). 1ïļâƒĢ From a Python List — np.array() The most common way. Hand your list to NumPy and it creates an efficient array. ðŸŽŊ scores = np.array([85, 92, 78, 95, 88]) # ðŸ“Ķ Array of zeros (5 zeros) # ðŸŸĐ Array of ones (3 ones) # ðŸ”Ē Range of numbers (0 to 9) sequence = np.arange(0, 10) # ðŸ”Ē Range with step (0, 2, 4, 6, 8) evens = np.arange(0, 10, 2) np.zeros(n) → n zeros | np.ones(n) → n ones | np.arange(start, stop, step) → sequence. Just like Python's range() but returns an array! ðŸŽŊ 2ïļâƒĢ 2D Arrays (Matrices) — List of Lists Pass a list of lists to create a grid. Think of a spreadsheet where each inner list is a row. 📋 # 📋 2D Array (3 students × 2 subjects) [85, 92], # Student 1: Math, Science [78, 88], # Student 2: Math, Science [95, 91] # Student 3: Math, Science # ðŸ“Ķ 2D zeros: 3 rows × 4 columns # ðŸŽŊ Identity matrix (1s on diagonal) 🧠 Quick Check — Activity 3 Which function creates an array filled with all zeros? ✍ïļ Fill in the Blank — Activity 4 np.arange(0, 10, 2) produces [0, 2, 4, 6, ___]. What's the missing number? ðŸ”Ē 🔍 Array Attributes — Inspecting Your Arrays Just like checking the specs on a phone ðŸ“ą (screen size, storage, weight), every NumPy array has attributes that tell you about its structure. Dimensions as a tuple: (rows, cols) Number of dimensions (1, 2, 3...) Data type of elements (int64, float64) print(arr.shape) # (2, 3) .shape returns a tuple. For (2, 3): first number = rows (2), second = columns (3). Total elements = 2 × 3 = 6 (which matches .size). 📐 🧠 Quick Check — Activity 5 For np.array([[1,2,3],[4,5,6]]), what does .ndim return? For a NumPy array with shape (4, 5), what is .size? (Total elements) ðŸ§Ū ⚔ïļ Array vs List — The Speed Battle Let's see exactly why NumPy beats Python lists. It's not just speed — the behavior is different too! ðŸĪŊ ðŸ“Ķ Each element stored as separate Python object 🐌 Operations use Python loops (slow) ➕ [1,2] + [3,4] = [1,2,3,4] (concatenation!) 🔀 Can mix types: [1, "hello", 3.14] ðŸ“Ķ All elements stored in one contiguous block ⚡ Operations use compiled C code (fast) ➕ arr1 + arr2 = [4, 6] (element-wise!) 🔒 All same type: [1, 2, 3] all integers With Python lists, + means concatenation (joining). With NumPy, + means element-wise addition. This catches beginners every time! ðŸŠĪ 🎎 List vs Array: Speed Comparison Watch how Python lists process one element at a time, while NumPy processes ALL at once ⚡ Press Play to start the speed comparison 🏁 Python List (slow, sequential) ✅ True or False — Activity 7 In Python, [1, 2, 3] + [4, 5, 6] gives [5, 7, 9] (element-wise addition). 📐 1D, 2D, 3D Arrays — Vectors, Matrices, Tensors

Subject: Python for Machine Learning | Chapter: Chapter 2: Python for Data Science

1.9 File Handling (read, write CSV) Chapter 2: Python for Data Science Course Complete!
Topics
Chapter 1: Python Essentials
Chapter 2: Python for Data Science
© Copyright ÂĐ 2020 ALGOPK . All Rights Reserved.