import numpy as np id="202210423210" name="陈宇杰" # Sigmoid Function def sigmoid(z): return 1 / (1 + np.exp(-z)) # Optimized Logistic Regression Training Function def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=128, lambda_reg=0.01): m, n = X.shape weights = np.random.randn(n) * 0.01 # Initialize weights with small random values prev_weights = np.copy(weights) for j in range(iterations): dataIndex = np.random.permutation(m) for i in range(0, m, batch_size): end = min(i + batch_size, m) batch_X = X[dataIndex[i:end]] batch_y = y[dataIndex[i:end]] # Vectorized gradient calculation z = np.dot(batch_X, weights) h = sigmoid(z) error = batch_y - h gradient = np.dot(batch_X.T, error) / len(batch_y) gradient -= lambda_reg * weights / len(batch_y) weights += learning_rate * gradient # Early stopping condition if np.linalg.norm(weights - prev_weights) < 1e-5: print(f"Converged after {j} iterations.") break prev_weights = np.copy(weights) return weights def predict_logreg(X, weights): probabilities = sigmoid(np.dot(X, weights)) return (probabilities >= 0.5).astype(int) # Data Loading Function def load_data(filepath): data, labels = [], [] with open(filepath, 'r') as file: for line in file: parts = line.strip().split() features = [float(x) if x != '?' else 0.0 for x in parts[:-1]] label = parts[-1] data.append(features) labels.append(1 if label in ['2', '3'] else 0) # Convert to numpy arrays data = np.array(data) labels = np.array(labels) return data, labels # Main script if __name__ == "__main__": # Load training data X_train, y_train = load_data("./data/horse-colic.data") # Calculate mean and std from training data data_mean = np.mean(X_train, axis=0) data_std = np.std(X_train, axis=0) X_train = (X_train - data_mean) / (data_std + 1e-10) # Load and normalize test data using training mean and std X_test, y_test = load_data("./data/horse-colic.test") X_test = (X_test - data_mean) / (data_std + 1e-10) # Train model weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=2000, batch_size=64, lambda_reg=0.01) # Make predictions and calculate accuracy y_pred = predict_logreg(X_test, weights) accuracy = np.mean(y_pred == y_test) print(f"Accuracy: {accuracy:.2f}")