444
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 24s

This commit is contained in:
lzh
2024-11-02 21:29:41 +08:00
parent ad305dda21
commit 02d09c2ab6
+10 -5
View File
@@ -1,4 +1,3 @@
# function.py
import numpy as np import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score
from sklearn.preprocessing import StandardScaler 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.ensemble import RandomForestClassifier, GradientBoostingClassifier, VotingClassifier
from sklearn.linear_model import LogisticRegression from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC from sklearn.svm import SVC
""" """
实验说明: 实验说明:
- 自行编写实现数据加载与预测病马死亡的对率回归算法。 - 自行编写实现数据加载与预测病马死亡的对率回归算法。
@@ -22,12 +22,12 @@ from sklearn.svm import SVC
- numpy: 2.1.1 - numpy: 2.1.1
""" """
""" """
请在这里填写学号和姓名,不填写即成绩无效。 请在这里填写学号和姓名,不填写即成绩无效。
""" """
id = "202210423221" id = "202210423221"
name = "廖子豪" name = "廖子豪"
def load_data(filepath): def load_data(filepath):
file = open(filepath) file = open(filepath)
fr = file.readlines() fr = file.readlines()
@@ -54,7 +54,7 @@ def load_data(filepath):
X_train = np.insert(X, 0, 1, axis=1) # 添加偏置项 X_train = np.insert(X, 0, 1, axis=1) # 添加偏置项
return X_train, y_train 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 m, n = X.shape
weights = np.zeros(n) weights = np.zeros(n)
for j in range(numIter): 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)) h = 1 / (1 + np.exp(-z))
error = batch_y - h error = batch_y - h
gradient = np.dot(batch_X.T, error) / len(batch_y) gradient = np.dot(batch_X.T, error) / len(batch_y)
if penalty == 'l1':
reg_term = lambda_reg * np.sign(weights)
elif penalty == 'l2':
reg_term = 2 * lambda_reg * weights reg_term = 2 * lambda_reg * weights
else:
raise ValueError("Unsupported penalty type. Use 'l1' or 'l2'.")
gradient -= reg_term / len(batch_y) gradient -= reg_term / len(batch_y)
weights += learning_rate * gradient weights -= learning_rate * gradient
return weights return weights
def predict_logreg(inX, weights): def predict_logreg(inX, weights):
@@ -90,7 +95,7 @@ if __name__ == "__main__":
best_weights = None best_weights = None
best_auc = 0 best_auc = 0
for iter in range(1, 1001): 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] y_pred = [predict_logreg(x, weights) for x in X_val]
auc = roc_auc_score(y_val, y_pred) auc = roc_auc_score(y_val, y_pred)
if auc > best_auc: if auc > best_auc: