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

This commit is contained in:
2024-11-02 14:22:12 +08:00
parent 0e1e2c9644
commit 02e212bb53
+25 -6
View File
@@ -29,13 +29,13 @@ name = "章程鸿"
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import train_test_split, KFold
def sigmoid(z):
"""Sigmoid 激活函数。"""
return 1 / (1 + np.exp(-z))
def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01, batch_size=32):
def train_logreg(X, y, learning_rate=0.1, iterations=2000, lambda_reg=0.01, batch_size=32):
"""逻辑回归训练函数,随机初始化权重,并在每次迭代前打乱数据。"""
m, n = X.shape
weights = np.random.randn(n) * 0.01 # 随机初始化权重,取较小值
@@ -61,7 +61,8 @@ def train_logreg(X, y, learning_rate=0.01, iterations=1000, lambda_reg=0.01, bat
weights -= learning_rate * gradient
# 动态调整学习率,逐渐减小
learning_rate *= 0.99
if i % 100 == 0: # 每100次迭代输出一次学习率
print(f"Iteration {i}, Learning Rate: {learning_rate}")
return weights
@@ -117,9 +118,27 @@ if __name__ == "__main__":
# 使用 train_test_split 打乱并随机抽取数据
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 训练逻辑回归模型,设置 batch_size
batch_size = 32
weights = train_logreg(X_train, y_train, learning_rate=0.1, iterations=1000, lambda_reg=0.01, batch_size=batch_size)
# K折交叉验证
k = 5
kf = KFold(n_splits=k, shuffle=True, random_state=42)
accuracies = []
for train_index, val_index in kf.split(X_train):
X_train_kf, X_val_kf = X_train[train_index], X_train[val_index]
y_train_kf, y_val_kf = y_train[train_index], y_train[val_index]
# 训练逻辑回归模型,设置 batch size
batch_size = 32
weights = train_logreg(X_train_kf, y_train_kf, learning_rate=0.1, iterations=2000, lambda_reg=0.01, batch_size=batch_size)
# 验证模型
val_predictions = [predict_logreg(X_val_kf[i], weights) for i in range(len(X_val_kf))]
accuracy = np.mean(val_predictions == y_val_kf)
accuracies.append(accuracy)
# 输出交叉验证的准确率和平均准确率
print(f"K折交叉验证得分: {accuracies}")
print(f"平均准确率: {np.mean(accuracies)}")
# 测试单个数据
y_pred = predict_logreg(X_test[11], weights)