From 02d09c2ab6caf62cd8c458fa16535f46d3a00a1d Mon Sep 17 00:00:00 2001 From: lzh <2233673261@qq.com> Date: Sat, 2 Nov 2024 21:29:41 +0800 Subject: [PATCH] 444 --- function.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/function.py b/function.py index d49ccc4..7c7d0df 100644 --- a/function.py +++ b/function.py @@ -1,4 +1,3 @@ -# function.py import numpy as np from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score from sklearn.preprocessing import StandardScaler @@ -6,6 +5,7 @@ from sklearn.metrics import accuracy_score, roc_auc_score, f1_score from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier from sklearn.linear_model import LogisticRegression from sklearn.svm import SVC + """ 实验说明: - 自行编写实现数据加载与预测病马死亡的对率回归算法。 @@ -22,12 +22,12 @@ from sklearn.svm import SVC - numpy: 2.1.1 """ - """ 请在这里填写学号和姓名,不填写即成绩无效。 """ id = "202210423221" name = "廖子豪" + def load_data(filepath): file = open(filepath) fr = file.readlines() @@ -54,7 +54,7 @@ def load_data(filepath): X_train = np.insert(X, 0, 1, axis=1) # 添加偏置项 return X_train, y_train -def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128, lambda_reg=0.01): +def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128, lambda_reg=0.01, penalty='l2'): m, n = X.shape weights = np.zeros(n) for j in range(numIter): @@ -67,9 +67,14 @@ def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128, lambda_ h = 1 / (1 + np.exp(-z)) error = batch_y - h gradient = np.dot(batch_X.T, error) / len(batch_y) - reg_term = 2 * lambda_reg * weights + if penalty == 'l1': + reg_term = lambda_reg * np.sign(weights) + elif penalty == 'l2': + reg_term = 2 * lambda_reg * weights + else: + raise ValueError("Unsupported penalty type. Use 'l1' or 'l2'.") gradient -= reg_term / len(batch_y) - weights += learning_rate * gradient + weights -= learning_rate * gradient return weights def predict_logreg(inX, weights): @@ -90,7 +95,7 @@ if __name__ == "__main__": best_weights = None best_auc = 0 for iter in range(1, 1001): - weights = train_logreg(X_train, y_train, numIter=iter, learning_rate=0.01, lambda_reg=0.1) + weights = train_logreg(X_train, y_train, numIter=iter, learning_rate=0.01, lambda_reg=0.1, penalty='l2') y_pred = [predict_logreg(x, weights) for x in X_val] auc = roc_auc_score(y_val, y_pred) if auc > best_auc: @@ -137,4 +142,4 @@ if __name__ == "__main__": # 使用F1分数评估模型性能 f1 = f1_score(y_test, y_pred) - print("F1 Score:", f1) + print("F1 Score:", f1) \ No newline at end of file