-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimdbDataAnalysis.py
More file actions
40 lines (32 loc) · 1.33 KB
/
Copy pathimdbDataAnalysis.py
File metadata and controls
40 lines (32 loc) · 1.33 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
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Load the dataset
df = pd.read_csv('../data/cleanedTitles.csv')
# Convert to numeric and drop NaNs
df['averageRating'] = pd.to_numeric(df['averageRating'], errors='coerce')
ratings = df['averageRating'].dropna()
# Define 0.5-wide bins
bin_edges = np.arange(0, 10.5, 0.5) # From 0 to 10 (inclusive) with step of 0.5
bin_labels = [f"{b:.1f}–{b+0.5:.1f}" for b in bin_edges[:-1]] # Create labels like 8.0–8.5
# Bin the data
binned = pd.cut(ratings, bins=bin_edges, labels=bin_labels, right=False)
bin_percentages = binned.value_counts(normalize=True).sort_index() * 100
# Plot the bar chart
plt.figure(figsize=(12, 6))
bars = plt.bar(bin_percentages.index, bin_percentages.values, color='cornflowerblue', edgecolor='black')
# Standard deviation annotation
std_dev = ratings.std()
plt.text(0.95, 0.95, f"Std Dev: {std_dev:.2f}", fontsize=12,
rotation=45, transform=plt.gca().transAxes,
verticalalignment='top', horizontalalignment='right',
bbox=dict(facecolor='white', alpha=0.7))
# Labels and title
plt.xlabel('Average Rating Bins')
plt.ylabel('Percentage of Titles (%)')
plt.title('Distribution of IMDb Ratings (Grouped in 0.5 Intervals)')
plt.xticks(rotation=45)
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.tight_layout()
# Show the plot
plt.show()