diff --git a/function.py b/function.py index 7353415..bea1bd9 100644 --- a/function.py +++ b/function.py @@ -6,24 +6,18 @@ name = "吴杨洋" def sigmoid(z): 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 weights = np.zeros(n) for j in range(numIter): - indices = np.random.permutation(m) - X_shuffled = X[indices] - 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) + for i in range(m): + z = np.dot(X[i], weights) h = sigmoid(z) - error = batch_y - h - gradient = np.dot(batch_X.T, error) / len(batch_y) + error = y[i] - h + gradient = X[i] * error reg_term = lambda_reg * weights - gradient += reg_term / len(batch_y) - weights -= learning_rate * gradient + gradient += reg_term + weights += learning_rate * gradient return weights def predict_logreg(inX, weights): @@ -68,11 +62,22 @@ def main(): X_train, mean, std = standardize(X_train) 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] - accuracy_test = np.mean(y_pred_test == y_test) - print(f"Test Accuracy: {accuracy_test:.2f}") + best_accuracy = 0 + best_weights = None + 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__": main() \ No newline at end of file