更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 22s

This commit is contained in:
Wyy
2024-11-02 21:34:52 +08:00
parent 130bec1f67
commit 07daa3d4dd
+19 -48
View File
@@ -1,61 +1,40 @@
import numpy as np
"""
请在这里填写学号和姓名,不填写即成绩无效。
"""
id = "202210423224"
name = "吴杨洋"
def sigmoid(z, epsilon=1e-15):
"""Sigmoid 激活函数,添加 epsilon 防止溢出"""
s = 1 / (1 + np.exp(-z))
s = np.clip(s, epsilon, 1 - epsilon) # 防止溢出
return s
def sigmoid(z):
"""
Sigmoid 激活函数。
"""
return 1 / (1 + np.exp(-z))
def train_logreg(X, y, numIter=1000, learning_rate=0.01, lambda_reg=0.01):
def train_logreg(X, y, numIter=1000, learning_rate=0.01, lambda_reg=0.01, batch_size=64):
m, n = X.shape
weights = np.zeros(n)
for j in range(numIter):
for i in range(m):
z = np.dot(X[i], weights)
indices = np.random.permutation(m)
X_shuffled = X[indices]
y_shuffled = y[indices]
for i in range(0, m, batch_size):
end = min(i + batch_size, m)
batch_X = X_shuffled[i:end]
batch_y = y_shuffled[i:end]
z = np.dot(batch_X, weights)
h = sigmoid(z)
error = y[i] - h
gradient = X[i] * error
error = batch_y - h
gradient = np.dot(batch_X.T, error) / len(batch_y)
reg_term = lambda_reg * weights
gradient += reg_term
weights += learning_rate * gradient
gradient += reg_term / len(batch_y)
weights -= learning_rate * gradient
return weights
def predict_logreg(inX, weights):
"""
对率回归预测函数。
Args:
- inX (numpy array): 输入待分类的样本。
- weights (numpy array): 训练得到的权重。
Returns:
- int: 预测的分类标签。
"""
z = np.dot(inX, weights)
prob = sigmoid(z)
return 1 if prob >= 0.5 else 0
def load_data(filepath):
"""
从文件中加载数据,若不自行正确实现,则无法通过评测,不记录成绩。
Args:
- filepath (str): 数据文件路径。
Returns:
- X (np.array): 特征数据。
- y (np.array): 标签数据。
"""
file = open(filepath)
fr = file.readlines()
data_list = []
@@ -81,31 +60,23 @@ def load_data(filepath):
X_train = np.insert(X, 0, 1, axis=1) # 添加偏置项
return X_train, y_train
def standardize(X):
mean = X.mean(axis=0)
std = X.std(axis=0)
return (X - mean) / std, mean, std
def main():
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")
# 加载测试集
X_test, y_test = load_data("./data/horse-colic.test")
# 标准化特征数据
X_train, mean, std = standardize(X_train)
X_test = (X_test - mean) / std
# 训练对率回归模型
weights = train_logreg(X_train, y_train, numIter=1000, learning_rate=0.01, lambda_reg=0.1)
weights = train_logreg(X_train, y_train, numIter=1000, learning_rate=0.01, lambda_reg=0.1, batch_size=64)
# 测试单个数据
y_pred_test = [predict_logreg(x, weights) for x in X_test]
accuracy_test = np.mean(y_pred_test == y_test)
print(f"Test Accuracy: {accuracy_test:.2f}")
if __name__ == "__main__":
main()