-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctr_prediction.py
More file actions
211 lines (154 loc) · 6.42 KB
/
Copy pathctr_prediction.py
File metadata and controls
211 lines (154 loc) · 6.42 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
"""
Click-Through Rate Prediction with Python: Logistic Regression for E-commerce
Predicts whether a user will click a promotional banner ad based on
behavioral and demographic features.
Workflow:
1. Data exploration
2. Correlation & label distribution
3. Visualization
4. Missing value check
5. Logistic Regression modeling (80:20 train/test split)
6. Evaluation (accuracy, confusion matrix, classification report)
"""
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, classification_report
DATA_URL = "https://storage.googleapis.com/dqlab-dataset/pythonTutorial/ecommerce_banner_promo.csv"
NON_NUMERIC_COLUMNS = ["Ad Topic Line", "City", "Country", "Timestamp"]
TARGET_COLUMN = "Clicked on Ad"
# ---------------------------------------------------------------------------
# 1. DATA EXPLORATION
# ---------------------------------------------------------------------------
def load_data(url: str = DATA_URL) -> pd.DataFrame:
data = pd.read_csv(url)
print("\n[1] Data exploration with head(), info(), describe(), shape")
print("Top 5 rows:")
print(data.head())
print("Dataset info:")
print(data.info())
print("Descriptive statistics:")
print(data.describe())
print("Dataset shape:")
print(data.shape)
return data
def explore_correlation_and_distribution(data: pd.DataFrame) -> None:
print("\n[2] Feature correlation via corr()")
print(data.corr(numeric_only=True))
print("\n[3] Label distribution via groupby() and size()")
print(data.groupby("Clicked on Ad").size())
# ---------------------------------------------------------------------------
# 2. VISUALIZATION
# ---------------------------------------------------------------------------
def plot_age_histogram(data: pd.DataFrame) -> None:
sns.set_style("whitegrid")
plt.style.use("fivethirtyeight")
plt.figure(figsize=(10, 5))
plt.hist(data["Age"], bins=data.Age.nunique())
plt.xlabel("Age")
plt.tight_layout()
plt.show()
def plot_feature_pairplot(data: pd.DataFrame) -> None:
plt.figure()
sns.pairplot(data)
plt.show()
# ---------------------------------------------------------------------------
# 3. MISSING VALUE CHECK
# ---------------------------------------------------------------------------
def check_missing_values(data: pd.DataFrame) -> None:
print("\n[5] Missing value check")
print(data.isnull().sum().sum())
# ---------------------------------------------------------------------------
# 4. MODELING
# ---------------------------------------------------------------------------
def build_features_and_target(data: pd.DataFrame):
"""Drop non-numeric columns (Logistic Regression requires numeric input)
and split into features (X) and target (y)."""
X = data.drop(NON_NUMERIC_COLUMNS + [TARGET_COLUMN], axis=1)
y = data[TARGET_COLUMN]
return X, y
def train_model(X, y, test_size: float = 0.2, random_state: int = 42):
"""Split data 80:20 and train a Logistic Regression classifier."""
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=test_size, random_state=random_state
)
logreg = LogisticRegression()
logreg = logreg.fit(X_train, y_train)
y_pred = logreg.predict(X_test)
print("\n[6] Logistic Regression, 80:20 train/test split")
print("Model evaluation:")
print("Training Accuracy:", logreg.score(X_train, y_train))
print("Testing Accuracy :", logreg.score(X_test, y_test))
return logreg, X_train, X_test, y_train, y_test, y_pred
# ---------------------------------------------------------------------------
# 5. EVALUATION
# ---------------------------------------------------------------------------
def print_confusion_matrix_and_report(y_test, y_pred) -> None:
print("\n[7] Confusion matrix and classification report")
print("Confusion matrix:")
cm = confusion_matrix(y_test, y_pred)
print(cm)
print("Classification report:")
cr = classification_report(y_test, y_pred)
print(cr)
def plot_confusion_matrix(y_test, y_pred) -> None:
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6, 5))
sns.heatmap(
cm, annot=True, fmt="d",
cmap=sns.color_palette(["#800000", "#004080"]),
cbar=False, linewidths=1, linecolor="white"
)
plt.title("Confusion Matrix", fontsize=16, color="#800000", weight="bold")
plt.xlabel("Predicted Label", fontsize=12, color="#004080")
plt.ylabel("True Label", fontsize=12, color="#004080")
plt.xticks(ticks=[0.5, 1.5], labels=["0", "1"], fontsize=11)
plt.yticks(ticks=[0.5, 1.5], labels=["0", "1"], fontsize=11, rotation=0)
plt.tight_layout()
plt.show()
def plot_classification_report_table(y_test, y_pred) -> None:
report = classification_report(y_test, y_pred, output_dict=True)
df = pd.DataFrame(report).transpose()
# Total support for numeric classes only (0, 1)
total_support = df.loc[["0", "1"], "support"].sum()
df.loc["accuracy", "support"] = total_support
fig, ax = plt.subplots(figsize=(8, 3))
ax.axis("off")
ax.axis("tight")
table = ax.table(
cellText=df.round(2).values,
colLabels=df.columns,
rowLabels=df.index,
loc="center",
cellLoc="center"
)
for (i, j), cell in table.get_celld().items():
cell.set_fontsize(10)
if i == 0 or j == -1:
cell.set_text_props(weight="bold", color="white")
cell.set_facecolor("#800000")
elif i % 2 == 0:
cell.set_facecolor("#f7e6e6")
else:
cell.set_facecolor("#f2dcdc")
table.scale(1.2, 1.2)
plt.title("Classification Report", fontsize=14, color="#800000", weight="bold")
plt.show()
# ---------------------------------------------------------------------------
# MAIN
# ---------------------------------------------------------------------------
def main():
data = load_data()
explore_correlation_and_distribution(data)
plot_age_histogram(data)
plot_feature_pairplot(data)
check_missing_values(data)
X, y = build_features_and_target(data)
logreg, X_train, X_test, y_train, y_test, y_pred = train_model(X, y)
print_confusion_matrix_and_report(y_test, y_pred)
plot_confusion_matrix(y_test, y_pred)
plot_classification_report_table(y_test, y_pred)
if __name__ == "__main__":
main()