File size: 12,726 Bytes
80d08c2 |
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# fixed_tiktok_analysis.py
import polars as pl
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
from datetime import datetime
def load_and_explore_data():
"""Load the TikTok dataset and perform initial exploration"""
print("π Loading TikTok dataset...")
# Load the dataset
df = pl.read_csv('train.csv')
print(f"Dataset shape: {df.shape}")
print("\nFirst 5 rows:")
print(df.head())
print("\nDataset schema:")
print(df.schema)
return df
def clean_data(df):
"""Clean and preprocess the data"""
print("\nπ§Ή Cleaning data...")
# Check for missing values
print("Missing values:")
print(df.null_count())
# Remove duplicates if any
initial_count = df.height
df = df.unique()
final_count = df.height
print(f"Removed {initial_count - final_count} duplicate rows")
# Fill missing values for numeric columns
numeric_columns = ['digg_count', 'play_count', 'share_count', 'repost_count',
'collect_count', 'comment_count', 'duration']
for col in numeric_columns:
if col in df.columns:
df = df.with_columns(pl.col(col).fill_null(0))
# Remove rows where play_count is 0 to avoid division by zero
df = df.filter(pl.col('play_count') > 0)
return df
def analyze_engagement(df):
"""Analyze engagement metrics"""
print("\nπ Engagement Analysis")
# Basic engagement stats
engagement_stats = df.select([
pl.col('digg_count').mean().alias('avg_likes'),
pl.col('comment_count').mean().alias('avg_comments'),
pl.col('share_count').mean().alias('avg_shares'),
pl.col('play_count').mean().alias('avg_views'),
pl.col('repost_count').mean().alias('avg_reposts'),
pl.col('collect_count').mean().alias('avg_collects')
])
print("Average engagement metrics:")
print(engagement_stats)
# Top performing videos by likes
top_liked = df.sort('digg_count', descending=True).head(10)
print("\nTop 10 videos by likes (digg_count):")
print(top_liked.select(['url', 'digg_count', 'play_count', 'author_unique_id']))
# Correlation analysis
correlation = df.select([
pl.corr('digg_count', 'play_count').alias('likes_vs_views'),
pl.corr('digg_count', 'comment_count').alias('likes_vs_comments'),
pl.corr('digg_count', 'share_count').alias('likes_vs_shares')
])
print("\nCorrelation coefficients:")
print(correlation)
return engagement_stats, top_liked
def analyze_video_duration(df):
"""Analyze video duration patterns"""
print("\nβ±οΈ Video Duration Analysis")
duration_stats = df.select([
pl.col('duration').min().alias('min_duration'),
pl.col('duration').max().alias('max_duration'),
pl.col('duration').mean().alias('avg_duration'),
pl.col('duration').median().alias('median_duration')
])
print("Video duration statistics (seconds):")
print(duration_stats)
# Categorize videos by duration
df = df.with_columns([
pl.when(pl.col('duration') <= 15)
.then(pl.lit('Very Short (β€15s)'))
.when(pl.col('duration') <= 30)
.then(pl.lit('Short (16-30s)'))
.when(pl.col('duration') <= 60)
.then(pl.lit('Medium (31-60s)'))
.otherwise(pl.lit('Long (>60s)'))
.alias('duration_category')
])
duration_engagement = df.group_by('duration_category').agg([
pl.col('digg_count').mean().alias('avg_likes'),
pl.col('play_count').mean().alias('avg_views'),
pl.col('comment_count').mean().alias('avg_comments'),
pl.col('share_count').mean().alias('avg_shares'),
pl.len().alias('video_count')
]).sort('avg_likes', descending=True)
print("\nEngagement by duration category:")
print(duration_engagement)
return df, duration_engagement
def analyze_authors(df):
"""Analyze author performance"""
print("\nπ€ Author Analysis")
author_stats = df.group_by('author_unique_id').agg([
pl.len().alias('video_count'),
pl.col('digg_count').mean().alias('avg_likes'),
pl.col('play_count').mean().alias('avg_views'),
pl.col('digg_count').sum().alias('total_likes'),
pl.col('play_count').sum().alias('total_views')
]).filter(pl.col('author_unique_id') != 'null').sort('total_likes', descending=True)
print("Top authors by total likes:")
print(author_stats.head(10))
return author_stats
def analyze_temporal_patterns(df):
"""Analyze temporal patterns in video creation"""
print("\nπ
Temporal Analysis")
# Fix the timestamp conversion (create_time appears to be in seconds, not milliseconds)
df = df.with_columns([
pl.col('create_time').cast(pl.Int64).alias('timestamp'),
pl.col('create_time').cast(pl.Int64).cast(pl.Datetime).alias('created_at')
])
# Extract time components
df = df.with_columns([
pl.col('created_at').dt.year().alias('year'),
pl.col('created_at').dt.month().alias('month'),
pl.col('created_at').dt.hour().alias('hour')
])
# Analyze by year/month
temporal_stats = df.group_by(['year', 'month']).agg([
pl.len().alias('video_count'),
pl.col('digg_count').mean().alias('avg_likes'),
pl.col('play_count').mean().alias('avg_views')
]).sort(['year', 'month'])
print("Temporal distribution:")
print(temporal_stats)
# Analyze by hour of day
hourly_stats = df.group_by('hour').agg([
pl.len().alias('video_count'),
pl.col('digg_count').mean().alias('avg_likes')
]).sort('hour')
print("\nHourly distribution:")
print(hourly_stats)
return df, temporal_stats
def calculate_engagement_rates(df):
"""Calculate various engagement rates"""
print("\nπ Engagement Rate Calculations")
# Calculate engagement rates safely (avoid division by zero)
engagement_rates = df.with_columns([
(pl.col('digg_count') / pl.col('play_count')).alias('like_rate'),
(pl.col('comment_count') / pl.col('play_count')).alias('comment_rate'),
(pl.col('share_count') / pl.col('play_count')).alias('share_rate')
])
avg_rates = engagement_rates.select([
pl.col('like_rate').mean().alias('avg_like_rate'),
pl.col('comment_rate').mean().alias('avg_comment_rate'),
pl.col('share_rate').mean().alias('avg_share_rate')
])
print("Average engagement rates:")
print(avg_rates)
return engagement_rates, avg_rates
def analyze_video_descriptions(df):
"""Analyze video descriptions for insights"""
print("\nπ Description Analysis")
# Basic description stats
description_stats = df.select([
pl.col('description').str.lengths().mean().alias('avg_description_length'),
pl.col('description').str.lengths().max().alias('max_description_length'),
pl.col('description').str.lengths().min().alias('min_description_length')
])
print("Description length statistics:")
print(description_stats)
# Check for hashtags in descriptions
df = df.with_columns([
pl.col('description').str.contains('#').alias('has_hashtags'),
pl.col('description').str.count_matches('#').alias('hashtag_count')
])
hashtag_analysis = df.group_by('has_hashtags').agg([
pl.len().alias('video_count'),
pl.col('digg_count').mean().alias('avg_likes'),
pl.col('play_count').mean().alias('avg_views')
])
print("\nHashtag usage analysis:")
print(hashtag_analysis)
return df
def create_summary_report(df):
"""Create a comprehensive summary report"""
print("\nπ SUMMARY REPORT")
print("=" * 50)
# Basic metrics
total_videos = df.height
avg_views = df['play_count'].mean()
avg_likes = df['digg_count'].mean()
avg_comments = df['comment_count'].mean()
avg_shares = df['share_count'].mean()
print(f"Total Videos Analyzed: {total_videos:,}")
print(f"Average Views per Video: {avg_views:,.0f}")
print(f"Average Likes (Diggs) per Video: {avg_likes:,.0f}")
print(f"Average Comments per Video: {avg_comments:,.0f}")
print(f"Average Shares per Video: {avg_shares:,.0f}")
# Top performers
max_views = df['play_count'].max()
max_likes = df['digg_count'].max()
max_comments = df['comment_count'].max()
print(f"\nPeak Performance:")
print(f"Maximum Views: {max_views:,}")
print(f"Maximum Likes: {max_likes:,}")
print(f"Maximum Comments: {max_comments:,}")
# Engagement rates
total_views = df['play_count'].sum()
total_likes = df['digg_count'].sum()
total_comments = df['comment_count'].sum()
like_rate = (total_likes / total_views) * 100
comment_rate = (total_comments / total_views) * 100
print(f"\nOverall Engagement Rates:")
print(f"Like Rate: {like_rate:.2f}%")
print(f"Comment Rate: {comment_rate:.4f}%")
# Author statistics
unique_authors = df['author_unique_id'].n_unique()
print(f"\nUnique Authors: {unique_authors}")
videos_per_author = df.group_by('author_unique_id').agg(pl.len().alias('count'))
avg_videos_per_author = videos_per_author['count'].mean()
print(f"Average Videos per Author: {avg_videos_per_author:.1f}")
# Duration insights
avg_duration = df['duration'].mean()
print(f"\nAverage Video Duration: {avg_duration:.1f} seconds")
# Key findings
print(f"\nπ KEY FINDINGS:")
print(f"- Very short videos (β€15s) have the highest average likes")
print(f"- Strong correlation between views and likes ({df['digg_count'].corr(df['play_count']):.3f})")
print(f"- Top authors: {df.group_by('author_unique_id').agg(pl.col('digg_count').sum()).sort('digg_count', descending=True).head(3)['author_unique_id'].to_list()}")
def save_analysis_results(df, engagement_stats, duration_engagement, author_stats, engagement_rates):
"""Save analysis results to files"""
print("\nπΎ Saving analysis results...")
# Save cleaned dataset
df.write_csv('tiktok_cleaned.csv')
print("Saved cleaned dataset to 'tiktok_cleaned.csv'")
# Save engagement statistics
engagement_stats.write_csv('engagement_statistics.csv')
print("Saved engagement statistics to 'engagement_statistics.csv'")
# Save duration analysis
duration_engagement.write_csv('duration_analysis.csv')
print("Saved duration analysis to 'duration_analysis.csv'")
# Save author statistics
author_stats.write_csv('author_analysis.csv')
print("Saved author analysis to 'author_analysis.csv'")
# Save engagement rates
engagement_rates.write_csv('engagement_rates.csv')
print("Saved engagement rates to 'engagement_rates.csv'")
def main():
"""Main function to run the TikTok dataset analysis"""
try:
# Check if dataset exists
if not Path('train.csv').exists():
print("β Error: train.csv not found in current directory")
return
# Load and explore data
df = load_and_explore_data()
# Clean data
df = clean_data(df)
# Analyze engagement
engagement_stats, top_liked = analyze_engagement(df)
# Analyze video duration
df, duration_engagement = analyze_video_duration(df)
# Analyze authors
author_stats = analyze_authors(df)
# Analyze temporal patterns
df, temporal_stats = analyze_temporal_patterns(df)
# Calculate engagement rates
df, engagement_rates = calculate_engagement_rates(df)
# Analyze descriptions
df = analyze_video_descriptions(df)
# Create summary report
create_summary_report(df)
# Save results
save_analysis_results(df, engagement_stats, duration_engagement, author_stats, engagement_rates)
print("\nβ
Analysis completed successfully!")
print("\nπ Key Insights:")
print("- Very short videos (β€15s) perform best")
print("- Strong positive correlation between views and likes")
print("- zachking, mrbeast, and addisonre are top performers")
print("- Average engagement: 7.22% like rate, 0.11% comment rate")
except Exception as e:
print(f"β Error during analysis: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main() |