-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataGenerator.py
155 lines (128 loc) · 6.29 KB
/
dataGenerator.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import random
from faker import Faker
import uuid
def main():
# Initialize
fake = Faker()
np.random.seed(42)
random.seed(42)
# Constants
NUM_POSTS = 1000
BASE_DATE = datetime(2024, 1, 1)
# Define categories and options
GENRES = ['fashion', 'food', 'travel', 'tech', 'lifestyle', 'fitness', 'beauty', 'gaming']
POST_TYPES = ['image', 'video', 'carousel', 'reel']
HASHTAGS = {
'fashion': ['#ootd', '#style', '#fashion', '#trendy', '#fashionista'],
'food': ['#foodie', '#cooking', '#recipe', '#tasty', '#foodporn'],
'travel': ['#wanderlust', '#travel', '#explore', '#adventure', '#travelgram'],
'tech': ['#tech', '#gadgets', '#innovation', '#coding', '#ai'],
'lifestyle': ['#lifestyle', '#life', '#inspiration', '#motivation', '#mindfulness'],
'fitness': ['#fitness', '#workout', '#gym', '#health', '#wellness'],
'beauty': ['#beauty', '#skincare', '#makeup', '#glam', '#selfcare'],
'gaming': ['#gaming', '#gamer', '#esports', '#streamer', '#gameplay']
}
AGE_GROUPS = ['13-17', '18-24', '25-34', '35-44', '45-54', '55-90']
GENDERS = ['male', 'female', 'other']
REGIONS = ['North America', 'Europe', 'Asia', 'South America', 'Africa', 'Oceania']
DEVICES = ['mobile', 'desktop', 'tablet']
print("Generating social media analytics dataset...")
posts = []
for _ in range(NUM_POSTS):
# Basic post info
genre = random.choice(GENRES)
post_date = BASE_DATE + timedelta(
days=random.randint(0, 90),
hours=random.randint(0, 23),
minutes=random.randint(0, 59)
)
# Generate engagement metrics
base_engagement = random.randint(1000, 10000)
likes = int(base_engagement * random.uniform(0.4, 0.8))
comments = int(base_engagement * random.uniform(0.05, 0.2))
shares = int(base_engagement * random.uniform(0.02, 0.1))
saves = int(base_engagement * random.uniform(0.01, 0.05))
# Generate sentiment distributions
sentiment_total = likes + comments
positive_pct = random.uniform(0.4, 0.8)
negative_pct = random.uniform(0.05, 0.2)
neutral_pct = 1 - (positive_pct + negative_pct)
# Generate user demographics
age_distribution = {group: random.uniform(0, 1) for group in AGE_GROUPS}
total = sum(age_distribution.values())
age_distribution = {k: round(v/total * 100, 1) for k, v in age_distribution.items()}
gender_distribution = {gender: random.uniform(0, 1) for gender in GENDERS}
total = sum(gender_distribution.values())
gender_distribution = {k: round(v/total * 100, 1) for k, v in gender_distribution.items()}
region_distribution = {region: random.uniform(0, 1) for region in REGIONS}
total = sum(region_distribution.values())
region_distribution = {k: round(v/total * 100, 1) for k, v in region_distribution.items()}
# Select and track hashtags
num_hashtags = random.randint(3, 8)
selected_hashtags = random.sample(HASHTAGS[genre] + random.sample([tag for tags in HASHTAGS.values() for tag in tags], 10), num_hashtags)
hashtag_performance = {tag: random.randint(50, 100) for tag in selected_hashtags}
# Device distribution
device_distribution = {device: random.uniform(0, 1) for device in DEVICES}
total = sum(device_distribution.values())
device_distribution = {k: round(v/total * 100, 1) for k, v in device_distribution.items()}
# Compile post data
post_data = {
'post_id': str(uuid.uuid4()),
'posted_at': post_date,
'genre': genre,
'post_type': random.choice(POST_TYPES),
'hashtags': ', '.join(selected_hashtags),
'hashtag_reach_score': sum(hashtag_performance.values()),
# Engagement metrics
'total_engagements': base_engagement,
'likes': likes,
'comments': comments,
'shares': shares,
'saves': saves,
'engagement_rate': round((likes + comments + shares + saves) / base_engagement * 100, 2),
# Sentiment analysis
'positive_sentiment_pct': round(positive_pct * 100, 1),
'neutral_sentiment_pct': round(neutral_pct * 100, 1),
'negative_sentiment_pct': round(negative_pct * 100, 1),
# Demographics - Age
**{f'age_{age.replace("-", "_")}_pct': pct
for age, pct in age_distribution.items()},
# Demographics - Gender
**{f'{gender}_pct': pct
for gender, pct in gender_distribution.items()},
# Demographics - Region
**{f'region_{region.lower().replace(" ", "_")}_pct': pct
for region, pct in region_distribution.items()},
# Device distribution
**{f'device_{device}_pct': pct
for device, pct in device_distribution.items()},
# Time metrics
'avg_view_duration': random.randint(5, 300),
'completion_rate': random.uniform(0.3, 0.9)
}
posts.append(post_data)
# Convert to DataFrame and save
df = pd.DataFrame(posts)
# Reorder columns for better readability
column_order = [
'post_id', 'posted_at', 'genre', 'post_type', 'hashtags', 'hashtag_reach_score',
'total_engagements', 'likes', 'comments', 'shares', 'saves', 'engagement_rate',
'positive_sentiment_pct', 'neutral_sentiment_pct', 'negative_sentiment_pct',
'avg_view_duration', 'completion_rate'
] + [col for col in df.columns if col.startswith(('age_', 'gender_', 'region_', 'device_'))]
df = df[column_order]
# Save to CSV
output_file = 'social_media_analytics.csv'
df.to_csv(output_file, index=False)
# Print summary
print("\nDataset generation complete!")
print(f"File saved as: {output_file}")
print(f"Total records: {len(df)}")
print("\nColumns in dataset:")
for col in df.columns:
print(f"- {col}")
if __name__ == "__main__":
main()