generated from LufsX/Experiment-LogReg
This commit is contained in:
+33
-9
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user