generated from LufsX/Experiment-LogReg
This commit is contained in:
+25
-36
@@ -1,17 +1,14 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
id="202210423210"
|
id = "202210423210"
|
||||||
name="陈宇杰"
|
name = "陈宇杰"
|
||||||
|
|
||||||
# Sigmoid Function
|
|
||||||
def sigmoid(z):
|
def sigmoid(z):
|
||||||
return 1 / (1 + np.exp(-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):
|
def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=128, lambda_reg=0.01):
|
||||||
m, n = X.shape
|
m, n = X.shape
|
||||||
weights = np.random.randn(n) * 0.01 # Initialize weights with small random values
|
weights = np.zeros(n)
|
||||||
prev_weights = np.copy(weights)
|
|
||||||
|
|
||||||
for j in range(iterations):
|
for j in range(iterations):
|
||||||
dataIndex = np.random.permutation(m)
|
dataIndex = np.random.permutation(m)
|
||||||
@@ -20,63 +17,55 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=128, lamb
|
|||||||
batch_X = X[dataIndex[i:end]]
|
batch_X = X[dataIndex[i:end]]
|
||||||
batch_y = y[dataIndex[i:end]]
|
batch_y = y[dataIndex[i:end]]
|
||||||
|
|
||||||
# Vectorized gradient calculation
|
|
||||||
z = np.dot(batch_X, weights)
|
z = np.dot(batch_X, weights)
|
||||||
h = sigmoid(z)
|
h = sigmoid(z)
|
||||||
|
|
||||||
error = batch_y - h
|
error = batch_y - h
|
||||||
gradient = np.dot(batch_X.T, error) / len(batch_y)
|
gradient = np.dot(batch_X.T, error) / len(batch_y)
|
||||||
gradient -= lambda_reg * weights / len(batch_y)
|
gradient -= lambda_reg * weights / len(batch_y)
|
||||||
|
|
||||||
weights += learning_rate * gradient
|
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
|
return weights
|
||||||
|
|
||||||
def predict_logreg(X, weights):
|
def predict_logreg(inX, weights):
|
||||||
probabilities = sigmoid(np.dot(X, weights))
|
probability = sigmoid(np.dot(inX, weights))
|
||||||
return (probabilities >= 0.5).astype(int)
|
return 1 if probability >= 0.5 else 0
|
||||||
|
|
||||||
# Data Loading Function
|
|
||||||
def load_data(filepath):
|
def load_data(filepath):
|
||||||
data, labels = [], []
|
data = []
|
||||||
|
labels = []
|
||||||
with open(filepath, 'r') as file:
|
with open(filepath, 'r') as file:
|
||||||
for line in file:
|
for line in file:
|
||||||
parts = line.strip().split()
|
parts = line.strip().split()
|
||||||
features = [float(x) if x != '?' else 0.0 for x in parts[:-1]]
|
features = parts[:-1]
|
||||||
label = parts[-1]
|
label = parts[-1]
|
||||||
|
features = [float(x) if x != '?' else 0.0 for x in features]
|
||||||
data.append(features)
|
data.append(features)
|
||||||
labels.append(1 if label in ['2', '3'] else 0)
|
labels.append(1 if label in ['2', '3'] else 0)
|
||||||
|
|
||||||
# Convert to numpy arrays
|
# 转换为numpy数组
|
||||||
data = np.array(data)
|
data = np.array(data)
|
||||||
labels = np.array(labels)
|
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
|
return data, labels
|
||||||
|
|
||||||
# Main script
|
if name == "main":
|
||||||
if __name__ == "__main__":
|
|
||||||
# Load training data
|
|
||||||
X_train, y_train = load_data("./data/horse-colic.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, 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)
|
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
|
error_count = 0
|
||||||
y_pred = predict_logreg(X_test, weights)
|
for i in range(len(y_test)):
|
||||||
accuracy = np.mean(y_pred == 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]}")
|
||||||
|
|
||||||
print(f"Accuracy: {accuracy:.2f}")
|
accuracy = (len(y_test) - error_count) / len(y_test)
|
||||||
|
print(f"准确率: {accuracy:.2f}")
|
||||||
Reference in New Issue
Block a user