3/10/26, 10:49 PM Untitled32.
ipynb - Colab
# Step 1: Import Required Libraries
import pandas as pd
import numpy as np
import [Link] as plt
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder
from [Link] import StandardScaler
from [Link] import Pipeline
from [Link] import RandomForestRegressor
from [Link] import mean_absolute_error, r2_score
import joblib
# Step 2: Load Dataset
data = pd.read_csv("amazon_delivery.csv")
print("Dataset Loaded Successfully")
print([Link]())
Dataset Loaded Successfully
Order_ID Agent_Age Agent_Rating Store_Latitude Store_Longitude \
0 ialx566343618 37 4.9 22.745049 75.892471
1 akqg208421122 34 4.5 12.913041 77.683237
2 njpu434582536 23 4.4 12.914264 77.678400
3 rjto796129700 38 4.7 11.003669 76.976494
4 zguw716275638 32 4.6 12.972793 80.249982
Drop_Latitude Drop_Longitude Order_Date Order_Time Pickup_Time \
0 22.765049 75.912471 2022-03-19 11:30:00 11:45:00
1 13.043041 77.813237 2022-03-25 19:45:00 19:50:00
2 12.924264 77.688400 2022-03-19 08:30:00 08:45:00
3 11.053669 77.026494 2022-04-05 18:00:00 18:10:00
4 13.012793 80.289982 2022-03-26 13:30:00 13:45:00
Weather Traffic Vehicle Area Delivery_Time \
0 Sunny High motorcycle Urban 120
1 Stormy Jam scooter Metropolitian 165
2 Sandstorms Low motorcycle Urban 130
3 Sunny Medium motorcycle Metropolitian 105
4 Cloudy High scooter Metropolitian 150
Category
0 Clothing
1 Electronics
2 Sports
3 Cosmetics
4 Toys
[Link]
(43739, 16)
# Step 4: Check Missing Values
print("Missing Values in Dataset")
print([Link]().sum())
Missing Values in Dataset
Order_ID 0
Agent_Age 0
Agent_Rating 54
Store_Latitude 0
Store_Longitude 0
Drop_Latitude 0
Drop_Longitude 0
Order_Date 0
Order_Time 0
Pickup_Time 0
Weather 91
Traffic 0
Vehicle 0
[Link] 1/5
3/10/26, 10:49 PM [Link] - Colab
Area 0
Delivery_Time 0
Category 0
dtype: int64
# Step 5: Handle Missing Values
# Numeric columns
numeric_cols = data.select_dtypes(include=['int64','float64']).columns
data[numeric_cols] = data[numeric_cols].fillna(data[numeric_cols].mean())
# Categorical columns
categorical_cols = data.select_dtypes(include=['object']).columns
for col in categorical_cols:
data[col] = data[col].fillna(data[col].mode()[0])
print("Missing values handled")
print([Link]().sum())
Missing values handled
Order_ID 0
Agent_Age 0
Agent_Rating 0
Store_Latitude 0
Store_Longitude 0
Drop_Latitude 0
Drop_Longitude 0
Order_Date 0
Order_Time 0
Pickup_Time 0
Weather 0
Traffic 0
Vehicle 0
Area 0
Delivery_Time 0
Category 0
dtype: int64
# Step 6: Feature Engineering
# Create Age Group Feature
data['Age_Group'] = [Link](
data['Agent_Age'],
bins=[18,25,35,50],
labels=['Young','Adult','Senior']
)
# Convert Traffic category to numerical level
traffic_map = {
"Low":1,
"Medium":2,
"High":3,
"Jam":4
}
data['Traffic_Level'] = data['Traffic'].map(traffic_map)
print([Link]())
Order_ID Agent_Age Agent_Rating Store_Latitude Store_Longitude \
0 ialx566343618 37 4.9 22.745049 75.892471
1 akqg208421122 34 4.5 12.913041 77.683237
2 njpu434582536 23 4.4 12.914264 77.678400
3 rjto796129700 38 4.7 11.003669 76.976494
4 zguw716275638 32 4.6 12.972793 80.249982
Drop_Latitude Drop_Longitude Order_Date Order_Time Pickup_Time \
0 22.765049 75.912471 2022-03-19 11:30:00 11:45:00
1 13.043041 77.813237 2022-03-25 19:45:00 19:50:00
2 12.924264 77.688400 2022-03-19 08:30:00 08:45:00
3 11.053669 77.026494 2022-04-05 18:00:00 18:10:00
4 13.012793 80.289982 2022-03-26 13:30:00 13:45:00
[Link] 2/5
3/10/26, 10:49 PM [Link] - Colab
Weather Traffic Vehicle Area Delivery_Time \
0 Sunny High motorcycle Urban 120
1 Stormy Jam scooter Metropolitian 165
2 Sandstorms Low motorcycle Urban 130
3 Sunny Medium motorcycle Metropolitian 105
4 Cloudy High scooter Metropolitian 150
Category Age_Group Traffic_Level
0 Clothing Senior NaN
1 Electronics Adult NaN
2 Sports Young NaN
3 Cosmetics Senior NaN
4 Toys Adult NaN
# Step 7: Select Important Features
features = [
'Agent_Age',
'Agent_Rating',
'Traffic_Level'
]
X = data[features]
y = data['Delivery_Time']
# Step 8: Split Dataset
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
print("Training Data Size:", X_train.shape)
print("Testing Data Size:", X_test.shape)
Training Data Size: (34991, 3)
Testing Data Size: (8748, 3)
# Step 9: Create ML Pipeline
pipeline = Pipeline([
('scaler', StandardScaler()),
('model', RandomForestRegressor(n_estimators=100))
])
# Step 10: Train Model
[Link](X_train, y_train)
print("Model Training Completed")
/usr/local/lib/python3.12/dist-packages/sklearn/utils/[Link]: RuntimeWarning: invalid value enc
updated_mean = (last_sum + new_sum) / updated_sample_count
/usr/local/lib/python3.12/dist-packages/sklearn/utils/[Link]: RuntimeWarning: invalid value enc
T = new_sum / new_sample_count
/usr/local/lib/python3.12/dist-packages/sklearn/utils/[Link]: RuntimeWarning: invalid value enc
new_unnormalized_variance -= correction**2 / new_sample_count
Model Training Completed
# Step 11: Evaluate Model
predictions = [Link](X_test)
mae = mean_absolute_error(y_test, predictions)
r2 = r2_score(y_test, predictions)
print("Mean Absolute Error:", mae)
[Link] 3/5
3/10/26, 10:49 PM [Link] - Colab
print( Mean Absolute Error: , mae)
print("R2 Score:", r2)
Mean Absolute Error: 34.804068964512744
R2 Score: 0.19591032364467642
# Step 12: Visualization
[Link](y_test, predictions)
[Link]("Actual Delivery Time")
[Link]("Predicted Delivery Time")
[Link]("Actual vs Predicted Delivery Time")
[Link]()
# Step 13: Save Model
[Link](pipeline, "delivery_prediction_model.pkl")
print("Model saved successfully")
Model saved successfully
# Step 14: Load Model and Predict
model = [Link]("delivery_prediction_model.pkl")
sample_data = [[30,4.5,3]]
prediction = [Link](sample_data)
print("Predicted Delivery Time:", prediction)
Predicted Delivery Time: [127.56288703]
/usr/local/lib/python3.12/dist-packages/sklearn/utils/[Link]: UserWarning: X does not have v
[Link](
[Link] 4/5
3/10/26, 10:49 PM [Link] - Colab
[Link] 5/5