Skip to content

Commit 32214e3

Browse files
authored
Merge pull request #144 from codeharborhub/dev-1
Doneprogramming fundamentals
2 parents 100165e + fc20393 commit 32214e3

File tree

13 files changed

+1346
-0
lines changed

13 files changed

+1346
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: Conditionals and Branching
3+
sidebar_label: Conditionals
4+
description: "Mastering If, Else, and Elif statements to control program flow and handle logic in Machine Learning pipelines."
5+
tags: [python, programming, logic, conditionals, branching, mathematics-for-ml]
6+
---
7+
8+
Machine Learning is often about making decisions. **Conditionals** allow our code to react differently depending on the input. Whether it's checking if a dataset is empty or deciding which model to load, `if-else` logic is the foundation of programmatic decision-making.
9+
10+
## 1. The `if`, `elif`, and `else` Structure
11+
12+
Python uses indentation to define the scope of conditional blocks.
13+
14+
```python
15+
accuracy = 0.85
16+
17+
if accuracy > 0.90:
18+
print("Excellent model performance!")
19+
elif accuracy > 0.70:
20+
print("Good performance, but could be improved.")
21+
else:
22+
print("Model needs retraining.")
23+
24+
```
25+
26+
```mermaid
27+
flowchart TD
28+
Start([Check Accuracy]) --> C1{Acc > 0.90?}
29+
C1 -- Yes --> R1[Print: Excellent]
30+
C1 -- No --> C2{Acc > 0.70?}
31+
C2 -- Yes --> R2[Print: Good]
32+
C2 -- No --> R3[Print: Retrain]
33+
34+
```
35+
36+
## 2. Comparison and Logical Operators
37+
38+
Conditionals rely on boolean expressions that evaluate to either `True` or `False`.
39+
40+
### A. Comparison Operators
41+
42+
* `==` (Equal to)
43+
* `!=` (Not equal to)
44+
* `>` / `<` (Greater/Less than)
45+
* `>=` / `<=` (Greater/Less than or equal to)
46+
47+
### B. Logical Operators (Chaining)
48+
49+
* `and`: Both conditions must be True.
50+
* `or`: At least one condition must be True.
51+
* `not`: Reverses the boolean value.
52+
53+
```python
54+
# Check if learning rate is within a safe range
55+
lr = 0.001
56+
if lr > 0 and lr < 0.1:
57+
print("Learning rate is valid.")
58+
59+
```
60+
61+
## 3. The "ReLU" Example: Math meets Logic
62+
63+
One of the most famous conditional operations in Deep Learning is the **Rectified Linear Unit (ReLU)** activation function.
64+
65+
$$
66+
\text{ReLU}(x) = \max(0, x)
67+
$$
68+
69+
In Python code, this is a simple conditional:
70+
71+
```python
72+
def relu(x):
73+
if x > 0:
74+
return x
75+
else:
76+
return 0
77+
78+
```
79+
80+
## 4. Truthiness and Identity
81+
82+
In ML data cleaning, we often check if a variable actually contains data.
83+
84+
* **Falsy values:** `None`, `0`, `0.0`, `""` (empty string), `[]` (empty list), `{}` (empty dict).
85+
* **Truthy values:** Everything else.
86+
87+
```python
88+
features = get_features()
89+
90+
if not features:
91+
print("Warning: No features found in dataset!")
92+
93+
```
94+
95+
### `is` vs `==`
96+
97+
* `==` checks for **Value equality** (Are the numbers the same?).
98+
* `is` checks for **Identity** (Are they the exact same object in memory?).
99+
100+
## 5. Inline Conditionals (Ternary Operator)
101+
102+
For simple logic, Python allows a one-liner known as a ternary operator.
103+
104+
```python
105+
# status = "Spam" if probability > 0.5 else "Not Spam"
106+
prediction = "Positive" if y_hat > 0.5 else "Negative"
107+
108+
```
109+
110+
## 6. Match-Case (Python 3.10+)
111+
112+
For complex branching based on specific patterns (like different file extensions or model types), the `match` statement provides a cleaner syntax than multiple `elif` blocks.
113+
114+
```python
115+
optimizer_type = "adam"
116+
117+
match optimizer_type:
118+
case "sgd":
119+
print("Using Stochastic Gradient Descent")
120+
case "adam":
121+
print("Using Adam Optimizer")
122+
case _:
123+
print("Using Default Optimizer")
124+
125+
```
126+
127+
---
128+
129+
Decision-making is key, but to keep our code clean, we shouldn't repeat our logic. We need to wrap our conditionals and loops into reusable components.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
title: Data Structures
3+
sidebar_label: Data Structures
4+
description: "Mastering Python's built-in collections: Lists, Tuples, Dictionaries, and Sets, and their specific roles in data science pipelines."
5+
tags: [python, data-structures, lists, dictionaries, tuples, sets, mathematics-for-ml]
6+
---
7+
8+
While basic types hold single values, **Data Structures** allow us to group, organize, and manipulate large amounts of information. In ML, choosing the wrong structure can lead to code that runs $100\times$ slower than it should.
9+
10+
## 1. Lists: The Versatile Workhorse
11+
12+
A **List** is an ordered, mutable collection of items. Think of it as a flexible array that can grow or shrink.
13+
14+
* **Syntax:** `my_list = [0.1, 0.2, 0.3]`
15+
* **ML Use Case:** Storing the history of "Loss" values during training so you can plot them later.
16+
17+
```python
18+
losses = []
19+
for epoch in range(10):
20+
current_loss = train_step()
21+
losses.append(current_loss) # Dynamic growth
22+
23+
```
24+
25+
## 2. Tuples: The Immutable Safeguard
26+
27+
A **Tuple** is like a list, but it **cannot be changed** after creation (immutable).
28+
29+
* **Syntax:** `shape = (224, 224, 3)`
30+
* **ML Use Case:** Defining image dimensions or model architectures. Since these shouldn't change accidentally during execution, a tuple is safer than a list.
31+
32+
```mermaid
33+
graph LR
34+
L[List: Mutable] --> L_Edit["Can change: my_list[0] = 5"]
35+
T[Tuple: Immutable] --> T_Edit["Error: 'tuple' object does not support assignment"]
36+
style T fill:#fffde7,stroke:#fbc02d,color:#333
37+
38+
```
39+
40+
## 3. Dictionaries: Key-Value Mapping
41+
42+
A **Dictionary** stores data in pairs: a unique **Key** and its associated **Value**. It uses a "Hash Table" internally, making lookups incredibly fast ( complexity).
43+
44+
* **Syntax:** `params = {"learning_rate": 0.001, "batch_size": 32}`
45+
* **ML Use Case:** Managing hyperparameters or mapping integer IDs back to human-readable text labels.
46+
47+
```mermaid
48+
graph TD
49+
Key["Key: 'Cat'"] --> Hash["Hash Function"]
50+
Hash --> Index["Memory Index: 0x42"]
51+
Index --> Val["Value: [0.98, 0.02, ...]"]
52+
style Hash fill:#e1f5fe,stroke:#01579b,color:#333
53+
54+
```
55+
56+
## 4. Sets: Uniqueness and Logic
57+
58+
A **Set** is an unordered collection of **unique** items.
59+
60+
* **Syntax:** `classes = {"dog", "cat", "bird"}`
61+
* **ML Use Case:** Finding the unique labels in a messy dataset or performing mathematical operations like Union and Intersection on feature sets.
62+
63+
## 5. Performance Comparison
64+
65+
Choosing the right structure is about balancing **Speed** and **Memory**.
66+
67+
| Feature | List | Tuple | Dictionary | Set |
68+
| --- | --- | --- | --- | --- |
69+
| **Ordering** | Ordered | Ordered | Ordered (Python 3.7+) | Unordered |
70+
| **Mutable** | **Yes** | No | **Yes** | **Yes** |
71+
| **Duplicates** | Allowed | Allowed | Keys must be unique | Must be unique |
72+
| **Search Speed** | (Slow) | (Slow) | (Very Fast) | (Very Fast) |
73+
74+
```mermaid
75+
xychart-beta
76+
title "Search Speed (Lower is Better)"
77+
x-axis ["List", "Tuple", "Set", "Dict"]
78+
y-axis "Time Complexity" 0 --> 120
79+
bar [100, 100, 5, 5]
80+
81+
```
82+
83+
## 6. Slicing and Indexing
84+
85+
In ML, we often need to "slice" our data (e.g., taking the first 80% for training and the last 20% for testing).
86+
87+
$$
88+
\text{Syntax: } \text{data}[\text{start} : \text{stop} : \text{step}]
89+
$$
90+
91+
```python
92+
data = [10, 20, 30, 40, 50, 60]
93+
train = data[:4] # [10, 20, 30, 40]
94+
test = data[4:] # [50, 60]
95+
96+
```
97+
98+
---
99+
100+
Now that we can organize data, we need to control the flow of our program—making decisions based on that data and repeating tasks efficiently.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Exception Handling
3+
sidebar_label: Exceptions
4+
description: "Learning to handle errors gracefully in Python to build robust and fault-tolerant Machine Learning pipelines."
5+
tags: [python, programming, exceptions, error-handling, debugging, mathematics-for-ml]
6+
---
7+
8+
In Machine Learning, things often go wrong: a dataset file is missing, a GPU runs out of memory, or a feature contains a `NaN` (Not a Number) that crashes a calculation. **Exception Handling** allows your program to "fail gracefully" rather than crashing completely.
9+
10+
## 1. The Try-Except Block
11+
12+
The basic tool for handling errors is the `try...except` block. You "try" a piece of code, and if it raises an error, the "except" block catches it.
13+
14+
```python
15+
try:
16+
# Attempting to load a large dataset
17+
data = load_dataset("huge_data.csv")
18+
except FileNotFoundError:
19+
print("Error: The dataset file was not found. Please check the path.")
20+
21+
```
22+
23+
```mermaid
24+
flowchart TD
25+
Start([Start Try Block]) --> Op[Execute Code]
26+
Op --> Success{Error Occurred?}
27+
Success -- No --> End([Continue Program])
28+
Success -- Yes --> Catch[Match Exception Type]
29+
Catch --> Handle[Execute Except Block]
30+
Handle --> End
31+
style Catch fill:#ffebee,stroke:#c62828,color:#333
32+
33+
```
34+
35+
## 2. Handling Multiple Exceptions
36+
37+
Different operations can fail in different ways. You can catch specific errors to provide tailored solutions.
38+
39+
* **`ValueError`**: Raised when a function receives an argument of the right type but inappropriate value (e.g., trying to take the square root of a negative number).
40+
* **`TypeError`**: Raised when an operation is applied to an object of inappropriate type.
41+
* **`ZeroDivisionError`**: Common in manual normalization logic.
42+
43+
```python
44+
try:
45+
result = total_loss / num_samples
46+
except ZeroDivisionError:
47+
result = 0
48+
print("Warning: num_samples was zero. Setting loss to 0.")
49+
except TypeError:
50+
print("Error: Check if total_loss and num_samples are numbers.")
51+
52+
```
53+
54+
## 3. The Full Lifecycle: `else` and `finally`
55+
56+
To build truly robust pipelines (like those that open and close database connections), we use the extended syntax:
57+
58+
1. **`try`**: The code that might fail.
59+
2. **`except`**: Code that runs only if an error occurs.
60+
3. **`else`**: Code that runs only if **no** error occurs.
61+
4. **`finally`**: Code that runs **no matter what** (perfect for closing files or releasing GPU memory).
62+
63+
```python
64+
try:
65+
file = open("model_weights.bin", "rb")
66+
weights = file.read()
67+
except IOError:
68+
print("Could not read file.")
69+
else:
70+
print("Weights loaded successfully.")
71+
finally:
72+
file.close()
73+
print("File resource released.")
74+
75+
```
76+
77+
## 4. Raising Exceptions
78+
79+
Sometimes, you *want* to stop the program if a specific condition isn't met. For example, if a user provides a negative learning rate.
80+
81+
```python
82+
def set_learning_rate(lr):
83+
if lr <= 0:
84+
raise ValueError(f"Learning rate must be positive. Received: {lr}")
85+
return lr
86+
87+
```
88+
89+
## 5. Exceptions in ML Data Pipelines
90+
91+
In production ML, we use exceptions to ensure data quality.
92+
93+
```mermaid
94+
graph LR
95+
Input[Data Source] --> Check{Check Data}
96+
Check -->|Valid| Train[Start Training]
97+
Check -->|Corrupted| Exc[Raise DataValidationError]
98+
Exc --> Log[Log Error to Dashboard]
99+
Exc --> Fallback[Use Last Known Good Data]
100+
style Exc fill:#ffc107,stroke:#ff8f00,color:#333
101+
102+
```
103+
104+
## 6. Summary of Common ML Exceptions
105+
106+
| Exception | When it happens in ML |
107+
| --- | --- |
108+
| **`IndexError`** | Trying to access a non-existent column or row index in an array. |
109+
| **`KeyError`** | Looking for a hyperparameter in a config dictionary that doesn't exist. |
110+
| **`AttributeError`** | Calling a method (like `.predict()`) on a model that hasn't been trained yet. |
111+
| **`MemoryError`** | Loading a dataset that is larger than the available RAM. |
112+
113+
---
114+
115+
Handling errors ensures your code doesn't crash, but how do we organize our code so it's easy to read and maintain? Let's explore the world of Classes and Objects.

0 commit comments

Comments
 (0)