From c73871005adc28874591c47a2cc5e673c1eb3515 Mon Sep 17 00:00:00 2001 From: lzh <2233673261@qq.com> Date: Sat, 2 Nov 2024 21:33:01 +0800 Subject: [PATCH] 4544444 --- function.py | 142 ++++++++++++---------------------------------------- 1 file changed, 33 insertions(+), 109 deletions(-) diff --git a/function.py b/function.py index 7c7d0df..0d7691d 100644 --- a/function.py +++ b/function.py @@ -1,10 +1,6 @@ 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 """ 实验说明: @@ -29,117 +25,45 @@ id = "202210423221" name = "廖子豪" def load_data(filepath): - file = open(filepath) - fr = file.readlines() - data_list = [] - y = [] - for data in fr: - data = data.strip() - data = data.split(' ') - data_25 = data[24] - y.append(data[22]) - del data[-1] - del data[26] - del data[25] - del data[24] - del data[22] - del data[2] - for i in data_25: - data.append(i) - data = [0 if data_elem == '?' else float(data_elem) for data_elem in data] - data_list.append(data) - data_number = len(y) - y_train = np.array([1 if y[j] == '2' or y[j] == '3' else 0 for j in range(data_number)]) - X = np.array(data_list) - X_train = np.insert(X, 0, 1, axis=1) # 添加偏置项 - return X_train, y_train + # 使用 numpy 的 genfromtxt 函数加载数据,自动处理缺失值 + data = np.genfromtxt(filepath, delimiter=' ', missing_values='?', filling_values=0) + labels = data[:, -1].astype(int) + features = data[:, :-1] + + # 将标签转换为二分类问题 + labels = np.where((labels == 2) | (labels == 3), 1, 0) + + # 删除不相关特征(如果需要) + # features = np.delete(features, [2] + list(range(23, 28)), axis=1) + + # 数据标准化 + scaler = StandardScaler() + features = scaler.fit_transform(features) + + return features, labels -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): - 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) - 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 - return weights +def train_logreg(X, y, random_state=42): + # 创建逻辑回归模型,使用liblinear求解器 + model = LogisticRegression(solver='liblinear', random_state=random_state) + model.fit(X, y) + return model -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 predict_logreg(inX, model): + # 使用模型进行预测 + return int(model.predict([inX])[0]) +# 示例使用 if __name__ == "__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) + # 训练模型 + model = train_logreg(X_train, y_train, random_state=42) - X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42) + # 进行预测 + y_pred = [predict_logreg(x, model) for x in X_test] - 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, 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: - best_auc = auc - best_weights = weights - if iter > 10 and auc == best_auc: - 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}") - - # 随机森林模型 - 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 + # 计算准确率 + accuracy = np.mean(y_pred == y_test) + print(f"Test Accuracy: {accuracy:.2f}") \ No newline at end of file