💚
Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! 🚀
Simple AI Prediction Example
Let us build a complete, simple AI prediction example from scratch. This will show you the full cycle: data, learning, and prediction.
15 min•By Priygop Team•Updated 2026
Building a Simple Predictor Step by Step
We are going to build a simple predictor that estimates how long it takes to deliver a package, based on distance and weight.
This is a complete example of the AI learning process at a beginner level:
1. Create training data
2. Find a pattern
3. Use the pattern to predict
4. Evaluate accuracy
Diagram
Loading diagram…
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
Complete Working Example
Complete Working Example
# Simple delivery time predictor
# This illustrates the full AI prediction cycle
# Step 1: Training data (distance_km, weight_kg) -> delivery_days
training_data = [
((10, 1), 1), # 10km, 1kg -> 1 day
((50, 2), 2), # 50km, 2kg -> 2 days
((100, 5), 3), # 100km, 5kg -> 3 days
((200, 10), 5), # 200km, 10kg -> 5 days
((500, 20), 8), # 500km, 20kg -> 8 days
((30, 1), 1),
((80, 3), 2),
((150, 7), 4),
]
# Step 2: Find patterns (simplified)
# Pattern 1: days roughly = distance / 70
# Pattern 2: each 10kg adds about 1 day
# Real ML would find these automatically
def train_model(data):
"""Simplified training: find average relationships"""
# In real ML, complex math finds the best formula
# Here we use averages to illustrate
distance_rates = []
weight_rates = []
for (distance, weight), days in data:
distance_rates.append(days / distance)
weight_rates.append(days / max(weight, 1))
avg_distance_rate = sum(distance_rates) / len(distance_rates)
avg_weight_rate = sum(weight_rates) / len(weight_rates)
return avg_distance_rate, avg_weight_rate
dist_rate, weight_rate = train_model(training_data)
# Step 3: Make predictions
def predict_delivery_days(distance_km, weight_kg, dist_rate, weight_rate):
days = (distance_km * dist_rate * 0.5) + (weight_kg * weight_rate * 0.5)
return round(max(1, days))
# Step 4: Test on new deliveries
new_deliveries = [
(75, 4), # 75km, 4kg
(300, 15), # 300km, 15kg
(20, 0.5), # 20km, 0.5kg
]
print("Delivery Time Predictions:")
print("-" * 40)
for distance, weight in new_deliveries:
days = predict_delivery_days(distance, weight, dist_rate, weight_rate)
print(f" {distance}km, {weight}kg -> Estimated {days} day(s)")Try It Yourself
Try It YourselfHTML
HTML Editor
✓ ValidTab = 2 spaces
HTML|27 lines|874 chars|✓ Valid syntax
UTF-8