From 48e2a405f061047464fd797f5ba16376732fee63 Mon Sep 17 00:00:00 2001 From: 43606070 <43606070@qq.com> Date: Thu, 31 Oct 2024 10:48:16 +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 | 70 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/function.py b/function.py index 881308e..40c5549 100644 --- a/function.py +++ b/function.py @@ -1,12 +1,14 @@ import os import csv import numpy as np -from sklearn.model_selection import train_test_split, GridSearchCV -from sklearn.linear_model import LogisticRegression +import torch +import torch.nn as nn +import torch.optim as optim +from sklearn.model_selection import train_test_split 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 +from torch.utils.data import DataLoader, TensorDataset # 读取 CSV 数据的函数 def load_data(file_path): @@ -35,6 +37,15 @@ def evaluate_model(y_true, y_pred, y_prob=None): auc = roc_auc_score(y_true, y_prob) print(f"AUC Score: {auc:.4f}") +# 定义逻辑回归模型 +class LogisticRegressionModel(nn.Module): + def __init__(self, input_dim): + super(LogisticRegressionModel, self).__init__() + self.linear = nn.Linear(input_dim, 1) # 线性层 + + def forward(self, x): + return torch.sigmoid(self.linear(x)) # 使用 Sigmoid 激活函数 + # 主函数 def main(): # 加载数据 @@ -48,28 +59,45 @@ def main(): smote = SMOTE(random_state=42) X_train_res, y_train_res = smote.fit_resample(X_train, y_train) - # 创建管道,包含标准化和逻辑回归 - pipeline = Pipeline([ - ('scaler', StandardScaler()), # 数据标准化 - ('classifier', LogisticRegression(max_iter=1000)) # 逻辑回归 - ]) + # 标准化数据 + scaler = StandardScaler() + X_train_res = scaler.fit_transform(X_train_res) + X_test = scaler.transform(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_}") + # 将数据转换为 Tensor + X_train_tensor = torch.FloatTensor(X_train_res) + y_train_tensor = torch.FloatTensor(y_train_res).view(-1, 1) + X_test_tensor = torch.FloatTensor(X_test) + y_test_tensor = torch.FloatTensor(y_test).view(-1, 1) - # 使用最佳模型进行预测 - best_model = grid_search.best_estimator_ - y_pred = best_model.predict(X_test) - y_prob = best_model.predict_proba(X_test)[:, 1] # 用于 AUC 计算 + # 创建数据加载器 + train_dataset = TensorDataset(X_train_tensor, y_train_tensor) + train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True) + + # 初始化模型、损失函数和优化器 + input_dim = X_train_res.shape[1] + model = LogisticRegressionModel(input_dim) + criterion = nn.BCELoss() # 二元交叉熵损失 + optimizer = optim.SGD(model.parameters(), lr=0.01) + + # 训练模型 + model.train() + for epoch in range(100): # 训练 100 个 epoch + for inputs, labels in train_loader: + optimizer.zero_grad() + outputs = model(inputs) + loss = criterion(outputs, labels) + loss.backward() + optimizer.step() # 评估模型 - evaluate_model(y_test, y_pred, y_prob) + model.eval() + with torch.no_grad(): + y_pred_prob = model(X_test_tensor).numpy() + y_pred = (y_pred_prob > 0.5).astype(int) + + # 评估模型 + evaluate_model(y_test, y_pred, y_pred_prob) if __name__ == "__main__": main() -