generated from LufsX/Experiment-LogReg
This commit is contained in:
+43
-12
@@ -1,12 +1,18 @@
|
|||||||
import os
|
import os
|
||||||
import csv
|
import csv
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import tensorflow as tf
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torch.optim as optim
|
||||||
from sklearn.model_selection import train_test_split
|
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 imblearn.over_sampling import SMOTE
|
from imblearn.over_sampling import SMOTE
|
||||||
|
|
||||||
|
# 姓名和学号
|
||||||
|
id = 202210423118
|
||||||
|
name = 刘国庆
|
||||||
|
|
||||||
# 读取 CSV 数据的函数
|
# 读取 CSV 数据的函数
|
||||||
def load_data(file_path):
|
def load_data(file_path):
|
||||||
if not os.path.exists(file_path):
|
if not os.path.exists(file_path):
|
||||||
@@ -34,6 +40,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_size):
|
||||||
|
super(LogisticRegressionModel, self).__init__()
|
||||||
|
self.fc = nn.Linear(input_size, 1)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return torch.sigmoid(self.fc(x))
|
||||||
|
|
||||||
# 主函数
|
# 主函数
|
||||||
def main():
|
def main():
|
||||||
# 加载数据
|
# 加载数据
|
||||||
@@ -52,24 +67,40 @@ def main():
|
|||||||
X_train_res = scaler.fit_transform(X_train_res)
|
X_train_res = scaler.fit_transform(X_train_res)
|
||||||
X_test = scaler.transform(X_test)
|
X_test = scaler.transform(X_test)
|
||||||
|
|
||||||
# 创建 Keras 模型
|
# 转换为 PyTorch 张量
|
||||||
model = tf.keras.Sequential([
|
X_train_res = torch.tensor(X_train_res, dtype=torch.float32)
|
||||||
tf.keras.layers.InputLayer(input_shape=(X_train_res.shape[1],)),
|
y_train_res = torch.tensor(y_train_res, dtype=torch.float32).view(-1, 1)
|
||||||
tf.keras.layers.Dense(1, activation='sigmoid') # 逻辑回归输出
|
X_test = torch.tensor(X_test, dtype=torch.float32)
|
||||||
])
|
y_test = torch.tensor(y_test, dtype=torch.float32).view(-1, 1)
|
||||||
|
|
||||||
# 编译模型
|
# 创建模型
|
||||||
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
model = LogisticRegressionModel(input_size=X_train_res.shape[1])
|
||||||
|
|
||||||
|
# 定义损失函数和优化器
|
||||||
|
criterion = nn.BCELoss()
|
||||||
|
optimizer = optim.Adam(model.parameters(), lr=0.001)
|
||||||
|
|
||||||
# 训练模型
|
# 训练模型
|
||||||
model.fit(X_train_res, y_train_res, epochs=100, batch_size=32, verbose=1)
|
model.train()
|
||||||
|
for epoch in range(100):
|
||||||
|
optimizer.zero_grad() # 清空梯度
|
||||||
|
outputs = model(X_train_res) # 前向传播
|
||||||
|
loss = criterion(outputs, y_train_res) # 计算损失
|
||||||
|
loss.backward() # 反向传播
|
||||||
|
optimizer.step() # 更新权重
|
||||||
|
|
||||||
|
if (epoch + 1) % 10 == 0:
|
||||||
|
print(f'Epoch [{epoch + 1}/100], Loss: {loss.item():.4f}')
|
||||||
|
|
||||||
# 评估模型
|
# 评估模型
|
||||||
y_pred_prob = model.predict(X_test).flatten() # 获取预测概率
|
model.eval()
|
||||||
y_pred = (y_pred_prob > 0.5).astype(int)
|
with torch.no_grad():
|
||||||
|
y_pred_prob = model(X_test).numpy().flatten() # 获取预测概率
|
||||||
|
y_pred = (y_pred_prob > 0.5).astype(int)
|
||||||
|
|
||||||
# 评估模型
|
# 评估模型
|
||||||
evaluate_model(y_test, y_pred, y_pred_prob)
|
evaluate_model(y_test.numpy(), y_pred, y_pred_prob)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user