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

This commit is contained in:
Wyy
2024-11-02 20:38:28 +08:00
parent cae2705f28
commit 73d12cd545
+4 -4
View File
@@ -7,13 +7,13 @@ id = "202210423224"
name = "吴杨洋"
def sigmoid(z):
"""
Sigmoid 激活函数。
"""
return 1 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.01, iterations=1000):
"""
对率回归训练函数。
@@ -37,7 +37,6 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000):
return weights
def predict_logreg(inX, weights):
"""
对率回归预测函数。
@@ -53,6 +52,7 @@ def predict_logreg(inX, weights):
y_predicted = sigmoid(linear_model)
return 1 if y_predicted > 0.5 else 0
def load_data(filepath):
"""
从文件中加载数据,若不自行正确实现,则无法通过评测,不记录成绩。
@@ -64,11 +64,12 @@ def load_data(filepath):
- X (np.array): 特征数据。
- y (np.array): 标签数据。
"""
data = np.loadtxt(filepath, delimiter=',')
data = np.genfromtxt(filepath, delimiter=',', dtype=float, filling_values=0)
X = data[:, :-1]
y = data[:, -1]
return X, y
if __name__ == "__main__":
# 加载训练集
X_train, y_train = load_data("./data/horse-colic.data")
@@ -76,7 +77,6 @@ if __name__ == "__main__":
# 加载测试集
X_test, y_test = load_data("./data/horse-colic.test")
# 训练对率回归模型
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000)