forked from LufsX/Experiment-LogReg
This commit is contained in:
+22
-17
@@ -6,24 +6,18 @@ name = "吴杨洋"
|
|||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1 / (1 + np.exp(-np.clip(z, -500, 500)))
|
return 1 / (1 + np.exp(-np.clip(z, -500, 500)))
|
||||||
|
|
||||||
def train_logreg(X, y, numIter=1000, learning_rate=0.01, lambda_reg=0.01, batch_size=64):
|
def train_logreg(X, y, numIter=1000, learning_rate=0.01, lambda_reg=0.01):
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.zeros(n)
|
weights = np.zeros(n)
|
||||||
for j in range(numIter):
|
for j in range(numIter):
|
||||||
indices = np.random.permutation(m)
|
for i in range(m):
|
||||||
X_shuffled = X[indices]
|
z = np.dot(X[i], weights)
|
||||||
y_shuffled = y[indices]
|
|
||||||
for i in range(0, m, batch_size):
|
|
||||||
end = min(i + batch_size, m)
|
|
||||||
batch_X = X_shuffled[i:end]
|
|
||||||
batch_y = y_shuffled[i:end]
|
|
||||||
z = np.dot(batch_X, weights)
|
|
||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
error = batch_y - h
|
error = y[i] - h
|
||||||
gradient = np.dot(batch_X.T, error) / len(batch_y)
|
gradient = X[i] * error
|
||||||
reg_term = lambda_reg * weights
|
reg_term = lambda_reg * weights
|
||||||
gradient += reg_term / len(batch_y)
|
gradient += reg_term
|
||||||
weights -= learning_rate * gradient
|
weights += learning_rate * gradient
|
||||||
return weights
|
return weights
|
||||||
|
|
||||||
def predict_logreg(inX, weights):
|
def predict_logreg(inX, weights):
|
||||||
@@ -68,11 +62,22 @@ def main():
|
|||||||
X_train, mean, std = standardize(X_train)
|
X_train, mean, std = standardize(X_train)
|
||||||
X_test = (X_test - mean) / std
|
X_test = (X_test - mean) / std
|
||||||
|
|
||||||
weights = train_logreg(X_train, y_train, numIter=1000, learning_rate=0.01, lambda_reg=0.1, batch_size=64)
|
# 尝试不同的学习率和正则化参数
|
||||||
|
learning_rates = [0.01, 0.001, 0.0001]
|
||||||
|
lambda_regs = [0.1, 0.01, 0.001]
|
||||||
|
|
||||||
y_pred_test = [predict_logreg(x, weights) for x in X_test]
|
best_accuracy = 0
|
||||||
accuracy_test = np.mean(y_pred_test == y_test)
|
best_weights = None
|
||||||
print(f"Test Accuracy: {accuracy_test:.2f}")
|
for lr in learning_rates:
|
||||||
|
for reg in lambda_regs:
|
||||||
|
weights = train_logreg(X_train, y_train, numIter=1000, learning_rate=lr, lambda_reg=reg)
|
||||||
|
y_pred_test = [predict_logreg(x, weights) for x in X_test]
|
||||||
|
accuracy_test = np.mean(y_pred_test == y_test)
|
||||||
|
if accuracy_test > best_accuracy:
|
||||||
|
best_accuracy = accuracy_test
|
||||||
|
best_weights = weights
|
||||||
|
|
||||||
|
print(f"Best Test Accuracy: {best_accuracy:.2f}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user