使用神经网络算法对葡萄酒数据集进行分类并生成预测结果图片
时间: 2023-12-14 13:39:02 浏览: 220
基于matlab的SVM神经网络的数据分类预测-葡萄酒种类识别
好的,我可以帮你实现这个任务。这里我会使用Python和PyTorch深度学习框架来完成。
首先,我们需要加载葡萄酒数据集并进行预处理。这个数据集包含了红酒和白酒的13个属性,我们需要将其划分成训练集和测试集,并将其转换为PyTorch张量。
```python
import pandas as pd
import numpy as np
import torch
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# 加载数据集
data = pd.read_csv('wine.csv')
# 划分数据集
X_train, X_test, y_train, y_test = train_test_split(data.drop('class', axis=1).values, data['class'].values, test_size=0.2, random_state=42)
# 标准化数据
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# 转换为PyTorch张量
X_train = torch.tensor(X_train, dtype=torch.float32)
X_test = torch.tensor(X_test, dtype=torch.float32)
y_train = torch.tensor(y_train, dtype=torch.long)
y_test = torch.tensor(y_test, dtype=torch.long)
```
接下来,我们需要定义一个神经网络模型。这里我们使用一个简单的多层感知器(MLP)模型,包含输入层、两个隐藏层和输出层。
```python
class MLP(torch.nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.fc1 = torch.nn.Linear(13, 64)
self.fc2 = torch.nn.Linear(64, 32)
self.fc3 = torch.nn.Linear(32, 3)
self.relu = torch.nn.ReLU()
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
```
然后,我们需要定义损失函数和优化器。这里我们使用交叉熵损失和随机梯度下降优化器。
```python
model = MLP()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
```
接下来,我们可以定义训练和测试函数。
```python
def train(model, optimizer, criterion, train_loader):
model.train()
train_loss = 0.0
train_acc = 0
for data, target in train_loader:
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
train_loss += loss.item() * data.size(0)
_, preds = torch.max(output, 1)
train_acc += torch.sum(preds == target.data)
train_loss = train_loss / len(train_loader.dataset)
train_acc = train_acc / len(train_loader.dataset)
return train_loss, train_acc
def test(model, criterion, test_loader):
model.eval()
test_loss = 0.0
test_acc = 0
with torch.no_grad():
for data, target in test_loader:
output = model(data)
loss = criterion(output, target)
test_loss += loss.item() * data.size(0)
_, preds = torch.max(output, 1)
test_acc += torch.sum(preds == target.data)
test_loss = test_loss / len(test_loader.dataset)
test_acc = test_acc / len(test_loader.dataset)
return test_loss, test_acc
```
最后,我们可以开始训练模型,并输出预测结果。
```python
train_dataset = torch.utils.data.TensorDataset(X_train, y_train)
test_dataset = torch.utils.data.TensorDataset(X_test, y_test)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False)
num_epochs = 100
for epoch in range(num_epochs):
train_loss, train_acc = train(model, optimizer, criterion, train_loader)
test_loss, test_acc = test(model, criterion, test_loader)
print('Epoch: [{}/{}], Train Loss: {:.4f}, Train Acc: {:.2f}%, Test Loss: {:.4f}, Test Acc: {:.2f}%'.format(epoch+1, num_epochs, train_loss, train_acc*100, test_loss, test_acc*100))
# 输出预测结果
model.eval()
with torch.no_grad():
output = model(X_test)
_, preds = torch.max(output, 1)
preds = preds.numpy()
y_test = y_test.numpy()
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
cm = confusion_matrix(y_test, preds)
plt.imshow(cm, cmap=plt.cm.Blues)
plt.title('Confusion matrix')
plt.colorbar()
plt.xticks(np.arange(3))
plt.yticks(np.arange(3))
plt.xlabel('True label')
plt.ylabel('Predicted label')
plt.show()
```
通过运行上述代码,我们可以得到训练和测试的损失以及准确率,并输出预测结果图片。预测结果图片可以帮助我们更直观地了解模型的性能。
阅读全文