-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_regression.py
149 lines (122 loc) · 5.72 KB
/
logistic_regression.py
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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from scipy.spatial.distance import cdist
import os
import matplotlib
matplotlib.use('Agg')
result_dir = "results"
os.makedirs(result_dir, exist_ok=True)
def generate_ellipsoid_clusters(distance, n_samples=100, cluster_std=0.5):
np.random.seed(0)
covariance_matrix = np.array([[cluster_std, cluster_std * 0.8],
[cluster_std * 0.8, cluster_std]])
X1 = np.random.multivariate_normal(mean=[1, 1], cov=covariance_matrix, size=n_samples)
y1 = np.zeros(n_samples)
X2 = np.random.multivariate_normal(mean=[1 - distance, 1 + distance], cov=covariance_matrix, size=n_samples)
y2 = np.ones(n_samples)
X = np.vstack((X1, X2))
y = np.hstack((y1, y2))
return X, y
def fit_logistic_regression(X, y):
model = LogisticRegression()
model.fit(X, y)
beta0 = model.intercept_[0]
beta1, beta2 = model.coef_[0]
return model, beta0, beta1, beta2
def do_experiments(start, end, step_num):
shift_distances = np.linspace(start, end, step_num)
beta0_list, beta1_list, beta2_list, slope_list, intercept_list, loss_list, margin_widths = [], [], [], [], [], [], []
sample_data = {}
n_samples = step_num
n_cols = 2
n_rows = (n_samples + n_cols - 1) // n_cols
plt.figure(figsize=(20, n_rows * 10))
for i, distance in enumerate(shift_distances, 1):
X, y = generate_ellipsoid_clusters(distance=distance)
model, beta0, beta1, beta2 = fit_logistic_regression(X, y)
beta0_list.append(beta0)
beta1_list.append(beta1)
beta2_list.append(beta2)
slope = -beta1 / beta2
intercept = -beta0 / beta2
slope_list.append(slope)
intercept_list.append(intercept)
y_pred_proba = model.predict_proba(X)[:, 1]
logistic_loss_value = -np.mean(y * np.log(y_pred_proba) + (1-y) * np.log(1-y_pred_proba))
loss_list.append(logistic_loss_value)
plt.subplot(n_rows, n_cols, i)
plt.scatter(X[y == 0][:, 0], X[y == 0][:, 1], color='blue', label="Class 0")
plt.scatter(X[y == 1][:, 0], X[y == 1][:, 1], color='red', label="Class 1")
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200), np.linspace(y_min, y_max, 200))
Z = model.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z = Z.reshape(xx.shape)
plt.contour(xx, yy, Z, levels=[0.5], colors='green')
contour_levels = [0.7, 0.8, 0.9]
alphas = [0.05, 0.1, 0.15]
for level, alpha in zip(contour_levels, alphas):
class_1_contour = plt.contourf(xx, yy, Z, levels=[level, 1.0], colors=['red'], alpha=alpha)
class_0_contour = plt.contourf(xx, yy, Z, levels=[0.0, 1 - level], colors=['blue'], alpha=alpha)
if level == 0.7:
distances = cdist(class_1_contour.collections[0].get_paths()[0].vertices, class_0_contour.collections[0].get_paths()[0].vertices, metric='euclidean')
min_distance = np.min(distances)
margin_widths.append(min_distance)
plt.title(f"Shift Distance = {distance}", fontsize=24)
plt.xlabel("x1")
plt.ylabel("x2")
equation_text = f"{beta0:.2f} + {beta1:.2f} * x1 + {beta2:.2f} * x2 = 0\nx2 = {slope:.2f} * x1 + {intercept:.2f}"
margin_text = f"Margin Width: {min_distance:.2f}"
plt.text(x_min + 0.1, y_max - 1.0, equation_text, fontsize=24, color="black", ha='left',
bbox=dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.3'))
plt.text(x_min + 0.1, y_max - 1.5, margin_text, fontsize=24, color="black", ha='left',
bbox=dict(facecolor='white', edgecolor='black', boxstyle='round,pad=0.3'))
if i == 1:
plt.legend(loc='lower right', fontsize=20)
sample_data[distance] = (X, y, model, beta0, beta1, beta2, min_distance)
plt.tight_layout()
plt.savefig(f"{result_dir}/dataset.png")
plt.figure(figsize=(18, 15))
plt.subplot(3, 3, 1)
plt.plot(shift_distances, beta0_list, marker='o', color='blue')
plt.title("Shift Distance vs Beta0")
plt.xlabel("Shift Distance")
plt.ylabel("Beta0")
plt.subplot(3, 3, 2)
plt.plot(shift_distances, beta1_list, marker='o', color='blue')
plt.title("Shift Distance vs Beta1 (Coefficient for x1)")
plt.xlabel("Shift Distance")
plt.ylabel("Beta1")
plt.subplot(3, 3, 3)
plt.plot(shift_distances, beta2_list, marker='o', color='blue')
plt.title("Shift Distance vs Beta2 (Coefficient for x2)")
plt.xlabel("Shift Distance")
plt.ylabel("Beta2")
plt.subplot(3, 3, 4)
plt.plot(shift_distances, slope_list, marker='o', color='blue')
plt.title("Shift Distance vs Beta1 / Beta2 (Slope)")
plt.xlabel("Shift Distance")
plt.ylabel("Beta1 / Beta2")
plt.subplot(3, 3, 5)
plt.plot(shift_distances, intercept_list, marker='o', color='blue')
plt.title("Shift Distance vs Beta0 / Beta2 (Intercept Ratio)")
plt.xlabel("Shift Distance")
plt.ylabel("Beta0 / Beta2")
plt.subplot(3, 3, 6)
plt.plot(shift_distances, loss_list, marker='o', color='blue')
plt.title("Shift Distance vs Logistic Loss")
plt.xlabel("Shift Distance")
plt.ylabel("Logistic Loss")
plt.subplot(3, 3, 7)
plt.plot(shift_distances, margin_widths, marker='o', color='blue')
plt.title("Shift Distance vs Margin Width")
plt.xlabel("Shift Distance")
plt.ylabel("Margin Width")
plt.tight_layout()
plt.savefig(f"{result_dir}/parameters_vs_shift_distance.png")
if __name__ == "__main__":
start = 0.25
end = 2.0
step_num = 8
do_experiments(start, end, step_num)