generated from LufsX/Experiment-LogReg
This commit is contained in:
+24
-17
@@ -26,17 +26,17 @@ from sklearn.ensemble import RandomForestClassifier
|
|||||||
id = "202210423117"
|
id = "202210423117"
|
||||||
name = "程悦"
|
name = "程悦"
|
||||||
|
|
||||||
|
|
||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1.0 / (1 + np.exp(-z))
|
return 1.0 / (1 + np.exp(-z))
|
||||||
|
|
||||||
|
|
||||||
def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64):
|
def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64, patience=100, decay_rate=0.95):
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.random.randn(n) * 0.01
|
weights = np.random.randn(n) * 0.01
|
||||||
best_weights = weights.copy()
|
best_weights = weights.copy()
|
||||||
best_accuracy = 0
|
best_accuracy = 0
|
||||||
wait = 0
|
wait = 0
|
||||||
|
decay_step = iterations // 10 # 每10%的迭代衰减一次学习率
|
||||||
|
|
||||||
for i in range(iterations):
|
for i in range(iterations):
|
||||||
indices = np.random.permutation(m)
|
indices = np.random.permutation(m)
|
||||||
@@ -49,11 +49,12 @@ def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64):
|
|||||||
z = np.dot(X_batch, weights)
|
z = np.dot(X_batch, weights)
|
||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
|
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
|
||||||
|
|
||||||
weights -= learning_rate * gradient
|
weights -= learning_rate * gradient
|
||||||
|
|
||||||
# 计算训练集准确率
|
# 计算训练集准确率
|
||||||
train_accuracy = accuracy(X, y, weights)
|
train_accuracy = accuracy(X, y, weights)
|
||||||
|
print(f"Iteration {i + 1}, Train Accuracy: {train_accuracy:.4f}")
|
||||||
|
|
||||||
if train_accuracy > best_accuracy:
|
if train_accuracy > best_accuracy:
|
||||||
best_accuracy = train_accuracy
|
best_accuracy = train_accuracy
|
||||||
best_weights = weights.copy()
|
best_weights = weights.copy()
|
||||||
@@ -61,8 +62,13 @@ def train_logreg(X, y, learning_rate=0.05, iterations=1500, batch_size=64):
|
|||||||
else:
|
else:
|
||||||
wait += 1
|
wait += 1
|
||||||
|
|
||||||
if i % 100 == 0:
|
if i % decay_step == 0 and i > 0:
|
||||||
learning_rate *= 0.95
|
learning_rate *= decay_rate
|
||||||
|
print(f"Learning rate decayed to: {learning_rate:.6f}")
|
||||||
|
|
||||||
|
if wait >= patience:
|
||||||
|
print("Early stopping!")
|
||||||
|
break
|
||||||
|
|
||||||
return best_weights
|
return best_weights
|
||||||
|
|
||||||
@@ -72,23 +78,24 @@ def predict_logreg(X, weights):
|
|||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
return np.where(h > 0.5, 1, 0)
|
return np.where(h > 0.5, 1, 0)
|
||||||
|
|
||||||
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
data = []
|
data = []
|
||||||
labels = []
|
labels = []
|
||||||
with open(filepath, 'r') as f:
|
with open(filepath, 'r') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
line = line.strip().split()
|
parts = line.strip().split()
|
||||||
if len(line) >= 1 and line[-1].isdigit():
|
if len(parts) >= 2 and parts[-1].isdigit():
|
||||||
line_data = [0 if x == '?' else float(x) for x in line[:-1]]
|
line_data = [0 if x == '?' else float(x) for x in parts[:-1]]
|
||||||
|
label = int(parts[-1])
|
||||||
|
# 将标签映射到0和1(假设原始标签是1, 2, 3,我们将其映射为0和1)
|
||||||
|
target_label = 0 if label in [2, 3] else 1
|
||||||
data.append(line_data)
|
data.append(line_data)
|
||||||
labels.append(int(line[-1]))
|
labels.append(target_label)
|
||||||
|
|
||||||
data = np.array(data)
|
data = np.array(data)
|
||||||
labels = np.array(labels)
|
labels = np.array(labels)
|
||||||
|
return data, labels
|
||||||
target_variable = np.where((labels == 2) | (labels == 3), 0, labels)
|
|
||||||
|
|
||||||
return data, target_variable
|
|
||||||
|
|
||||||
|
|
||||||
def accuracy(X, y, weights):
|
def accuracy(X, y, weights):
|
||||||
@@ -97,15 +104,15 @@ def accuracy(X, y, weights):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
X_train, y_train = load_data("./data/horse-colic.data")
|
X_train, y_train = load_data("./horse-colic.data")
|
||||||
X_test, y_test = load_data("./data/horse-colic.test")
|
X_test, y_test = load_data("./horse-colic.test")
|
||||||
|
|
||||||
# 标准化数据
|
# 标准化数据
|
||||||
scaler = StandardScaler()
|
scaler = StandardScaler()
|
||||||
X_train = scaler.fit_transform(X_train)
|
X_train = scaler.fit_transform(X_train)
|
||||||
X_test = scaler.transform(X_test)
|
X_test = scaler.transform(X_test)
|
||||||
|
|
||||||
final_weights = train_logreg(X_train, y_train, learning_rate=0.001, iterations=1500, batch_size=64)
|
final_weights = train_logreg(X_train, y_train, learning_rate=0.001, iterations=1500, batch_size=64, patience=200)
|
||||||
|
|
||||||
final_accuracy = accuracy(X_test, y_test, final_weights)
|
final_accuracy = accuracy(X_test, y_test, final_weights)
|
||||||
print(f"最终测试集的准确率: {final_accuracy:.4f}")
|
print(f"最终测试集的准确率: {final_accuracy:.4f}")
|
||||||
Reference in New Issue
Block a user