diff --git a/function.py b/function.py index 85c93ae..6e2ab41 100644 --- a/function.py +++ b/function.py @@ -1,34 +1,65 @@ -# function.py import numpy as np -from sklearn.model_selection import train_test_split, GridSearchCV, cross_val_score -from sklearn.preprocessing import StandardScaler -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 -""" -实验说明: -- 自行编写实现数据加载与预测病马死亡的对率回归算法。 - -注意事项: -- 在 function.py 中实现对率回归算法。 -- 可以自行导入其他库,需要在 requirements.txt 添加依赖,若未在 requirements.txt 中添加使用到的依赖,则无法自动评测,成绩为零。 -- 自动评测脚本仅会加载 train_logreg、predict_logreg 和 load_data 函数,请勿修改函数名。 - -测评环境: -- CPU: Intel Xeon(R) CPU E3-1231 v3 @ 3.40GHz -- OS: Ubuntu 22.04.4 LTS -- Python: 3.10.12 -- numpy: 2.1.1 -""" - """ 请在这里填写学号和姓名,不填写即成绩无效。 """ -id = "202210423221" -name = "廖子豪" +id = "202210423224" +name = "吴杨洋" + + +def sigmoid(z): + """ + Sigmoid 激活函数。 + """ + return 1 / (1 + np.exp(-z)) + + +def train_logreg(X, y, numIter=1000, learning_rate=0.06, batch_size=128, lambda_reg=0.01): + m, n = X.shape + weights = np.zeros(n) + for j in range(numIter): + dataIndex = np.random.permutation(m) + for i in range(0, m, batch_size): + end = min(i + batch_size, m) + batch_X = X[dataIndex[i:end]] + batch_y = y[dataIndex[i:end]] + z = np.dot(batch_X, weights) + 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 + gradient -= reg_term / len(batch_y) + weights += learning_rate * gradient + return weights + + +def predict_logreg(inX, weights): + """ + 对率回归预测函数。 + + Args: + - inX (numpy array): 输入待分类的样本。 + - weights (numpy array): 训练得到的权重。 + + Returns: + - int: 预测的分类标签。 + """ + z = np.dot(inX, weights) + prob = 1 / (1 + np.exp(-z)) + return 1 if prob >= 0.5 else 0 + + def load_data(filepath): + """ + 从文件中加载数据,若不自行正确实现,则无法通过评测,不记录成绩。 + + Args: + - filepath (str): 数据文件路径。 + + Returns: + - X (np.array): 特征数据。 + - y (np.array): 标签数据。 + """ file = open(filepath) fr = file.readlines() data_list = [] @@ -54,87 +85,31 @@ 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): - m, n = X.shape - weights = np.zeros(n) - for j in range(numIter): - dataIndex = np.random.permutation(m) - for i in range(0, m, batch_size): - end = min(i + batch_size, m) - batch_X = X[dataIndex[i:end]] - batch_y = y[dataIndex[i:end]] - z = np.dot(batch_X, weights) - 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 - gradient -= reg_term / len(batch_y) - weights += learning_rate * gradient - return weights -def predict_logreg(inX, weights): - z = np.dot(inX, weights) - prob = 1 / (1 + np.exp(-z)) - return 1 if prob >= 0.5 else 0 +def standardize(X): + mean = X.mean(axis=0) + std = X.std(axis=0) + return (X - mean) / std, mean, std -if __name__ == "__main__": + +def main(): + # 加载训练集 X_train, y_train = load_data("./data/horse-colic.data") + # 加载测试集 X_test, y_test = load_data("./data/horse-colic.test") - scaler = StandardScaler() - X_train = scaler.fit_transform(X_train) - X_test = scaler.transform(X_test) + # 标准化特征数据 + X_train, mean, std = standardize(X_train) + X_test = (X_test - mean) / std - X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42) + # 训练对率回归模型 + weights = train_logreg(X_train, y_train, numIter=1000, learning_rate=0.01, lambda_reg=0.1) - 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) - y_pred = [predict_logreg(x, weights) for x in X_val] - auc = roc_auc_score(y_val, y_pred) - if auc > best_auc: - best_auc = auc - best_weights = weights - if iter > 10 and auc == best_auc: - break + # 测试单个数据 + y_pred_test = [predict_logreg(x, weights) for x in X_test] + accuracy_test = np.mean(y_pred_test == y_test) + print(f"Test Accuracy: {accuracy_test:.2f}") - 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}") - # 随机森林模型 - param_grid = { - 'n_estimators': [100, 200, 300], - 'max_depth': [5, 10, 15], - 'learning_rate': [0.01, 0.05, 0.1] - } - rf = RandomForestClassifier() - grid_search = GridSearchCV(estimator=rf, param_grid=param_grid, cv=5, scoring='accuracy') - grid_search.fit(X_train, y_train) - best_rf = grid_search.best_estimator_ - print("Best parameters:", grid_search.best_params_) - print("Best accuracy:", grid_search.best_score_) - - # 集成学习 - 投票分类器 - log_clf = LogisticRegression() - rnd_clf = RandomForestClassifier(n_estimators=100, random_state=42) - voting_clf = VotingClassifier(estimators=[('lr', log_clf), ('rf', rnd_clf)], voting='soft') - voting_clf.fit(X_train, y_train) - y_pred = voting_clf.predict(X_test) - print("Voting classifier accuracy:", accuracy_score(y_test, y_pred)) - - # 梯度提升机模型 - gb_clf = GradientBoostingClassifier() - gb_clf.fit(X_train, y_train) - y_pred = gb_clf.predict(X_test) - print("Gradient Boosting classifier accuracy:", accuracy_score(y_test, y_pred)) - - # 使用K折交叉验证评估模型性能 - scores = cross_val_score(gb_clf, X_train, y_train, cv=5, scoring='accuracy') - print("Cross-validation scores:", scores) - print("Average cross-validation score:", np.mean(scores)) - - # 使用F1分数评估模型性能 - f1 = f1_score(y_test, y_pred) - print("F1 Score:", f1) \ No newline at end of file +if __name__ == "__main__": + main() \ No newline at end of file