From ad305dda21e376c45e5f90b0841c63f0ac67393d Mon Sep 17 00:00:00 2001 From: lzh <2233673261@qq.com> Date: Sat, 2 Nov 2024 21:26:05 +0800 Subject: [PATCH] 444 --- function.py | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/function.py b/function.py index 50ed426..d49ccc4 100644 --- a/function.py +++ b/function.py @@ -42,7 +42,6 @@ def load_data(filepath): del data[26] del data[25] del data[24] - del data[23] del data[22] del data[2] for i in data_25: @@ -53,12 +52,9 @@ def load_data(filepath): 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) # 添加偏置项 - scaler = StandardScaler() - X= scaler.fit_transform(X) - return X_train, y_train -def train_logreg(X, y, numIter=1500, learning_rate=0.05, batch_size=64, lambda_reg=0.01): +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): @@ -71,10 +67,9 @@ def train_logreg(X, y, numIter=1500, learning_rate=0.05, batch_size=64, lambda_r h = 1 / (1 + np.exp(-z)) error = batch_y - h gradient = np.dot(batch_X.T, error) / len(batch_y) - # L1正则化项 - l1_reg_term = np.sign(weights) - # 更新权重,加入L1正则化 - weights -= learning_rate * (gradient + lambda_reg * l1_reg_term) + reg_term = 2 * lambda_reg * weights + gradient -= reg_term / len(batch_y) + weights += learning_rate * gradient return weights def predict_logreg(inX, weights): @@ -108,3 +103,38 @@ if __name__ == "__main__": 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)