更新 function.py
Gitea Actions Experiment Test / Experiment-Actions (push) Successful in 11s

This commit is contained in:
2024-10-31 10:48:16 +08:00
parent 8f9194ec43
commit 48e2a405f0
+48 -20
View File
@@ -1,12 +1,14 @@
import os import os
import csv import csv
import numpy as np import numpy as np
from sklearn.model_selection import train_test_split, GridSearchCV import torch
from sklearn.linear_model import LogisticRegression 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.metrics import classification_report, confusion_matrix, accuracy_score, roc_auc_score
from sklearn.preprocessing import StandardScaler from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from imblearn.over_sampling import SMOTE from imblearn.over_sampling import SMOTE
from torch.utils.data import DataLoader, TensorDataset
# 读取 CSV 数据的函数 # 读取 CSV 数据的函数
def load_data(file_path): 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) auc = roc_auc_score(y_true, y_prob)
print(f"AUC Score: {auc:.4f}") 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(): def main():
# 加载数据 # 加载数据
@@ -48,28 +59,45 @@ def main():
smote = SMOTE(random_state=42) smote = SMOTE(random_state=42)
X_train_res, y_train_res = smote.fit_resample(X_train, y_train) X_train_res, y_train_res = smote.fit_resample(X_train, y_train)
# 创建管道,包含标准化和逻辑回归 # 标准化数据
pipeline = Pipeline([ scaler = StandardScaler()
('scaler', StandardScaler()), # 数据标准化 X_train_res = scaler.fit_transform(X_train_res)
('classifier', LogisticRegression(max_iter=1000)) # 逻辑回归 X_test = scaler.transform(X_test)
])
# 网格搜索以优化正则化参数 C # 将数据转换为 Tensor
param_grid = {'classifier__C': [0.01, 0.1, 1, 10, 100]} X_train_tensor = torch.FloatTensor(X_train_res)
grid_search = GridSearchCV(pipeline, param_grid, cv=5, scoring='accuracy') y_train_tensor = torch.FloatTensor(y_train_res).view(-1, 1)
grid_search.fit(X_train_res, y_train_res) X_test_tensor = torch.FloatTensor(X_test)
y_test_tensor = torch.FloatTensor(y_test).view(-1, 1)
# 输出最佳参数 # 创建数据加载器
print(f"Best Parameters: {grid_search.best_params_}") train_dataset = TensorDataset(X_train_tensor, y_train_tensor)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
# 使用最佳模型进行预测 # 初始化模型、损失函数和优化器
best_model = grid_search.best_estimator_ input_dim = X_train_res.shape[1]
y_pred = best_model.predict(X_test) model = LogisticRegressionModel(input_dim)
y_prob = best_model.predict_proba(X_test)[:, 1] # 用于 AUC 计算 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__": if __name__ == "__main__":
main() main()