diff --git a/function.py b/function.py index 7badbae..9a7f323 100644 --- a/function.py +++ b/function.py @@ -2,7 +2,7 @@ import numpy as np import pandas as pd 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.metrics import accuracy_score, classification_report @@ -44,11 +44,11 @@ def compute_gradient(X_batch, y_batch, weights, lambda_reg): return gradient # 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 - weights = np.random.randn(n) * np.sqrt(2. / n) # He初始化 - best_weights = weights.copy() + weights = np.zeros(n) best_accuracy = 0 + best_weights = weights.copy() for i in range(iterations): 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) weights -= learning_rate * gradient + # 计算训练集准确率 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 + # 动态调整学习率 + if i % 100 == 0 and accuracy > 0.95: # 达到高准确率时减小学习率 + learning_rate *= 0.9 return best_weights # predict_logreg函数,用于使用训练好的权重进行预测 -def predict_logreg(X, weights): - z = np.dot(X, weights) - h = sigmoid(z) - return np.where(h >= 0.5, 1, 0) +def predict_logreg(inX, weights): + linear_output = np.dot(inX, weights) + probability = sigmoid(linear_output) + return np.where(probability > 0.5, 1, 0) # preprocess_data函数,用于预处理数据 def preprocess_data(X, y): @@ -107,22 +108,24 @@ def load_data(filepath): def main(): X, y = load_data("./data/horse-colic.data") 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_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) - val_accuracy = accuracy_score(y_val, y_pred) - print(f"验证集的准确率: {val_accuracy:.4f}") - print(classification_report(y_val, y_pred)) + # 预测测试集 + y_pred = predict_logreg(X_test, weights) + + # 计算准确率 + accuracy = accuracy_score(y_test, y_pred) + print(f"准确率: {accuracy}") + print(classification_report(y_test, y_pred)) if __name__ == "__main__": main() \ No newline at end of file