-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML2.py
More file actions
90 lines (63 loc) · 2.48 KB
/
Copy pathML2.py
File metadata and controls
90 lines (63 loc) · 2.48 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
import pandas as pd
from datetime import datetime
df = pd.read_csv(r"C:\Users\Rig1\Documents\csv\Marvel_Comics.csv")
df.columns = df.columns.str.strip()
print("Rows:", len(df))
print(df.head(3))
df["publish_date"] = pd.to_datetime(df["publish_date"], errors="coerce")
current_year = datetime.now().year
df["publish_year"] = df["publish_date"].dt.year
df["comic_age_years"] = current_year - df["publish_year"]
for col in ["writer", "penciler", "cover_artist"]:
df[col] = df[col].fillna("Unknown")
writer_counts = df["writer"].value_counts()
penciler_counts = df["penciler"].value_counts()
cover_counts = df["cover_artist"].value_counts()
df["writer_popularity"] = df["writer"].map(writer_counts)
df["penciler_popularity"] = df["penciler"].map(penciler_counts)
df["cover_artist_popularity"] = df["cover_artist"].map(cover_counts)
df["Imprint"] = df["Imprint"].fillna("Unknown")
df["Format"] = df["Format"].fillna("Unknown")
df["Rating"] = df["Rating"].fillna("Unknown")
df["title_length"] = df["issue_title"].astype(str).str.len()
df["description_length"] = df["issue_description"].astype(str).str.len()
df["word_count"] = df["issue_description"].astype(str).str.split().str.len()
feature_cols = [
"comic_age_years",
"writer_popularity",
"penciler_popularity",
"cover_artist_popularity",
"title_length",
"description_length",
"word_count"
]
feature_df = df[feature_cols].copy()
feature_df = feature_df.dropna()
print("Feature table shape:", feature_df.shape)
print(feature_df.head(10))
feature_df.to_csv("comic_feature_table_v1.csv", index=False)
print("Saved comic_feature_table_v1.csv")
from sklearn.preprocessing import StandardScaler
X = feature_df.copy()
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=5, random_state=42)
clusters = kmeans.fit_predict(X_scaled)
feature_df["cluster"] = clusters
print(feature_df.head(10))
cluster_summary = feature_df.groupby("cluster").mean()
print(cluster_summary)
df_with_clusters = df.loc[feature_df.index].copy()
df_with_clusters["cluster"] = feature_df["cluster"]
print(
df_with_clusters[
["comic_name", "issue_title", "publish_year", "cluster"]
].head(10)
)
df_with_clusters.to_csv("comics_with_clusters_v1.csv", index=False)
print("Saved comics_with_clusters_v1.csv")
for c in [2, 0]:
print(f"\n--- Cluster {c} summary ---")
print(feature_df[feature_df["cluster"] == c].mean())
print("Count:", (feature_df["cluster"] == c).sum())