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

This commit is contained in:
2024-11-01 11:11:29 +08:00
parent c170971ce0
commit 12ca8dd66c
+12 -6
View File
@@ -9,7 +9,7 @@ name = "董秋"
def train_logreg(X, y, learning_rate=0.01, iterations=1500, lambda_reg=0.01):
"""
对率回归训练函数。
逻辑回归训练函数。
Args:
- X (numpy array): 特征数据。
@@ -22,6 +22,13 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1500, lambda_reg=0.01):
weights = np.zeros(n)
for i in range(iterations):
# 每100次迭代后随机打乱数据
if i % 100 == 0:
indices = np.arange(m)
np.random.shuffle(indices)
X = X[indices]
y = y[indices]
z = np.dot(X, weights)
predictions = 1 / (1 + np.exp(-z))
gradient = np.dot(X.T, (predictions - y)) / m
@@ -32,14 +39,15 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1500, lambda_reg=0.01):
# 更新权重
weights -= learning_rate * gradient
# 动态调整学习率
learning_rate *= 0.99 # 每次迭代将学习率乘以0.99
# 每100次迭代后衰减学习率
if i % 100 == 0 and i > 0: # 确保不在第一次迭代后衰减学习率
learning_rate *= 0.99 # 每次100次迭代将学习率乘以0.99
return weights
def predict_logreg(inX, weights):
"""
对率回归预测函数。
逻辑回归预测函数。
Args:
- inX (numpy array): 输入待分类的样本。
@@ -84,9 +92,7 @@ def preprocess_data(X):
X_poly = poly.fit_transform(X_scaled)
# 例如,可以对某个特征进行分箱处理
# 假设 X_scaled[:, 0] 是一个连续特征,比如年龄
age_bins = pd.cut(X_scaled[:, 0], bins=[0, 30, 60, 100], labels=["年轻", "中年", "老年"])
# 将分箱结果转换为独热编码
age_dummies = pd.get_dummies(age_bins, drop_first=True)
# 将独热编码的结果与原特征组合