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

This commit is contained in:
2024-11-03 17:19:14 +08:00
parent decffd768d
commit 7262f8b839
-11
View File
@@ -1,6 +1,3 @@
import os
import numpy as np
"""
实验说明:
- 自行编写实现数据加载与预测病马死亡的对率回归算法。
@@ -23,16 +20,13 @@ import numpy as np
id = "202210423105"
name = "魏明帅"
# 这里写自己的代码实现
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
def sigmoid(z):
return 1.0 / (1 + np.exp(-z))
def compute_gradient(X_batch, y_batch, weights, lambda_reg):
z = np.dot(X_batch, weights)
h = sigmoid(z)
@@ -40,7 +34,6 @@ def compute_gradient(X_batch, y_batch, weights, lambda_reg):
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):
m, n = X.shape
weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化
@@ -70,13 +63,11 @@ def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lam
return best_weights
def predict_logreg(X, weights):
z = np.dot(X, weights)
h = sigmoid(z)
return np.where(h >= 0.5, 1, 0)
def load_data(filepath):
data, labels = [], []
with open(filepath, 'r') as f:
@@ -106,12 +97,10 @@ def load_data(filepath):
return data, target_variable
def accuracy(X, y, weights):
predictions = predict_logreg(X, weights)
return np.mean(predictions == y)
# 其余代码保持不变
if __name__ == "__main__":
# 加载数据