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

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