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