4个特征的csv文件利用lstm模型进行5分类的python代码
时间: 2024-04-15 15:26:28 浏览: 125
svm多分类的python版代码
要使用LSTM模型进行5分类,你可以使用PyTorch库来构建和训练模型。下面是一个示例代码:
```python
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset, DataLoader
# 自定义数据集类
class CustomDataset(Dataset):
def __init__(self, data, labels):
self.data = data
self.labels = labels
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
return self.data[idx], self.labels[idx]
# 自定义LSTM模型类
class LSTMClassifier(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(LSTMClassifier, self).__init__()
self.hidden_size = hidden_size
self.lstm = nn.LSTM(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
_, (h_n, _) = self.lstm(x)
out = self.fc(h_n[-1])
return out
# 读取CSV文件
data = pd.read_csv('data.csv')
# 提取特征列和标签列
features = data[['特征列1', '特征列2', '特征列3', '特征列4']] # 替换为实际的特征列名
labels = data['标签列'] # 替换为实际的标签列名
# 划分训练集和测试集
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size=0.2)
# 转换数据为PyTorch张量
train_dataset = CustomDataset(torch.tensor(train_features.values).float(), torch.tensor(train_labels.values))
test_dataset = CustomDataset(torch.tensor(test_features.values).float(), torch.tensor(test_labels.values))
# 定义训练参数
batch_size = 16
num_epochs = 10
# 初始化数据加载器
train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dataloader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# 初始化模型
input_size = 4 # 输入特征维度
hidden_size = 64 # LSTM隐藏层大小
num_classes = 5 # 分类类别数
model = LSTMClassifier(input_size, hidden_size, num_classes)
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# 训练模型
model.train()
for epoch in range(num_epochs):
running_loss = 0.0
for inputs, labels in train_dataloader:
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f'Epoch {epoch+1}/{num_epochs}, Loss: {running_loss/len(train_dataloader):.4f}')
# 测试模型
model.eval()
total_correct = 0
total_samples = 0
with torch.no_grad():
for inputs, labels in test_dataloader:
outputs = model(inputs)
_, predicted_labels = torch.max(outputs.data, 1)
total_correct += (predicted_labels == labels).sum().item()
total_samples += labels.size(0)
accuracy = total_correct / total_samples
print(f'Test Accuracy: {accuracy:.4f}')
```
在上面的代码中,你需要将 `data.csv` 替换为你的实际数据文件名,并根据实际情况调整特征列和标签列的提取方式。此示例代码使用了一个简单的LSTM模型进行文本分类任务,你可以根据需要调整模型的结构和超参数。
阅读全文