-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathML.py
More file actions
138 lines (101 loc) · 3.57 KB
/
Copy pathML.py
File metadata and controls
138 lines (101 loc) · 3.57 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
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.metrics import classification_report
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.pipeline import Pipeline
from sklearn.metrics import mean_absolute_error, r2_score
from datetime import datetime
df = pd.read_csv(r"C:\Users\Rig1\Documents\csv\Marvel_Comics.csv")
# print(df.columns.tolist())
#
# print(df.head(3))
#
# print(df.dtypes)
#
# print(df.info())
#
#
# df["publish_date"] = pd.to_datetime(df["publish_date"], errors="coerce")
# df["publish_year"] = df["publish_date"].dt.year
# df["publish_month"] = df["publish_date"].dt.month
#
# df_num = df.select_dtypes(include=["number"]).dropna()
#
# threshold = df_num["APPEARANCES"].quantile(0.75)
# #df_num["HIGH_VALUE_PROXY"] = (df_num["APPEARANCES"] >= threshold).astype(int)
#
# #df_num["HIGH_VALUE_PROXY"] = ...
#
#
# X = df_num.drop(columns=["APPEARANCES", "HIGH_VALUE_PROXY"])
# y = df_num["HIGH_VALUE_PROXY"]
#
# X_train, X_test, y_train, y_test = train_test_split(
# X, y, test_size=0.25, random_state=42
# )
#
# model = LogisticRegression(max_iter=1000)
# model.fit(X_train, y_train)
#
# preds = model.predict(X_test)
# print(classification_report(y_test, preds))
print("Rows:", len(df))
# print("Price missing:", df["Price"].isna().sum())
# print("Price non-missing:", df["Price"].notna().sum())
# print("Columns:", df.columns.tolist())
df.columns = df.columns.str.strip()
print(df.head(3))
#print(df.dtypes)
df["Price"] = pd.to_numeric(df["Price"], errors="coerce")
print("Price nulls after cleaning:", df["Price"].isna().sum())
print(df[["Price"]].head(10))
df["Price_raw"] = df["Price"]
s = df["Price"].astype(str).str.strip()
df["Price"] = (
s.str.replace(",", "", regex=False)
.str.extract(r"(\d+(?:\.\d+)?)", expand=False)
)
df["Price"] = pd.to_numeric(df["Price"], errors="coerce")
print("Non-null Price count:", df["Price"].notna().sum())
print(df[["Price_raw", "Price"]].head(20))
df["publish_date"] = pd.to_datetime(df["publish_date"], errors="coerce")
df["publish_year"] = df["publish_date"].dt.year
df["publish_month"] = df["publish_date"].dt.month
target = "Price"
features = ["publish_year", "publish_month", "Imprint", "Format", "Rating"]
#df_model = df[features + [target]].dropna()
df_model = df[features + [target]].copy()
df_model = df_model[df_model["Price"].notna()]
for col in ["Imprint", "Format", "Rating"]:
df_model[col] = df_model[col].fillna("Unknown")
df_model["publish_year"] = df_model["publish_year"].fillna(df_model["publish_year"].median())
df_model["publish_month"] = df_model["publish_month"].fillna(df_model["publish_month"].median())
print("Rows available for modeling:", len(df_model))
X = df_model[features]
y = df_model[target]
print("Total rows in df:", len(df))
print("Rows after cleaning:", len(df_model))
print(df_model.head())
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42
)
cat_cols = ["Imprint", "Format", "Rating"]
num_cols = ["publish_year", "publish_month"]
preprocess = ColumnTransformer(
transformers=[
("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols),
("num", "passthrough", num_cols),
]
)
model = Pipeline(steps=[
("prep", preprocess),
("lr", LinearRegression())
])
model.fit(X_train, y_train)
preds = model.predict(X_test)
print("MAE:", mean_absolute_error(y_test, preds))
print("R²:", r2_score(y_test, preds))