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

This commit is contained in:
sn
2024-11-02 22:10:26 +08:00
parent 175b7fa4f2
commit e1728f97a4
+25 -22
View File
@@ -2,7 +2,7 @@
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from sklearn.impute import SimpleImputer from sklearn.impute import SimpleImputer
from sklearn.preprocessing import MinMaxScaler from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report from sklearn.metrics import accuracy_score, classification_report
@@ -44,11 +44,11 @@ def compute_gradient(X_batch, y_batch, weights, lambda_reg):
return gradient return gradient
# train_logreg函数,用于训练逻辑回归模型 # train_logreg函数,用于训练逻辑回归模型
def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lambda_reg=0.1): def train_logreg(X, y, learning_rate=0.01, iterations=1000, batch_size=64, lambda_reg=0.1):
m, n = X.shape m, n = X.shape
weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化 weights = np.zeros(n)
best_weights = weights.copy()
best_accuracy = 0 best_accuracy = 0
best_weights = weights.copy()
for i in range(iterations): for i in range(iterations):
indices = np.random.permutation(m) indices = np.random.permutation(m)
@@ -61,22 +61,23 @@ def train_logreg(X, y, learning_rate=0.001, iterations=10000, batch_size=64, lam
gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg) gradient = compute_gradient(X_batch, y_batch, weights, lambda_reg)
weights -= learning_rate * gradient weights -= learning_rate * gradient
# 计算训练集准确率
accuracy = accuracy_score(y, predict_logreg(X, weights)) accuracy = accuracy_score(y, predict_logreg(X, weights))
if accuracy > best_accuracy: if accuracy > best_accuracy:
best_accuracy = accuracy best_accuracy = accuracy
best_weights = weights.copy() best_weights = weights.copy()
if i % 1000 == 0: # 每1000次迭代调整学习率 # 动态调整学习率
if best_accuracy > 0.95: # 如果达到高准确率,则减小学习率 if i % 100 == 0 and accuracy > 0.95: # 达到高准确率时减小学习率
learning_rate *= 0.9 learning_rate *= 0.9
return best_weights return best_weights
# predict_logreg函数,用于使用训练好的权重进行预测 # predict_logreg函数,用于使用训练好的权重进行预测
def predict_logreg(X, weights): def predict_logreg(inX, weights):
z = np.dot(X, weights) linear_output = np.dot(inX, weights)
h = sigmoid(z) probability = sigmoid(linear_output)
return np.where(h >= 0.5, 1, 0) return np.where(probability > 0.5, 1, 0)
# preprocess_data函数,用于预处理数据 # preprocess_data函数,用于预处理数据
def preprocess_data(X, y): def preprocess_data(X, y):
@@ -107,22 +108,24 @@ def load_data(filepath):
def main(): def main():
X, y = load_data("./data/horse-colic.data") X, y = load_data("./data/horse-colic.data")
if X is not None: if X is not None:
# 分割数据集为训练集和验证集 # 分割数据集为训练集和测试集
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 数据归一化 # 特征缩放
scaler = MinMaxScaler() scaler = StandardScaler()
X_train = scaler.fit_transform(X_train) X_train = scaler.fit_transform(X_train)
X_val = scaler.transform(X_val) X_test = scaler.transform(X_test)
# 训练模型 # 训练模型
weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=5000, batch_size=64, lambda_reg=0.1) weights = train_logreg(X_train, y_train, learning_rate=0.01, iterations=1000, batch_size=64, lambda_reg=0.1)
# 验证模型 # 预测测试集
y_pred = predict_logreg(X_val, weights) y_pred = predict_logreg(X_test, weights)
val_accuracy = accuracy_score(y_val, y_pred)
print(f"验证集的准确率: {val_accuracy:.4f}") # 计算准确率
print(classification_report(y_val, y_pred)) accuracy = accuracy_score(y_test, y_pred)
print(f"准确率: {accuracy}")
print(classification_report(y_test, y_pred))
if __name__ == "__main__": if __name__ == "__main__":
main() main()