-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
140 lines (114 loc) · 5.32 KB
/
Copy pathmain.py
File metadata and controls
140 lines (114 loc) · 5.32 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import time
from src.data_loader import run_pipeline
from src.analysis import (
load_clean_data, top_active_users, hourly_engagement,
feature_popularity, device_breakdown, premium_vs_free,
city_engagement, day_of_week_pattern
)
from src.visualize import run_all_plots
from src.ml_pipeline import run_ml_pipeline
BANNER = """
╔══════════════════════════════════════════════════╗
║ AI-Enhanced User Behavior Analysis ║
║ Analytics Pipeline ║
╚══════════════════════════════════════════════════╝
"""
def print_section(title):
print(f"\n{'='*50}")
print(f" {title}")
print(f"{'='*50}")
def print_summary(df):
print_section("FINAL INSIGHTS SUMMARY")
total_sessions = len(df)
premium_pct = round(df['is_premium'].mean() * 100, 1)
avg_duration = round(df['session_duration_mins'].mean(), 2)
top_feature = df['feature_used'].value_counts().idxmax()
top_city = df['location'].value_counts().idxmax()
top_device = df['device_type'].value_counts().idxmax()
peak_hour = df.groupby('hour')['session_id'].count().idxmax()
busiest_day = df.groupby('day_of_week')['session_id'].count().idxmax()
print(f"""
Total Sessions Analysed : {total_sessions:,}
Premium Users : {premium_pct}%
Avg Session Duration : {avg_duration} mins
Most Used Feature : {top_feature}
Top City : {top_city}
Dominant Device : {top_device}
Peak Hour : {peak_hour}:00
Busiest Day : {busiest_day}
""")
def main():
start = time.time()
print(BANNER)
# Step 1 — Generate Data
print_section("STEP 1: Generating Dataset")
import pandas as pd
import numpy as np
from faker import Faker
import random, os
fake = Faker()
np.random.seed(42)
random.seed(42)
NUM_RECORDS = 100_000
DEVICE_TYPES = ['mobile', 'tablet', 'desktop']
DEVICE_WEIGHTS = [0.65, 0.10, 0.25]
FEATURES = ['home_feed', 'search', 'profile', 'notifications',
'messages', 'settings', 'explore', 'checkout']
FEATURE_WEIGHTS = [0.30, 0.20, 0.15, 0.12, 0.10, 0.05, 0.05, 0.03]
AGE_GROUPS = ['18-24', '25-34', '35-44', '45+']
AGE_WEIGHTS = [0.30, 0.35, 0.20, 0.15]
CITIES = ['Mumbai', 'Delhi', 'Bengaluru', 'Hyderabad', 'Chennai',
'Pune', 'Kolkata', 'Ahmedabad', 'Jaipur', 'Surat']
def generate_timestamp():
return fake.date_time_between(start_date='-1y', end_date='now')
def generate_session_duration(feature, is_premium):
base = {'home_feed': 8, 'explore': 10, 'search': 5,
'messages': 12, 'profile': 3, 'notifications': 2,
'settings': 1, 'checkout': 6}
mean = base.get(feature, 5)
if is_premium:
mean *= 1.4
return round(min(max(np.random.exponential(scale=mean), 0.5), 120), 2)
def generate_pages_visited(duration):
return min(int(duration / 2) + np.random.randint(1, 5), 50)
data = {
'user_id': [f"U{str(uid).zfill(6)}" for uid in np.random.randint(100000, 999999, size=NUM_RECORDS)],
'session_id': [f"S{fake.uuid4()[:8].upper()}" for _ in range(NUM_RECORDS)],
'timestamp': [generate_timestamp() for _ in range(NUM_RECORDS)],
'device_type': random.choices(DEVICE_TYPES, weights=DEVICE_WEIGHTS, k=NUM_RECORDS),
'location': random.choices(CITIES, k=NUM_RECORDS),
'feature_used': random.choices(FEATURES, weights=FEATURE_WEIGHTS, k=NUM_RECORDS),
'age_group': random.choices(AGE_GROUPS, weights=AGE_WEIGHTS, k=NUM_RECORDS),
'is_premium': np.random.choice([True, False], size=NUM_RECORDS, p=[0.25, 0.75]),
}
df_raw = pd.DataFrame(data)
df_raw['session_duration_mins'] = df_raw.apply(
lambda row: generate_session_duration(row['feature_used'], row['is_premium']), axis=1)
df_raw['pages_visited'] = df_raw['session_duration_mins'].apply(generate_pages_visited)
df_raw = df_raw.sort_values('timestamp').reset_index(drop=True)
os.makedirs('data/raw', exist_ok=True)
df_raw.to_csv('data/raw/user_behavior.csv', index=False)
print(f" Dataset generated: {df_raw.shape[0]:,} rows x {df_raw.shape[1]} columns")
# Step 2 — Clean Data
print_section("STEP 2: Cleaning Data")
run_pipeline()
# Step 3 — Load Clean Data
print_section("STEP 3: Loading Clean Data")
df = load_clean_data()
print(f" Clean data loaded: {len(df):,} rows")
# Step 4 — Visualizations
print_section("STEP 4: Generating Visualizations")
run_all_plots(df)
# Step 5 — ML Pipeline (Advanced AI Features)
print_section("STEP 5: Running ML Pipeline")
pipeline, models, metrics = run_ml_pipeline(df)
# Step 6 — Summary
print_summary(df)
elapsed = round(time.time() - start, 2)
print(f" Total pipeline completed in {elapsed}s")
print("\n Plots saved to : outputs/plots/")
print(" Clean data at : data/processed/user_behavior_clean.csv")
print(" ML models trained : Random Forest, K-Means, Isolation Forest")
print(" ML metrics saved : Integrated in pipeline output\n")
if __name__ == "__main__":
main()