-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt4o_output.py
More file actions
214 lines (164 loc) · 6.56 KB
/
chatgpt4o_output.py
File metadata and controls
214 lines (164 loc) · 6.56 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
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
# === Imports ===
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score, davies_bouldin_score
from collections import Counter
import itertools
# === Load the dataset ===
file_path = 'datasets/IMDB-Movies.csv'
df = pd.read_csv(file_path)
# Display basic info and first few rows to inspect column names and types
print(df.info())
print(df.head())
# === Rename for clarity ===
df = df.rename(columns={"Unnamed: 0": "Ranking"})
# Select numerical columns (excluding Year)
numerical_df = df[["Ranking", "Runtime..Minutes.", "Rating",
"Votes", "Revenue..Millions."]]
# Compute correlation matrix
correlation_matrix = numerical_df.corr()
# Plot correlation heatmap
plt.figure(figsize=(8, 6))
sns.heatmap(correlation_matrix, annot=True, cmap="coolwarm", fmt=".2f")
plt.title("Correlation Matrix of Numerical Features")
plt.tight_layout()
plt.show()
# Show correlations with Rating
print("Correlation with Rating:")
print(correlation_matrix["Rating"].sort_values(ascending=False))
# === Imputation and Standardization ===
# Drop Ranking and Year, and prepare numerical features
features = ["Runtime..Minutes.", "Rating", "Votes", "Revenue..Millions."]
X = df[features].copy()
# Impute missing values in Revenue using median (robust to skew)
imputer = SimpleImputer(strategy="median")
X_imputed = imputer.fit_transform(X)
# Standardize the numerical features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X_imputed)
# Convert back to DataFrame for reference
X_scaled_df = pd.DataFrame(X_scaled, columns=features)
print("Scaled Numerical Features (First 5 Rows):")
print(X_scaled_df.head())
# === Perform PCA ===
pca = PCA()
X_pca = pca.fit_transform(X_scaled)
# Explained variance
explained_variance_ratio = pca.explained_variance_ratio_
cumulative_variance = np.cumsum(explained_variance_ratio)
# Component loadings
loadings = pd.DataFrame(pca.components_.T,
columns=[f'PC{i+1}'
for i in range(len(features))], index=features)
print("Explained Variance Ratio:")
print(explained_variance_ratio)
print("Cumulative Explained Variance:")
print(cumulative_variance)
print("PCA Component Loadings:")
print(loadings)
# 2D PCA projection
plt.figure(figsize=(8, 6))
plt.scatter(X_pca[:, 0], X_pca[:, 1], c='steelblue', edgecolor='k', s=60)
plt.xlabel("Principal Component 1 (44.3%)")
plt.ylabel("Principal Component 2 (28.8%)")
plt.title("2D PCA Projection of IMDB Movies (Numerical Features)")
plt.grid(True)
plt.tight_layout()
plt.show()
# === KMeans Clustering ===
# Try different values of k for K-Means
k_values = range(2, 8)
silhouette_scores = []
db_scores = []
for k in k_values:
kmeans = KMeans(n_clusters=k, n_init=10, random_state=42)
labels = kmeans.fit_predict(X_scaled)
silhouette_scores.append(silhouette_score(X_scaled, labels))
db_scores.append(davies_bouldin_score(X_scaled, labels))
# Plot silhouette and Davies-Bouldin scores
fig, ax1 = plt.subplots(figsize=(10, 5))
ax2 = ax1.twinx()
ax1.plot(k_values, silhouette_scores, 'bo-', label='Silhouette Score')
ax2.plot(k_values, db_scores, 'rs-', label='Davies-Bouldin Index')
ax1.set_xlabel("Number of Clusters (k)")
ax1.set_ylabel("Silhouette Score", color='b')
ax2.set_ylabel("Davies-Bouldin Index", color='r')
plt.title("K-Means Clustering Evaluation")
fig.tight_layout()
plt.show()
print("Silhouette Scores by k:")
print(silhouette_scores)
print("Davies-Bouldin Scores by k:")
print(db_scores)
# Run KMeans with k=2 and k=5
kmeans_2 = KMeans(n_clusters=2, n_init=10, random_state=42).fit(X_scaled)
kmeans_5 = KMeans(n_clusters=5, n_init=10, random_state=42).fit(X_scaled)
# Add cluster labels to PCA data for plotting
df["Cluster_k2"] = kmeans_2.labels_
df["Cluster_k5"] = kmeans_5.labels_
df["PC1"] = X_pca[:, 0]
df["PC2"] = X_pca[:, 1]
# Plot PCA with k=2
plt.figure(figsize=(8, 6))
sns.scatterplot(data=df, x="PC1", y="PC2", hue="Cluster_k2", palette="Set1", s=80)
plt.title("PCA Projection with K-Means Clustering (k=2)")
plt.grid(True)
plt.tight_layout()
plt.show()
# Plot PCA with k=5
plt.figure(figsize=(8, 6))
sns.scatterplot(data=df, x="PC1", y="PC2", hue="Cluster_k5", palette="Set2", s=80)
plt.title("PCA Projection with K-Means Clustering (k=5)")
plt.grid(True)
plt.tight_layout()
plt.show()
# Group summary statistics by cluster
cluster_summary_k2 = df.groupby("Cluster_k2")[features].mean().round(2)
cluster_summary_k5 = df.groupby("Cluster_k5")[features].mean().round(2)
print("KMeans k=2 Cluster Summary:")
print(cluster_summary_k2)
print("KMeans k=5 Cluster Summary:")
print(cluster_summary_k5)
# Expand genre into multiple dummy columns for analysis
genre_split = df['Genre'].str.get_dummies(sep=',')
# Combine with cluster labels
genre_cluster_df = pd.concat([df[["Cluster_k2", "Cluster_k5"]], genre_split], axis=1)
# Average genre presence per cluster (as proportions)
genre_by_cluster_k2 = genre_cluster_df.groupby("Cluster_k2").mean().round(2).T
genre_by_cluster_k5 = genre_cluster_df.groupby("Cluster_k5").mean().round(2).T
print("Genre Distribution by k=2 Cluster:")
print(genre_by_cluster_k2)
print("Genre Distribution by k=5 Cluster:")
print(genre_by_cluster_k5)
# Inspect top recurring directors
top_directors = df['Director'].value_counts()
top_directors = top_directors[top_directors > 1]
# Map directors with 2+ movies
df['Top_Director'] = df['Director'].apply(lambda x: x if x in top_directors else 'Other')
# Cross-tabulation of Top Directors vs Clusters
director_cluster_k2 = pd.crosstab(df['Top_Director'], df['Cluster_k2'])
director_cluster_k5 = pd.crosstab(df['Top_Director'], df['Cluster_k5'])
print("Top Directors vs k=2 Clusters:")
print(director_cluster_k2)
print("Top Directors vs k=5 Clusters:")
print(director_cluster_k5)
# === Actor Analysis ===
# Extract and flatten actor lists
df['Actor_List'] = df['Actors'].str.split(',').apply(lambda x: [a.strip() for a in x])
all_actors = list(itertools.chain.from_iterable(df['Actor_List']))
actor_counts = Counter(all_actors)
# Focus on frequently occurring actors (in 2+ movies)
top_actors = {actor for actor, count in actor_counts.items() if count > 1}
# Create binary matrix of actors
for actor in top_actors:
df[actor] = df['Actor_List'].apply(lambda x: int(actor in x))
# Aggregate actor presence by cluster (k=5)
actor_k5= df.groupby("Cluster_k5")[[actor for actor in top_actors]].sum().astype(int).T
print("Top Actor Occurrences by k=5 Cluster:")
print(actor_k5)