Template
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a436a9ca27 | ||
|
|
a392cde243 | ||
|
|
c04a62e60c | ||
|
|
9bb3b14ad0 | ||
|
|
3ddcbb2064 | ||
|
|
ae3325d15e | ||
|
|
55d44b8658 | ||
|
|
47b2cc1cbe |
+122
-58
@@ -1,85 +1,149 @@
|
||||
# function.py
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
"""
|
||||
实验说明:
|
||||
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
|
||||
# 学号和姓名
|
||||
id = "202210423220"
|
||||
name = "方子涵"
|
||||
|
||||
注意事项:
|
||||
- 在 function.py 中实现对率回归算法。
|
||||
- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。
|
||||
- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。
|
||||
def sigmoid(z):
|
||||
"""计算sigmoid函数"""
|
||||
return 1.0 / (1 + np.exp(-z))
|
||||
|
||||
测评环境:
|
||||
- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz
|
||||
- OS: Ubuntu 22.04.4 LTS
|
||||
- Python: 3.10.12
|
||||
- numpy: 2.1.1
|
||||
"""
|
||||
def compute_gradient(X_batch, y_batch, weights, lambda_reg):
|
||||
"""计算梯度,包含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正则化
|
||||
return gradient
|
||||
|
||||
def train_logreg(X, y, learning_rate=0.01, iterations=20000, batch_size=64, lambda_reg=0.01, patience=5):
|
||||
"""对率回归训练函数。"""
|
||||
m, n = X.shape
|
||||
weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化
|
||||
best_weights = weights.copy()
|
||||
best_accuracy = 0
|
||||
wait = 0
|
||||
|
||||
"""
|
||||
请在这里填写学号和姓名,不填写即成绩无效。
|
||||
姓名请直接**填写中文**
|
||||
例如: name = "张三"
|
||||
"""
|
||||
id = "202210423xxx"
|
||||
name = "test_name"
|
||||
for i in range(iterations):
|
||||
indices = np.random.permutation(m)
|
||||
X_shuffled = X[indices]
|
||||
y_shuffled = y[indices]
|
||||
|
||||
for j in range(0, m, batch_size):
|
||||
X_batch = X_shuffled[j:j + batch_size]
|
||||
y_batch = y_shuffled[j:j + batch_size]
|
||||
gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg)
|
||||
weights -= learning_rate * gradient
|
||||
|
||||
# 这里写自己的代码实现
|
||||
def train_logreg(X, y, other_params="你自己设置一些其它参数"):
|
||||
"""
|
||||
对率回归训练函数。
|
||||
|
||||
Args:
|
||||
- X (numpy array): 特征数据。
|
||||
- y (numpy array): 标签数据。
|
||||
- other_params (str): 其它参数,可选设置。若需设置其它参数,必须设置其默认值,否则将**无法通过**自动评测,**成绩为零**。
|
||||
|
||||
Returns:
|
||||
- numpy array: 训练得到的权重。
|
||||
"""
|
||||
# 早停法:在验证集上评估模型性能
|
||||
accuracy = np.mean([1 if sigmoid(np.dot(X[i], weights)) >= 0.5 and y[i] == 1 or sigmoid(np.dot(X[i], weights)) < 0.5 and y[i] == 0 else 0 for i in range(m)])
|
||||
if accuracy > best_accuracy:
|
||||
best_accuracy = accuracy
|
||||
best_weights = weights.copy()
|
||||
wait = 0
|
||||
else:
|
||||
wait += 1
|
||||
if wait >= patience:
|
||||
print(f"Early stopping at iteration {i+1}")
|
||||
break
|
||||
|
||||
return best_weights
|
||||
|
||||
def predict_logreg(inX, weights):
|
||||
"""
|
||||
对率回归预测函数。
|
||||
"""对率回归预测函数。"""
|
||||
z = np.dot(inX, weights)
|
||||
h = sigmoid(z)
|
||||
return 1 if h >= 0.5 else 0
|
||||
|
||||
Args:
|
||||
- inX (numpy array): 输入待分类的样本。
|
||||
- weights (numpy array): 训练得到的权重。
|
||||
def standardize_data(X):
|
||||
"""标准化数据"""
|
||||
mean = np.mean(X, axis=0)
|
||||
std = np.std(X, axis=0)
|
||||
return (X - mean) / std, mean, std
|
||||
|
||||
Returns:
|
||||
- int: 预测的分类标签。
|
||||
"""
|
||||
|
||||
|
||||
# 这里也写自己的代码实现
|
||||
def load_data(filepath):
|
||||
"""
|
||||
从文件中加载数据,若不自行正确实现,则无法通过评测,**不记录成绩**。
|
||||
"""从文件中加载数据。"""
|
||||
if not os.path.exists(filepath):
|
||||
raise FileNotFoundError(f"文件 {filepath} 不存在!")
|
||||
|
||||
data, labels = [], []
|
||||
print(f"Loading data: {filepath}")
|
||||
with open(filepath, 'r') as f:
|
||||
for line in f:
|
||||
line = line.strip().split()
|
||||
if len(line) >= 1 and line[-1].isdigit():
|
||||
line_data = [0 if x == '?' else float(x) for x in line[:-1]]
|
||||
data.append(line_data)
|
||||
labels.append(int(line[-1]))
|
||||
|
||||
Args:
|
||||
- directory (str): 数据文件路径。
|
||||
data = np.array(data)
|
||||
labels = np.array(labels)
|
||||
|
||||
num_features = data.shape[1]
|
||||
indices_to_remove = [2] # 去掉特征3(索引2)
|
||||
|
||||
if num_features > 23:
|
||||
indices_to_remove.extend(range(23, min(28, num_features)))
|
||||
|
||||
Returns:
|
||||
- np.array: 特征数据。
|
||||
- np.array: 标签数据。
|
||||
"""
|
||||
data = np.delete(data, indices_to_remove, axis=1)
|
||||
target_variable = np.where((labels == 2) | (labels == 3), 0, 1)
|
||||
|
||||
print(f"Data loaded successfully, data shape: {data.shape}, label shape: {target_variable.shape}")
|
||||
return data, target_variable
|
||||
|
||||
def k_fold_cross_validation(X, y, k=5, learning_rate=0.01, iterations=20000, batch_size=64, lambda_reg=0.01):
|
||||
"""K折交叉验证。"""
|
||||
m = X.shape[0]
|
||||
fold_size = m // k
|
||||
accuracies = []
|
||||
|
||||
for i in range(k):
|
||||
val_start = i * fold_size
|
||||
val_end = (i + 1) * fold_size if i != k - 1 else m
|
||||
|
||||
X_val = X[val_start:val_end]
|
||||
y_val = y[val_start:val_end]
|
||||
X_train = np.concatenate((X[:val_start], X[val_end:]))
|
||||
y_train = np.concatenate((y[:val_start], y[val_end:]))
|
||||
|
||||
weights = train_logreg(X_train, y_train, learning_rate, iterations, batch_size, lambda_reg)
|
||||
|
||||
val_accuracy = np.mean([predict_logreg(X_val[j], weights) == y_val[j] for j in range(len(y_val))])
|
||||
accuracies.append(val_accuracy)
|
||||
|
||||
print(f"Fold {i + 1}/{k} - Validation accuracy: {val_accuracy:.4f}")
|
||||
|
||||
return np.mean(accuracies)
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 以下代码可用于测试,可自行随意修改
|
||||
# 加载训练集
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
try:
|
||||
X_train, y_train = load_data("./data/horse-colic.data")
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# 数据标准化处理
|
||||
X_train, mean, std = standardize_data(X_train)
|
||||
|
||||
# K折交叉验证
|
||||
avg_accuracy = k_fold_cross_validation(X_train, y_train, k=5, learning_rate=0.01, iterations=20000)
|
||||
print(f"Average accuracy of K-fold cross-validation: {avg_accuracy:.4f}")
|
||||
|
||||
# 加载测试集
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
try:
|
||||
X_test, y_test = load_data("./data/horse-colic.test")
|
||||
except FileNotFoundError as e:
|
||||
print(e)
|
||||
exit()
|
||||
|
||||
# 测试集也需要进行相同的标准化
|
||||
X_test = (X_test - mean) / std
|
||||
|
||||
# 训练对率回归模型
|
||||
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=20000)
|
||||
|
||||
# 测试单个数据
|
||||
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