generated from LufsX/Experiment-LogReg
This commit is contained in:
+28
-39
@@ -1,17 +1,14 @@
|
||||
import numpy as np
|
||||
|
||||
id="202210423210"
|
||||
name="陈宇杰"
|
||||
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)
|
||||
weights = np.zeros(n)
|
||||
|
||||
for j in range(iterations):
|
||||
dataIndex = np.random.permutation(m)
|
||||
@@ -19,64 +16,56 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=128, lamb
|
||||
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)
|
||||
def predict_logreg(inX, weights):
|
||||
probability = sigmoid(np.dot(inX, weights))
|
||||
return 1 if probability >= 0.5 else 0
|
||||
|
||||
# Data Loading Function
|
||||
def load_data(filepath):
|
||||
data, labels = [], []
|
||||
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]]
|
||||
features = parts[:-1]
|
||||
label = parts[-1]
|
||||
features = [float(x) if x != '?' else 0.0 for x in features]
|
||||
data.append(features)
|
||||
labels.append(1 if label in ['2', '3'] else 0)
|
||||
|
||||
# Convert to numpy arrays
|
||||
# 转换为numpy数组
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
|
||||
# 简单均值归一化
|
||||
data_mean = np.mean(data, axis=0)
|
||||
data_std = np.std(data, axis=0)
|
||||
data = (data - data_mean) / (data_std + 1e-10) # 防止除零
|
||||
|
||||
return data, labels
|
||||
|
||||
# Main script
|
||||
if __name__ == "__main__":
|
||||
# Load training data
|
||||
if name == "main":
|
||||
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}")
|
||||
error_count = 0
|
||||
for i in range(len(y_test)):
|
||||
y_pred = predict_logreg(X_test[i], weights)
|
||||
if y_pred != y_test[i]:
|
||||
error_count += 1
|
||||
print(f"预测结果: {y_pred}, 实际结果: {y_test[i]}")
|
||||
|
||||
accuracy = (len(y_test) - error_count) / len(y_test)
|
||||
print(f"准确率: {accuracy:.2f}")
|
||||
Reference in New Issue
Block a user