-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
116 lines (89 loc) · 3.42 KB
/
Copy pathexamples.py
File metadata and controls
116 lines (89 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Example: Using the Linear Regression Model
===========================================
This script demonstrates how to use individual components of the pipeline.
"""
import numpy as np
from src.linear_regression import LinearRegression
from src.data_ingestion import fetch_data
from src.data_preprocessing import preprocess_data
from src.model_training import train_model
from src.model_evaluation import evaluate_model
from src.prediction import predict_single
# Suppress warnings
import warnings
warnings.filterwarnings('ignore')
def example_basic_usage():
"""Example: Basic usage of LinearRegression class"""
print("\n" + "="*80)
print("EXAMPLE 1: Basic Linear Regression Usage")
print("="*80 + "\n")
# Create simple dataset
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([2, 4, 6, 8, 10])
# Create and train model
model = LinearRegression(learning_rate=0.1, n_iterations=1000)
model.fit(X, y)
# Make predictions
predictions = model.predict(X)
print("Training Data:")
for i in range(len(X)):
print(f" X={X[i][0]}, y={y[i]}, predicted={predictions[i]:.2f}")
print(f"\nModel Parameters:")
print(f" Weight: {model.weights[0]:.4f}")
print(f" Bias: {model.bias:.4f}")
def example_full_pipeline():
"""Example: Using the full pipeline"""
print("\n" + "="*80)
print("EXAMPLE 2: Full Pipeline with Boston Housing Data")
print("="*80 + "\n")
# Load data
print("1. Loading data...")
data = fetch_data()
print(f" Loaded {len(data)} samples\n")
# Preprocess
print("2. Preprocessing data...")
X_train, X_test, y_train, y_test, scaler = preprocess_data(data, test_size=0.2)
print(f" Training: {len(X_train)}, Testing: {len(X_test)}\n")
# Train
print("3. Training model...")
model = train_model(X_train, y_train, learning_rate=0.01, n_iterations=1000)
# Evaluate
print("\n4. Evaluating model...")
train_metrics, test_metrics, _, _ = evaluate_model(
model, X_train, y_train, X_test, y_test
)
# Single prediction
print("\n5. Making single prediction...")
sample_features = X_test[0]
prediction = predict_single(model, scaler, scaler.inverse_transform([sample_features])[0])
actual = y_test.iloc[0]
print(f" Predicted: {prediction:.2f}")
print(f" Actual: {actual:.2f}")
def example_hyperparameter_tuning():
"""Example: Testing different hyperparameters"""
print("\n" + "="*80)
print("EXAMPLE 3: Comparing Different Learning Rates")
print("="*80 + "\n")
# Load and preprocess data
data = fetch_data()
X_train, X_test, y_train, y_test, scaler = preprocess_data(data, test_size=0.2)
learning_rates = [0.001, 0.01, 0.1]
print("Testing different learning rates:\n")
for lr in learning_rates:
model = LinearRegression(learning_rate=lr, n_iterations=1000)
model.fit(X_train, y_train)
# Test predictions
y_pred = model.predict(X_test)
mse = np.mean((y_test - y_pred) ** 2)
print(f"Learning Rate: {lr}")
print(f" Final Cost: {model.cost_history[-1]:.4f}")
print(f" Test MSE: {mse:.4f}\n")
if __name__ == "__main__":
# Run examples
example_basic_usage()
example_full_pipeline()
example_hyperparameter_tuning()
print("\n" + "="*80)
print("All examples completed!")
print("="*80 + "\n")