From 8f9194ec43509ce0da7fe244aa6df81f7b5191af Mon Sep 17 00:00:00 2001 From: 43606070 <43606070@qq.com> Date: Thu, 31 Oct 2024 10:45:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20function.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- function.py | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/function.py b/function.py index e401431..881308e 100644 --- a/function.py +++ b/function.py @@ -1,12 +1,18 @@ +import os import csv import numpy as np -from sklearn.model_selection import train_test_split +from sklearn.model_selection import train_test_split, GridSearchCV from sklearn.linear_model import LogisticRegression -from sklearn.metrics import classification_report, confusion_matrix +from sklearn.metrics import classification_report, confusion_matrix, accuracy_score, roc_auc_score +from sklearn.preprocessing import StandardScaler +from sklearn.pipeline import Pipeline from imblearn.over_sampling import SMOTE # 读取 CSV 数据的函数 def load_data(file_path): + if not os.path.exists(file_path): + raise FileNotFoundError(f"文件未找到: {file_path}") + features = [] labels = [] with open(file_path, 'r') as csvfile: @@ -18,11 +24,16 @@ def load_data(file_path): return np.array(features), np.array(labels) # 评估模型的函数 -def evaluate_model(y_true, y_pred): +def evaluate_model(y_true, y_pred, y_prob=None): print("Confusion Matrix:") print(confusion_matrix(y_true, y_pred)) print("\nClassification Report:") print(classification_report(y_true, y_pred)) + print(f"Accuracy: {accuracy_score(y_true, y_pred):.4f}") + + if y_prob is not None: + auc = roc_auc_score(y_true, y_prob) + print(f"AUC Score: {auc:.4f}") # 主函数 def main(): @@ -37,15 +48,28 @@ def main(): smote = SMOTE(random_state=42) X_train_res, y_train_res = smote.fit_resample(X_train, y_train) - # 训练逻辑回归模型 - model = LogisticRegression(max_iter=1000) - model.fit(X_train_res, y_train_res) + # 创建管道,包含标准化和逻辑回归 + pipeline = Pipeline([ + ('scaler', StandardScaler()), # 数据标准化 + ('classifier', LogisticRegression(max_iter=1000)) # 逻辑回归 + ]) - # 进行预测 - y_pred = model.predict(X_test) + # 网格搜索以优化正则化参数 C + param_grid = {'classifier__C': [0.01, 0.1, 1, 10, 100]} + grid_search = GridSearchCV(pipeline, param_grid, cv=5, scoring='accuracy') + grid_search.fit(X_train_res, y_train_res) + + # 输出最佳参数 + print(f"Best Parameters: {grid_search.best_params_}") + + # 使用最佳模型进行预测 + best_model = grid_search.best_estimator_ + y_pred = best_model.predict(X_test) + y_prob = best_model.predict_proba(X_test)[:, 1] # 用于 AUC 计算 # 评估模型 - evaluate_model(y_test, y_pred) + evaluate_model(y_test, y_pred, y_prob) if __name__ == "__main__": main() +