diff --git a/function.py b/function.py index 505db58..d074dd0 100644 --- a/function.py +++ b/function.py @@ -1,5 +1,8 @@ # function.py import numpy as np +from sklearn.model_selection import train_test_split +from sklearn.preprocessing import StandardScaler +from sklearn.metrics import accuracy_score """ 实验说明: - 自行编写实现数据加载与预测病马死亡的对率回归算法。 @@ -123,16 +126,28 @@ if __name__ == "__main__": # 加载测试集 X_test, y_test = load_data("./data/horse-colic.test") - # 训练对率回归模型 - weights = train_logreg(X_train, y_train, numIter=1000, learning_rate=0.01, lambda_reg=0.2) + # 特征缩放 + scaler = StandardScaler() + X_train = scaler.fit_transform(X_train) + X_test = scaler.transform(X_test) - # 测试模型 - error = 0 - accuracy_list = [] - for i in range(len(y_test)): - y_pred = predict_logreg(X_test[i], weights) - if y_pred != y_test[i]: - error += 1 - print(f"预测结果: {y_pred}, 实际结果: {y_test[i]}") - accuracy = (len(y_test) - error) / len(y_test) - print(f"Accuracy: {accuracy}") \ No newline at end of file + # 划分训练集和验证集 + X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42) + + # 训练对率回归模型,使用早停法 + best_weights = None + best_accuracy = 0 + for iter in range(1, 1001): + weights = train_logreg(X_train, y_train, numIter=iter, learning_rate=0.01, lambda_reg=0.1) + y_pred = [predict_logreg(x, weights) for x in X_val] + accuracy = accuracy_score(y_val, y_pred) + if accuracy > best_accuracy: + best_accuracy = accuracy + best_weights = weights + if iter > 10 and accuracy == best_accuracy: + break # 早停 + + # 使用最佳权重在测试集上评估模型 + y_pred_test = [predict_logreg(x, best_weights) for x in X_test] + accuracy_test = accuracy_score(y_test, y_pred_test) + print(f"Test Accuracy: {accuracy_test}") \ No newline at end of file