forked from LufsX/Experiment-LogReg
This commit is contained in:
+29
-25
@@ -1,28 +1,27 @@
|
||||
import numpy as np
|
||||
import os
|
||||
from sklearn.preprocessing import StandardScaler # Import standardization library
|
||||
|
||||
# Student ID and Name
|
||||
# 学号和姓名
|
||||
id = "202210423220"
|
||||
name = "方子涵"
|
||||
|
||||
def sigmoid(z):
|
||||
"""Calculate the sigmoid function"""
|
||||
"""计算sigmoid函数"""
|
||||
return 1.0 / (1 + np.exp(-z))
|
||||
|
||||
def compute_gradient(X_batch, y_batch, weights, lambda_reg):
|
||||
"""Calculate the gradient, including L2 regularization"""
|
||||
"""计算梯度,包含L2正则化"""
|
||||
z = np.dot(X_batch, weights)
|
||||
h = sigmoid(z)
|
||||
gradient = np.dot(X_batch.T, (h - y_batch)) / len(y_batch)
|
||||
gradient += (lambda_reg / len(y_batch)) * weights # L2 regularization
|
||||
gradient += (lambda_reg / len(y_batch)) * weights # L2正则化
|
||||
return gradient
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
|
||||
"""Logistic regression training function."""
|
||||
"""对率回归训练函数。"""
|
||||
m, n = X.shape
|
||||
weights = np.random.randn(n) * np.sqrt(2. / n) # He initialization
|
||||
print("Starting to train the logistic regression model")
|
||||
weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化
|
||||
print("开始训练对率回归模型")
|
||||
|
||||
for i in range(iterations):
|
||||
indices = np.random.permutation(m)
|
||||
@@ -35,21 +34,27 @@ def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lam
|
||||
gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg)
|
||||
weights -= learning_rate * gradient
|
||||
|
||||
if i % 1000 == 0: # Print information every 1000 iterations
|
||||
print(f"Iteration {i}: Current weight shape: {weights.shape}")
|
||||
if i % 1000 == 0: # 每1000次迭代打印一次信息
|
||||
print(f"迭代 {i}: 当前权重形状: {weights.shape}")
|
||||
|
||||
return weights
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
"""Logistic regression prediction function."""
|
||||
"""对率回归预测函数。"""
|
||||
z = np.dot(inX, weights)
|
||||
h = sigmoid(z)
|
||||
return 1 if h >= 0.5 else 0
|
||||
|
||||
def standardize_data(X):
|
||||
"""标准化数据"""
|
||||
mean = np.mean(X, axis=0)
|
||||
std = np.std(X, axis=0)
|
||||
return (X - mean) / std, mean, std
|
||||
|
||||
def load_data(filepath):
|
||||
"""Load data from a file."""
|
||||
"""从文件中加载数据。"""
|
||||
if not os.path.exists(filepath):
|
||||
raise FileNotFoundError(f"File {filepath} does not exist!")
|
||||
raise FileNotFoundError(f"文件 {filepath} 不存在!")
|
||||
|
||||
data, labels = [], []
|
||||
print(f"Loading data: {filepath}")
|
||||
@@ -65,7 +70,7 @@ def load_data(filepath):
|
||||
labels = np.array(labels)
|
||||
|
||||
num_features = data.shape[1]
|
||||
indices_to_remove = [2] # Remove feature 3 (index 2)
|
||||
indices_to_remove = [2] # 去掉特征3(索引2)
|
||||
|
||||
if num_features > 23:
|
||||
indices_to_remove.extend(range(23, min(28, num_features)))
|
||||
@@ -77,7 +82,7 @@ def load_data(filepath):
|
||||
return data, target_variable
|
||||
|
||||
def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1):
|
||||
"""K-fold cross-validation."""
|
||||
"""K折交叉验证。"""
|
||||
m = X.shape[0]
|
||||
fold_size = m // k
|
||||
accuracies = []
|
||||
@@ -101,34 +106,33 @@ def k_fold_cross_validation(X, y, k=5, learning_rate=0.001, iterations=10000, ba
|
||||
return np.mean(accuracies)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Load training set
|
||||
# 加载训练集
|
||||
try:
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# Data normalization
|
||||
scaler = StandardScaler()
|
||||
X_train = scaler.fit_transform(X_train) # Standardize using the training set
|
||||
# 数据标准化处理
|
||||
X_train, mean, std = standardize_data(X_train)
|
||||
|
||||
# K-fold cross-validation
|
||||
# K折交叉验证
|
||||
avg_accuracy = k_fold_cross_validation(X_train, y_train, k=5, learning_rate=0.01, iterations=1000)
|
||||
print(f"Average accuracy of K-fold cross-validation: {avg_accuracy:.4f}")
|
||||
|
||||
# Load test set
|
||||
# 加载测试集
|
||||
try:
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# Test set also needs the same normalization
|
||||
X_test = scaler.transform(X_test) # Standardize using the same Scaler
|
||||
# 测试集也需要进行相同的标准化
|
||||
X_test = (X_test - mean) / std # 使用训练集的均值和标准差进行标准化
|
||||
|
||||
# Train logistic regression model
|
||||
# 训练对率回归模型
|
||||
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)
|
||||
|
||||
# Test a single data point
|
||||
# 测试单个数据
|
||||
y_pred = predict_logreg(X_test[11], weights)
|
||||
print(f"Predicted result: {y_pred}, Actual result: {y_test[11]}")
|
||||
Reference in New Issue
Block a user